Jump to content

[SOLVED] Send data to db and retrieve


doc1355

Recommended Posts

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.

Link to comment
Share on other sites

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'];
?>

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.
?>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.