Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Show unique primary key from record just submitted 1

Status
Not open for further replies.

DrDan1

Technical User
Oct 2, 2002
127
0
0
GB
Hi. The answer to this is probably so simple I'll end up cracking my head on the keyboard later.

Using a very simple example, I have a table with just 2 fields. A primary key consisting of a unique auto-number and a name field. A user would submit their name in a form and the ctahing page will say thanks and show them their unique number. Of course I can't find the unique number by querying against the name, as name's are hardly unique themselves.

Here is my example code. How do I get the $reference variable to be the relevent primary key?

Code:
<?php
    if (isset($_POST['submit'])) {
    	$query = 'INSERT INTO table VALUES(default,' .$_POST['name']. ')';
    	mysql_query($query);
    	
		$reference = 'XXXX';     //Where XXXX is the primary key of the record just submitted (i.e. 'id')
		echo 'Thank you ' .$_POST['name']. '. Your reference is ' .$reference;
		
    }else{
		echo '<p>Submit your name </p>';
		echo '
			<form action="thispage.php" method="post">
				Name: <input type="text" name="name" /><br />
				<input type="submit" name="submit" value="Submit your name" />
			</form>';
    }
?>

----------------------------------------
Knowing is not enough, we must apply. Willing is not enough, we must do.
--So said the ever wise Bruce Lee
Memorize and live by it!
 
Now I'm going to have to buy a new keyboard :)

Thanks. I knew it's be something simple!

----------------------------------------
Knowing is not enough, we must apply. Willing is not enough, we must do.
--So said the ever wise Bruce Lee
Memorize and live by it!
 
Hi, remember to also wrap that inside a mysql_affected_rows.

ref.:
I usually do like so:

PSUEDOCODE
Code:
If (mysql_affected_rows() > 0) {
  ;
  printf ("\n\t<p>The data was stored with ID <strong>%s</strong></p>\n", mysql_insert_id());
  }
else {
  echo "\n\t<p>Failed to insert data, please try again.</p>\n";
}

I would also like to point out that your script is very open to SQL Injection.

Read here:

I recommend you to use the -Example #3 A "Best Practice" query-.

You can also leave out the php file from the action in the <form>, as it then will submit to self.

Maybe also have a dropdown list which asks something like "are you human?" or it might ask: 1+1= and the user has to choose 2 in the list. It might prevent spambots-registrations, if you care about such issues.

However, secure your SQL data..
Also look on search about functions:
trim
strip_tags
strtolower
ucfirst
htmlentities

If you make some coffe and read up on those functions, I'm sure you will make many great additions to your script :)

Olav Alexander Mjelde
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top