auto_increment PK help!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

auto_increment PK help!

Post 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!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

mysql_insert_id()
User avatar
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

Post by C_Calav »

what will that do? will that take the PK that i just entered onto the next page?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

read the docs.
User avatar
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

Post by C_Calav »

thanx feyd just read php.net that looks like the function i needed!
User avatar
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

mysql_query('INSERT .....') or die(mysql_error());
$id = mysql_insert_id();
pass $id to the next page.
User avatar
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

indeedily-do.
User avatar
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

Post by C_Calav »

cheers!
User avatar
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

toss it into the session.. or add it to the url you redirect to.. there's a few ways..
User avatar
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

single quotes vs. double quotes.
User avatar
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

Post by C_Calav »

ah thanx :D
Post Reply