I'm trying to pass a value for on-screen analysis in the following situation:
The user fills in a form. When the form is submitted to the MySQL database, it is given an ID (performed by the table - it automatically increments new entry IDs).
When the user submits the Form (form.php), the information gets added to this database and the user gets sent to a "Thank You" page (thanku.php)
Is it possible to show the ID of that entry on the "Thank You" page? (i.e. "Thank you for your submission. Your ID is [ID]")
So I'm assuming it would be:
$link = mysql_connect("mysql_host", "mysql_user", "mysql_password")
or die ("Could not connect");
$result = mysql_query($query);
Is that right? I'm struggling here.
Right after you insert the new record, you run the following:
$new_id = mysql_insert_id();
That will populate the variable $new_id with the id.
To answer your most recent question, if you are adding lots of records at once, you need to run mysql_insert_id after each insert.
You would put $new_id=mysql_insert_id in the head if that's where your insert statement is. Like I said, you put it immediately following your insert.
I'm not sure what you mean by "at the appropriate anchor in the body" It depends on what you are trying to do with it.
Usually this function is used as follows: You are creating a new record but not all the data is going into the same table, You need to retrieve the id so you can use it as the foreign key for the subsequent table inserts.
>Also, what do you do if there are lots of records being added at once?
You need to clarify that question. How are many records being added at once?
Through single insert statement? Multiple insert statements? Multiple users running a single insert statement?
Different unique IDs will be created for different users connected to the page.
It doesn't matter if you put it immediately after insert statement or not.
mysql_insert_id(RESOURCE) will always return the last insert ID made by that user only.
Let's say John entered your site and filled the form. If you can, theoretically, stop your PHP code for few hours between insert and mysql_insert_id staements, AND 100 people filled the form at that time, John will have same unique ID created by his own mysql session, not the last id of the whole table.
Slightly off topic but something for you to consider:
If the MySQL db is creating the insert_ID is it doing this sequentially - i.e. is it incrementing the ID each time a user adds a record? Does it create a security issue for you if the user knows what their ID is, for example, do you have a facility where the user enters the insert_ID to retrieve their info? It would be pretty simple to subtract 1 from your own insert_ID and see what someone else entered.
Any system is as secure as your code. You should not let any user retrieve any kind of data with only insert id. You should implement a proper login system and data retrieving privileges for each user.
Europe, Middle East and Africa