Help wiht Mysql_insert_ID. its nots not work

Hi everyone.

sorry to disturb everyone again.

i have been stuck on this problem for 2 days now .

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( ); 


		
			
			
		
		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.
		




   

Notice the issue if I simply give you the two opposing lines?


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

You’re using MySQLi to interact with the database, but you’re trying to use a MySQL function to get the inserted ID. MySQLi and MySQL are different libraries.

Solution?

$id = mysqli_insert_id($dbc);

Wow. that was amazing.
what a silly mistake on my part.

thank you so much for your help. i just tried it and it work.

i am eternally grateful

You’re using mysql_insert_id, but you’re using the mysqli set of functions. Change it to mysqli_insert_id, and you should be good to go. :wink:

Ah, beaten to the post by Jake. Cheers Jake.

Heh, and i beat all of you on codingforums.com :stuck_out_tongue:

Traitor :stuck_out_tongue:

Heh :wink:

hi Tangoforce

thank for the comment regarding
if (isset($_POST[‘submit’])) ?

yes, i have used it. i am still writing the script now. the form is 3 pages long so its taking me a long time to draft the script. but i am getting there.

thank you everyone for all your help and support. its such a scary period (writing the first PHP script)

regards

Andreea

No problem. You should test for another field in your form instead though because IE has a bug with this. If your cursor is in a text box and you hit the enter key then IE will not send the button. If you click it with the mouse it will. Daft eh?.. Only a silly thing but it can make a lot of difference.

I prefer to test for a form submission with:


if('POST' === $_SERVER['REQUEST_METHOD']){
  
}

Of course, this isn’t always suitable depending on what you’re looking for; but should suffice for most of the time.