Skip navigation
_AJD_TUS_
Currently Being Moderated

Pass Values through PHP

Jan 24, 2012 6:01 AM

Tags: #php #database #mysql #form #submit

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]")

 
Replies
  • Currently Being Moderated
    Jan 24, 2012 7:34 AM   in reply to _AJD_TUS_

    Use mysql_insert_id()

     
    |
    Mark as:
  • Currently Being Moderated
    Jan 24, 2012 7:53 AM   in reply to _AJD_TUS_

    Sorry, it's a PHP function.

     
    |
    Mark as:
  • Currently Being Moderated
    Jan 24, 2012 8:23 AM   in reply to _AJD_TUS_

    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.

     
    |
    Mark as:
  • Currently Being Moderated
    Jan 24, 2012 9:12 AM   in reply to _AJD_TUS_

    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.

     
    |
    Mark as:
  • Currently Being Moderated
    Jan 24, 2012 9:25 AM   in reply to _AJD_TUS_

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

     
    |
    Mark as:
  • Currently Being Moderated
    Jan 25, 2012 8:47 PM   in reply to _AJD_TUS_

    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.

     

    Example:

    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.

     
    |
    Mark as:
  • Currently Being Moderated
    Jan 28, 2012 2:29 AM   in reply to _AJD_TUS_

    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.

     

    Tony

     
    |
    Mark as:
  • Currently Being Moderated
    Jan 28, 2012 8:42 AM   in reply to tonybabb

    Tony,

    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.

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points