Click here to Skip to main content
15,890,186 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want the id that matches username to be inserted into information table.
[Account Table]
[Information Table]
infosubmit.php
PHP
<?php
include "connection.php";
session_start();
$username = $_SESSION['username'];
$id = mysqli_query(" SELECT `id` FROM `account` WHERE `username` = '$username' limit 1");
$id = mysqli_real_escape_string($conn, $id);
$first = $_POST['first'];
$last = $_POST['last'];
$birthday =$_POST['birthday'];
$address = $_POST['address1'];
$weight = $_POST['weight'];
$height = $_POST['height'];
$sql = " INSERT INTO `information` (`id`, `first`, `last`, `birthday`, `address`, `weight`, `height`) 
							VALUES ('$id', '$first', '$last', '$birthday', '$address', '$weight', '$height') ";
if ($conn->query($sql) === TRUE) {
    $message = "New record created successfully";
} else {
    $message = "Error: " . $sql . "<br>" . $conn->error;
}
?>
<!DOCTYPE html>
	<head>
		<link rel="apple-touch-icon" sizes="180x180" href="images/favicons/apple-touch-icon.png">
		<link rel="icon" type="image/png" href="images/favicons/favicon-32x32.png" sizes="32x32">
		<link rel="icon" type="image/png" href="images/favicons/favicon-16x16.png" sizes="16x16">
		<link rel="manifest" href="manifest.json">
		<link rel="mask-icon" href="images/favicons/safari-pinned-tab.svg" color="#741372">
		<meta name="apple-mobile-web-app-title" content="Medical Database">
		<meta name="application-name" content="Medical Database">
		<meta name="theme-color" content="#741372">
		<link rel="stylesheet" href="style/main.css" type="text/css">
		<title>Information-Submitted</title>
	</head>
	<body>
		<menu>
			<ul>
				<li><a href="logout.php">Log Out</a></li>
			</ul>
		</menu>
		<?php echo $id; ?>
		<br><?php echo $message; ?>
	</body>
</html>


What I have tried:

looking online, added more php with
mysqli_real_escape_string
&
mysqli_query
Posted
Updated 17-Jan-17 4:52am
v2

You will get the last inserted id using following:
PHP
<?php
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
mysql_select_db('mydb');

mysql_query("INSERT INTO mytable (product) values ('kossu')");
printf("Last inserted record has id %d\n", mysql_insert_id());
?>
 
Share this answer
 
Comments
Katherynliza 17-Jan-17 8:27am    
I would do that it would not necessary be last id create. Because there sign up form and couple days later when they log in get form for additional data.
This
mysqli_query(" SELECT `id` FROM `account` WHERE `username` = '$username' limit 1");
returns a mysqli_result object, not the id value that you think. To get to the id vaiue, check out the Example (MySQLi Procedural)[^]. Always refer to the official PHP: mysqli::query - Manual[^].
I also notice you are using mysqli procedure code for select but mysqli oo for insert. You should adopt only one standard for the whole project.
 
Share this answer
 
Comments
Katherynliza 18-Jan-17 18:43pm    
Sorry new to php. What is mysqli oo?
Peter Leow 18-Jan-17 23:03pm    
MySQLi Object-oriented. Visit the links that are given.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900