• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 315
  • Last Modified:

check for duplicate value in input

I have code that checks mysql for duplicate entries in a database and issues error through ajax. That works fine. My problem is that I have no way for checking duplicate entries in the form when the form is sent to php for processing. The inputs are created by a jquery change on a slider so there could many inputs created by the user from which they create unique codes. What I am trying to do is check that if a user enters the same number in the form then issue an error. Am I better doing this before it reaches php using jquery?  I hope someone can help with this. Thanks

$duplicates = array();

foreach ($boxnumber as $val)
{
if ( $val != "" )
 {
  mysql_select_db($database_logistor, $logistor);
  $sql = "SELECT custref FROM boxes WHERE custref='$val' and status = 'In'";
  $qry = mysql_query($sql) or die(mysql_error());

  if(mysql_num_rows($qry) < 1)
  {
  $duplicates[] = '[ '.$val.' ]';
  $flag=1;
  }
  
 }
}

Open in new window

0
peter_coop
Asked:
peter_coop
1 Solution
 
acbxyzCommented:
If you want to do this in PHP you can use array_unique()
$uniqueboxes = array_unique($boxnumber);
if (count($uniqueboxes) != count($boxnumber)) {
  // there are duplicate boxes typed in
  // if you want to know which:
  $dupinputs = array();
  foreach (array_count_values($boxnumber) as $box => $count) {
    if ($count > 1) {
      $dupinputs[] = $box;
    }
  }
  // since array_diff() ignores duplicate values on input array this doesn't work
  // $dupinputs = array_diff($boxnumber, $uniqueboxes);
}

Open in new window

0
 
peter_coopAuthor Commented:
perfect. thank you
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now