...

View Full Version : Help wiht Mysql_insert_ID. its nots not work



andreea115
05-29-2011, 12:26 PM
Hi everyone.

i am new to PHP and designing my first database. i realize that this question has been asked many time and i have studied everything, but nothing seems to help

i have created a database and i am able to populate the database. however the Mysql_insert_id does not seem to collect the ID . i am at a complete loss where i am going wrong.

any help will be appreciated.

i enclose below the SQL for the database and the PHP script for my insertion.


USE global;
CREATE TABLE users(
user_id MEDIUMINT UNSIGNED NOT NULL AUTO_INCREMENT,
email VARCHAR (60) NOT NULL,
pass CHAR(40) NOT NULL,
reg_date DATETIME NOT NULL,
first_name VARCHAR(20) NOT NULL,
surname VARCHAR(20) NOT NULL,
nationality TINYTEXT NOT NULL ,
PRIMARY KEY (user_id));










// Make the query:
//$q = "INSERT INTO users (first_name, last_name, email, pass, reg_date) VALUES ('$fn', '$ln', '$e', SHA1('$p'), NOW() )";

$q = "INSERT INTO users
( email, pass, first_name, last_name , country_loc, reg_date) VALUES ('$EA', SHA1('$PW'), '$FN', '$LN', '$CL', NOW() )";


$r = mysqli_query ($dbc, $q); // Run the query.



$id = mysql_insert_id( $r );






if ($r) { // If it ran OK.





// Print a message:
echo "$id";
echo '<h1>Thank you!</h1>
<p>You are now registered. In Chapter 11 you will actually be able to log in!</p><p><br /></p>';

} else { // If it did not run OK.

// Public message:
echo '<h1>System Error</h1>
<p class="error">You could not be registered due to a system error. We apologize for any inconvenience.</p>';

// Debugging message:
echo '<p>' . mysqli_error($dbc) . '<br /><br />Query: ' . $q . '</p>';

} // End of if ($r) IF.

tangoforce
05-29-2011, 12:39 PM
Two issues:

You're using both mysql and mysqli functions.

Secondly:


$r = mysqli_query ($dbc, $q); // Run the query.

//This should be a mysql resource - $dbc not $r
$id = mysql_insert_id( $r );


Some interesting links on php.net:
Mysql_insert_id (http://uk3.php.net/mysql_insert_id)

Mysqli->insert_id (http://uk.php.net/manual/en/mysqli.insert-id.php)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum