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
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?

Link to comment
Share on other sites

I think it's trying to place the string "mysql_insert_id()" into the int column so it enters 0. Try this:

 

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

 

Orio.

Link to comment
Share on other sites

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.
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.