0

I have a problem with mysql duplication. I have two tables name as student and temp_student. Both tables have same fields except for temp_student, i've add studentStatus and statusNote. The problem is, when I use mysql_num_rows for any tables to search the duplication data, such as student name, it echos the duplication error. But when i use mysql_num_rows for both tables, and i inserted the same student name into both table, the data still duplicate. Here is my code:

$name = $_POST['name'];
$checkName = mysql_query("select *from student where name = '$name'");
$checkNameTemp = mysql_query("select * from temp_student where name = '$name");

//$check = $checkName + $checkNameTemp;

if(mysql_num_rows($checkName) && mysql_num_rows($checkNameTemp)  > 0){
echo "Duplicate Data";
}
else{

$insertStudentQuery = mysql_query("INSERT INTO student(name,noic, createDate) VALUES('$name','$Ic', NOW())");

$insertStudentQueryTemporary = mysql_query("INSERT INTO temp_student(name,noic, createDate) VALUES('$name','$Ic', NOW())");

}
Tiny
  • 27,221
  • 105
  • 339
  • 599
  • Don't use the `mysql_*()` family of functions. They're deprecated. Check out MySQLi or PDO instead. – BenM Mar 13 '14 at 11:53
  • Us join instead of two queries – Oyeme Mar 13 '14 at 11:53
  • You need to join the student table to the temp_student table, then use select count(id) would be more efficient than select *, as you only want a count, so no point returning all data. – i-CONICA Mar 13 '14 at 11:54

1 Answers1

0

Your IF will only hit when both have more than 0 rows. Not if any has. Use || to check for any non zero rows.

if(mysql_num_rows($checkName) || mysql_num_rows($checkNameTemp)){
    echo "Duplicate Data";
}
ToBe
  • 2,667
  • 1
  • 18
  • 30