tags:

views:

57

answers:

4

dear all. i have tried to prevent the duplicate data at my project. but until now it still make duplicate. i try this code but still not work:

  $cek_user= "SELECT Model, Serial_number, Line FROM inspection_report WHERE Model='".$Model."' AND Serial_number='".$Serial_number."' AND Line='".$Line."'";
  $cek_data=mysql_num_rows($cek_user);
  if($cek_data!=0){
                    echo "Data already exists!";
                   }
  else{
  $sql = "INSERT INTO inspection_report ";
  $sql.= "(Model,       Serial_number,       Line,        Shift,     Inspection_datetime,         Range_sampling,       Packing, ";
  $sql.= "Accesories,       Appearance,      Tuner,        General_operation,       Remark, ";
  $sql.= "NIK) ";
  $sql.= "VALUES ('";
  $sql.= $Model."','".$Serial_number."', '".$Line."','".$Shift."','".postVar('insp_date')." ".postVar('time')."','".$Range_sampling."','".$Packing."','";
  $sql.= $Accesories."','".$Appearance."','".$Tuner."','".$General_operation."','".$Remark."','";
  $sql.= $NIK."')";
//echo $sql;

$result=mysql_query($sql) or die(_ERROR26.": ".mysql_error());
//echo $result;
}
mysql_close($dbc);
}

but still not work,please help.

+1  A: 

This will not prevent duplicates unless your table also has a UNIQUE constraint somewhere allowing the database to determine what you mean by a duplicate. If you have such a constraint, perhaps you could post your table definition.

Brian Hooper
i have field name as "ID" and auto-increment
klox
The auto-increment will prevent any completely duplicated rows from appearing, because it will make sure they differ in the auto-increment column. What is the unique identifier for the real objects you are modelling in your table? You need to create a `UNIQUE KEY` or `UNIQUE INDEX` on the columns containing the unique identifer. In your case, Model and Serial_Number might be the unique identifier. Create a unique index on those columns.
Brian Hooper
are you mean i must change model and serial settings become UNIQUE KEY?and what do i do after change them?
klox
Not necessarily the Model and Serial_Number; the unique index must be whatever identifies your row. I don't know which columns they are because I don't know what your table is for. But when you have created a unique key, duplicates will not be inserted into the table, and errors will be returned if this is attempted. Your error trapping may need a little refinement as well. Echo the `mysql_error()` to see what the messages really are.
Brian Hooper
+1  A: 

You can do a select before insert, eg. Select id from table where serial_number = '$serial_number' If mysql_num_rows equals 0, do insert. This assumes serial_number is unique for each row.

$sql = "SELECT ID FROM inspection_report WHERE Serial_number = '$Serial_number'";
$result = mysql_query($sql);

if(mysql_num_rows($result) == 0){
  $sql = "your insert sql..."
  $result = mysql_query($sql);
}
subroutines
can you type as mysql or php script,it more easy to read
klox
php script added.
subroutines
oh no..it becomes show 500 internal server error.
klox
A: 

You do realize your're running the INSERT query twice, right?

if ( mysql_query($sql) ) {
     ^^^^^^^^^^^--- here
    [.... snip ....]
} 
$result=mysql_query($sql) or die(_ERROR26.": ".mysql_error());
        ^^^^^^^^^^^--- and here

As well, you should look into using HEREDOCs to build your query string. That long chunk of string concatenation and quote-soup you've got could look like this with a HEREDOC:

$insp_date = postVar('insp_date') . ' ' . postVar('time');
$sql = <<<EOL
INSERT INTO inspection_report
    (Model, Serial_number, Line, Shift, Inspection_datetime,
    Range_sampling, Packing, Accesories, Appearance, Tuner,
    General_operation, Remark, NIK)
VALUES (
    $Model, $Serial_number, $Line, $Shift, $insp_date,
    $Range_sampling, $Packing, $Accesories, $Appearance, $Tuner,
    $General_operation, $Remark, $NIK)
EOL;

every so slightly more readable.

edit/comment followup:

You're running the query twice, in the spots where I've put the '^^^^^--- here' lines.

First instance: if ( mysql_query($sql) ) {
Second instance: $result = mysql_query($sql) or die.......

You haven't changed the contents of $sql between the two mysql_query() calls, so when you do the second call, it runs the exact same query string, which is your INSERT query. So you end up inserting the data TWICE.

Beyond that, your error handling is atrocious. Scanning an error string for a particular string is the wrong way to go about it. The error text might change (think of what would happen if your code runs on a server running in (say) a German location, which has localized error messages and spits out "Doppelter eintrag für ..." instead of "Duplicate entry for". What you should have is something like this:

$sql = "... your query here ... "

$result = mysql_query($sql); // if query fails, this returns FALSE

if ($result === FALSE) {
    die("MySQL error: " . mysql_error());
}

If you need to check for a particular error that could be corrected by your code, you can use mysql_errno() to retrieve the server error code, and work from there. Using your example, 'Duplicate entry' is error # 1062 (full error codes documented here), so you'd do

if (mysql_error() == 1062) {
    ... handle error here ...
}
Marc B
if ( mysql_query($sql) ) { ^^^^^^^^^^^--- here [.... snip ....]} $result=mysql_query($sql) or die(_ERROR26.": ".mysql_error()); ^^^^^^^^^^^--- and herei'm not understand what you mean..
klox
A: 

first of all:

ALTER inspection_report ADD UNIQUE(Model, Serial_number, Line);

Then:

$sql = "INSERT IGNORE INTO..........";
klox