views:

22

answers:

2

Hi i have a form (X) that inserts data into a mysql table (A) and than redirects to another form (Y) which will insert data into another table (B) but i need to use tables A's last inserted row.

I know i can get it from using the mysqli_insert_id function but when i submit to form Y and re show form Y is messes up because the $ID value is nulled. i tried to type cast the value of id by doing this.

$id = (int)mysqli_insert_id($link);

but that didn't help at all.

+1  A: 

You could use sessions to store the value whilst you're processing form X, which can then be picked up by form Y.

X:

session_start();
$_SESSION['insert_id'] = 42;

Y:

session_start();
$insert_id = $_SESSION['insert_id']; // 42

See the manual for an example of how to destroy the session after you're done with it.

chigley
thanks i'll look into sessions
cferbs
+2  A: 

You'll have to persist the results from mysqli_insert_id between forms, either via cookies, session or hidden inputs.

As mysqli_insert_id will only return for the connection instance it's called on, rather than the database as a whole, when you shift to another form, the original connection will closed, and a new one opened, which has not inserted any id's (Hence the null value).

I'd personally go with session, but it all depends on your security requirements.

Psytronic
well my security isn't a big concern this is a very simple internal form for inventory requests. its all one controller index class that just includes either form based on 3 submit values. i think there is only one database connection being made from index class as i put the connection info in a separate file and did include_once 'db.php' to to it. I am new to php and haven't learned cookies or sessions yet.......
cferbs
thanks i'll look into sessions.
cferbs
If there is more than one form submission there will be more than one connection. Though you are connecting via the same details, it's not the same connection instance, like two same chocolate bars. If you eat one, then the second may be the same chocolate bar type, but not the exact same chocolate bar (the first is gone, never to be retrieved).
Psytronic