Jump to content

[SOLVED] grabbing an auto increment number??


aebstract

Recommended Posts

Hello,

I am entering information in to a database. Everything enters 100% fine, the first column is an auto_increment id number. What I want to do is enter information in to another table, but I need to use that id number that is generated in one of the columns. (and it has to be pretty much instant with the first one, its for a register form) Is this possible? If so, how would I do it? Thanks!

Link to comment
https://forums.phpfreaks.com/topic/42857-solved-grabbing-an-auto-increment-number/
Share on other sites

$result = mysql_query("INSERT INTO users (firstname,midname,lastname,email,password,date_signup,ip_signup,country,address1,address2,city,state,postal,telephone)".
"VALUES ('$firstname', '$midname', '$lastname', '$email', '$password', 'now()', '$ipsignup', '$country', '$address1', '$address2', '$city', '$state', '$postal', '$telephone')") or DIE(mysql_error());

$result2 = mysql_query("INSERT INTO map_users_roles (users_id,roles_id)".
"VALUES ('mysql_insert_id()', '3')") or DIE(mysql_error());

 

Okay, with that function in, it is placing '0' in the column. I also tryed mysql_insert_id(users_id) thinking that maybe I had to put the column name in place? What am I doing wrong here?

I think you may have the usage of the function backwards. Can you describe what you're trying to do? mysql_insert_id() returns the auto_incremented value of a newly inserted row, it doesn't tell you what the next ID is supposed to be:

<?php
$sql = mysql_query("INSERT INTO myTable (name) VALUES ('$val')");
$id = mysql_insert_id(); // returns the ID generated in the above query.
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.