doc1355 Posted September 5, 2007 Share Posted September 5, 2007 Hi, I'm new to php and I'm trying to work on a form with php and Mysql but I have a question. After a visitors submits the form, the data is saved in database table in a new row and a unique ID will be given to that row. I am trying to work with the data after it has been created by retrieving the ID. I want to send the ID to payment page, and after the payment is complete, change one field in that row to show that the payment is done). I don't know how can I get the ID for that record back from the database so I pass it on to next script. Thanks for help. Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted September 5, 2007 Share Posted September 5, 2007 Well, first off you select the ID from the database <?php $query = mysql_query("SELECT ID FROM table WHERE condition")or die(mysql_error()); $row = mysql_fetch_assoc($query); ?> To pass it on, you can use either a session, cookie, or hidden form input. If you have a submit button on that page, I would recommend the hidden form input...if not, I would use a session. Solution #1 - Hidden form Input <input type='hidden' name='id' value='<?php echo $row['id']; ?>'> Solution #2 - Session <?php $_SESSION['id'] = $row['id']; ?> Quote Link to comment Share on other sites More sharing options...
doc1355 Posted September 5, 2007 Author Share Posted September 5, 2007 thanks pocobueno1388 for your reply. My problem is not passing the data. My question is how can I retrieve the id related to that order back. The ID is created by MySQL and it is auto_increment. I want to retrieve the id related to the the same form data that has been inserted into the database. I hope you got my point. Thanks Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted September 5, 2007 Share Posted September 5, 2007 common question and your solution is mysql_insert_id(); Retrieves last inserted ID of a query on that page, set it to a variable and email to the user/store it somewhere do what ever you want with it. http://us.php.net/manual/en/function.mysql-insert-id.php Quote Link to comment Share on other sites More sharing options...
doc1355 Posted September 5, 2007 Author Share Posted September 5, 2007 common question and your solution is mysql_insert_id(); Retrieves last inserted ID of a query on that page, set it to a variable and email to the user/store it somewhere do what ever you want with it. http://us.php.net/manual/en/function.mysql-insert-id.php What if another form submits just before the first one complete? Will that make any confusion? How about creating a session and then saving the the session ID in the database? I heard about this, but I don't know how to insert session ID in MySql. Do you guys have any suggestion about this method? Thanks Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted September 5, 2007 Share Posted September 5, 2007 it is relative to the last query on that concurrent connection, not the last inserted, which is a common issue with flat files requriing documents to be locked down while one person executes a query then released. example <?php $q[0] = "Insert this into sql"; $q[1] = "insert some more"; $q[2] = "one more isnert"; $id[1] = mysql_insert_id(); //$id[1] is the id of $q[2] regardless of another page concurrently inserting and saying the last global insert was $q[0] on a different users execution. ?> Quote Link to comment Share on other sites More sharing options...
doc1355 Posted September 5, 2007 Author Share Posted September 5, 2007 I think that works. Thanks Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.