Jump to content

check for duplicate record


ploppy

Recommended Posts

hi. i would like some help with checking for duplicate record before it is inserted into db and print some kind of mssage. here is the code i have so far and would appreciate your comments.

 

if (isset($_POST['file_ret'])) {
// extract the array of list choices from the post data
 $file_ret = $_POST['file_ret'];
  
  $numChoices = count($file_ret);
  $insertVal = "";

  for($i = 0; $i < $numChoices; $i++) {
// concatenate each array value to one string variable
  $insertVal .= $file_ret[$i];
   
// put a comma after all but the last value
    if($i != $numChoices - 1) {
     $insertVal .= ",";
 }
 }
echo 'Fil(es) Selected: ' . '<span style="font-weight:bold;color: #000;">' . $insertVal . '</span>' . '<p />'; 
$_SESSION['file_ret'] = $_POST['file_ret'];}
elseif ($insertVal <= 0) {
echo '<span class="stop_error">' . 'You haven\'t chosen a file. you must select a file' . '</span>' . '<p />';}

this is the page that the client can print for there records.

 

and here is the page that inserts into db

 

//File retrival
$activity = $_POST['file'];
if ($activity == 'File Retrival') {
foreach ($_SESSION['file_ret'] as $key=>$value) {
			$query = "UPDATE boxes SET status = '$status' WHERE customer = '$customer' AND department = '$dept' AND custref = '$value'";
			mysql_query($query) or die('Error, query failed');
}
}

 

many thanks

Link to comment
https://forums.phpfreaks.com/topic/104762-check-for-duplicate-record/
Share on other sites

I don't see any insert statement in your SQL.

 

Anyway, if you wish to check for duplicated records, simply put a select statement with a WHERE clause to specify the condition on the searching. Then use mysql_num_rows($query) to get the return rows. If the rows > 0, which mean duplicated. Thats all.

@xeno

 

this is what i have at the moment.

 

<?php 

$insertVal = ""; 

if (isset($_POST['box_add'])) 
{
// extract the array of list choices from the post data
         $box_add = $_POST['box_add'];
  
          $numChoices = count($box_add);

         
          for($i = 0; $i < $numChoices; $i++) {
// concatenate each array value to one string variable
          $insertVal .= "'" . $box_add[$i] . "',";
         } // for($i = 0; $i < $numChoices; $i++)
} // if (isset($_POST['box_add'])) 


$sql = " select ref from box where ref IN( $insertVal '0' )" ;
$res = mysql_query($sql) or die("error on $sql : " . mysql_error());

if (mysql_num_rows($res) != 0)
{
  print "record already in database"; 
}
else {
  if ($insertVal == '')
  {
        echo '<span class="stop_error">' . 'You haven\'t added a box. you must add a box' . '</span>' . '<p />';
  }
  else
  {
      echo 'Fil(es) Added: ' . '<span style="font-weight:bold;color: #000;">' . $insertVal . '</span>' . '<p />';
      $_SESSION['box_add'] = $_POST['box_add'];}
   }


?>

 

what is supposed to happen, is that if the client inputs a value the print "record already in database" is printed. which it does ok. if the value is left blank, what should then happen is the message echo 'Fil(es) Added: ' . '<span style="font-weight:bold;color: #000;">' . $insertVal . '</span>' . '<p />'; should be printed. but instead it prints '',. how can i get this to work. thanks

 

@moon

 

I didn't know there was a time limit. how long am i supposed to wait? 24hrs not long enough? cheers

 

Alright let's clarify your problems.

 

You probably had something like this in your HTML:

<input type="i don't know what is your type" name="box_add[]" />

 

Am I right?

 

So your purpose was wanted to get these list of "box_add" to check whether is already existed in the database or not. Right?

If it is existed, you wish to print : "record already in database!"

 

Else, you will want all these data stored into the SESSION named 'box_add'

 

Ah, also you want to put a validation whereby if the user did't "add the box", you will want to warn them, right?

 

So here is my suggestion code, hope you can catch some idea:

<?php
if(isset($_POST['box_add']) && sizeof($_POST['box_add']) > 0)
{
    // To make sure the user did include the 'box'
    // We will need this variable later on!
    $shall_we_proceed = true;

    // Here, we will loop each box's value and query it to check whether exist or not.
    // Ofcourse, it might be slower than yours previous posting, whereby using only 1 statement.
    // Is up to you to use which algorithm you wish to validate it. ^^
    foreach($_POST['box_add'] as $val)
    {
        $sql = "SELECT * FROM box WHERE ref='$val';";
        $qry = mysql_query($sql) or die(mysql_error());
        
        if(mysql_num_rows($qry))
        {
            echo "Records Existed!!!";
            $shall_we_proceed = false;
            break;    // Quit the loop
        }
    }

    if($shall_we_proceed)
    {
        // Okay! We can proceed!
        $_SESSION['box_add'] = $_POST['box_add'];
    }
}
else
{
    // Error!
    echo "hey! please add box!";
}
?>

 

Hope it work, i did't test out :-[.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.