Jump to content

[SOLVED] Checking for duplicates in database on form submit


atl_andy

Recommended Posts

I had the idea to use the following to check for duplicate value when a form is submitted to a database:

 

elseif ($result == $_POST['serialnumber']) {
         $db_handle = pg_connect('dbname=test2 user=andy password=da4531');
         $query = 'SELECT serialnumber FROM system';
         $result = pg_query($query);
         pg_close($db_handle);
       echo "<h2><b>Duplicate serial number!  Go back, change serial number and submit form again.<b></h2><br />";
   }

 

I can't figure out how to get $_POST['serialnumber'] to check against each record of $result.  Any ideas?  All values of 'serialnumber' need to be unique.

Link to comment
Share on other sites

<?php
// checks if the serialnumber is in use
if (!get_magic_quotes_gpc()) {
$_POST['serialnumber'] = addslashes($_POST['serialnumber']);
}
$serialcheckcheck = $_POST['serialnumber'];
$check = mysql_query("SELECT serialnumber FROM systems WHERE serialnumber = '$serialcheck'") 
or die(mysql_error());
$check2 = mysql_num_rows($check);

//if the serial exists it gives an error
if ($check2 != 0) {
die('Sorry, the serial number '.$_POST['serialnumber'].' is already in use.');
}
?>

Link to comment
Share on other sites

I'm not sure where your coming from with the elseif but you would simply check the posted value in your query, something like....

 

<?php

 $sql = "SELECT serialnumber FROM system WHERE serialnumber = '{$_POST['serialnumber']}";
 if ($result = pg_query($sql)) {
   if (pg_num_rows($result)) {
     // you have a duplicate.
   } else {
     // No duplicate, good to do insert.
   }
 }

?>

Link to comment
Share on other sites

It is going past the validation and sending the data to the table.    Should the pg_connect, query, pg_close be inside the if statement?  The syntax I used is:

 

   // Serial number validation
  $db_handle = pg_connect('dbname=test2 user=andy password=da4531');
   $sql = "SELECT serialnumber FROM system WHERE serialnumber = {$_POST['serialnumber']}";

   if ($result = pg_query($sql)) {
      if (pg_num_rows($result)) {
      echo "<h2><b>Duplicate serial number!  Go back, change serial number and submit form again.<b></h2><br />";
      }
      pg_close($db_handle);
   } else ...

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.