Page 1 of 1
Trying to send a AUTO_INCREMENT value to a different table
Posted: Sat Feb 27, 2010 10:31 am
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';
?>
Re: Trying to send a AUTO_INCREMENT value to a different table
Posted: Sat Feb 27, 2010 10:35 am
by requinix
Strings
Variables in single quotes don't work. Check that link for alternatives.
Re: Trying to send a AUTO_INCREMENT value to a different table
Posted: Sat Feb 27, 2010 11:33 am
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)
Re: Trying to send a AUTO_INCREMENT value to a different table
Posted: Sat Feb 27, 2010 11:57 am
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.
Re: Trying to send a AUTO_INCREMENT value to a different table
Posted: Sat Feb 27, 2010 2:03 pm
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!!
Re: Trying to send a AUTO_INCREMENT value to a different table
Posted: Sat Feb 27, 2010 2:35 pm
by cher41
Ok, I had that all wrong!
I figured it out and got it to work!
Thanks for the help!