Page 1 of 1

auto_increment PK help!

Posted: Thu Jan 20, 2005 2:52 pm
by C_Calav
hi guys,

i was wondering if some could help me with some codeing/query in this script.


i am inserting some details to start with.

Code: Select all

$s_title = $_POSTї"txt_title"];
$s_intro = $_POSTї"txt_intro"];
$s_footer = $_POSTї"txt_footer"];
 
$sql  = "INSERT INTO tbl_survey (s_title,s_intro,s_footer) VALUES ('$s_title','$s_intro','$s_footer')";
there is also the feild s_id which is the PK and is a auto_increment.

then the next page i want to select or pass over the s_id and insert it as a FK.

but the problem is how can i select the s_id that was just created on the next page?

is there a easy way? normally i would post it over but since mysql is auto creating it i dont have control over it.

first time using the PK thats a auto number on the next page, just getting confused if someone could help me that would be great!

Posted: Thu Jan 20, 2005 3:03 pm
by feyd
mysql_insert_id()

Posted: Thu Jan 20, 2005 3:11 pm
by C_Calav
what will that do? will that take the PK that i just entered onto the next page?

Posted: Thu Jan 20, 2005 3:22 pm
by feyd
read the docs.

Posted: Thu Jan 20, 2005 3:23 pm
by C_Calav
thanx feyd just read php.net that looks like the function i needed!

Posted: Thu Jan 20, 2005 3:34 pm
by C_Calav
on my second page i go

Code: Select all

$id = mysql_insert_id()
and return a value of 0.

is that how i use mysql_insert_id() correctly?

Posted: Thu Jan 20, 2005 3:42 pm
by feyd

Code: Select all

mysql_query('INSERT .....') or die(mysql_error());
$id = mysql_insert_id();
pass $id to the next page.

Posted: Thu Jan 20, 2005 3:52 pm
by C_Calav
oh so i use $id = mysql_insert_id();on the page where i insert the PK, and then pass it over to the page i want to use it on?

Posted: Thu Jan 20, 2005 3:55 pm
by feyd
indeedily-do.

Posted: Thu Jan 20, 2005 3:59 pm
by C_Calav
cheers!

Posted: Thu Jan 20, 2005 4:07 pm
by C_Calav
hey feyd, how do i get it from:

$id = mysql_insert_id();

to the next page so i can use it if i redirect after my insert?

Posted: Thu Jan 20, 2005 4:12 pm
by feyd
toss it into the session.. or add it to the url you redirect to.. there's a few ways..

Posted: Thu Jan 20, 2005 4:55 pm
by C_Calav
can i do this?

header('Location: /survey/questions.php?s_id=$id');


it outputs as this.. am i doing something wrong?

http://localhost/survey/questions.php?s_id=$id

Posted: Thu Jan 20, 2005 4:57 pm
by feyd
single quotes vs. double quotes.

Posted: Thu Jan 20, 2005 4:59 pm
by C_Calav
ah thanx :D