Trying to send a AUTO_INCREMENT value to a different table

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
cher41
Forum Newbie
Posts: 9
Joined: Sat Feb 27, 2010 10:25 am

Trying to send a AUTO_INCREMENT value to a different table

Post by cher41 »

I am new to PHP and MySQL and I have researched this but haven't found an answer to my problem.

I am trying to use mysqli_insert_id() to transfer its value to another table.

On my first page I have inserted data into a table. I then used the mysqli_insert_id() to find the value. This works. I have set it to a value like this:

$cust_id = mysqli_insert_id($mysqli)

When I echo $cust_id it produces the correct value.

I am trying to send this value to new page so I can insert into a new table. I am using sessions and have used this code:
its not working!
<?php
session_start();
$_SESSION["$cust_id"]='$cust_id';
?>
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Trying to send a AUTO_INCREMENT value to a different table

Post by requinix »

Strings

Variables in single quotes don't work. Check that link for alternatives.
cher41
Forum Newbie
Posts: 9
Joined: Sat Feb 27, 2010 10:25 am

Re: Trying to send a AUTO_INCREMENT value to a different table

Post by cher41 »

I have messed around all night with the quotes, names and everything. The string isn't getting passed.

Is there something I am supposed to do on the first page to put it in a session - through a POST or something.

All I have done on the first page is the $cust_id = mysqli_insert_id($mysqli)
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Trying to send a AUTO_INCREMENT value to a different table

Post by califdon »

So you want to create a session variable containing the cust_id obtained in one script, so that you can use it in a second script, right? You have successfully obtained the desired value. Following Tasairis's directions of removing the single quotes ($cust_id is already a string, you don't want extra quotes around it), and assuming that the session_start(); command is the first line of script1, you should then have the value as the session variable. Your second script must also begin with a session_start(); command. Then you should be able to use the session variable or assign it to a new variable name in the second script.
cher41
Forum Newbie
Posts: 9
Joined: Sat Feb 27, 2010 10:25 am

Re: Trying to send a AUTO_INCREMENT value to a different table

Post by cher41 »

First of all Thank you for your patience and help!

Califdon - yes you are right this is what I am looking for.

This is what I have, which I think is per your instructions and have tried before but it doesn't seem to be working!

First page:
first line:
<?php
session_start();
$_SESSION["$cust_id"]=$cust_id;
?>

and then in the middle of the page i have the line:
$cust_id = mysqli_insert_id($mysqli)
I have an echo $cust_id to see if it is getting the value and it is working.

Second page:
first line:

<?php
session_start();
$_SESSION["$cust_id"]=$cust_id;
?>

I have an echo $cust_id and I get nothing!!
cher41
Forum Newbie
Posts: 9
Joined: Sat Feb 27, 2010 10:25 am

Re: Trying to send a AUTO_INCREMENT value to a different table

Post by cher41 »

Ok, I had that all wrong!

I figured it out and got it to work!

Thanks for the help!
Post Reply