I have a form which is like a question paper. It has multiple text fields for entering a question and related information like marks allotted for that question, complexity level, tags in that question etc. I enter all this info into my db. Tags are the keywords in that question. Before i enter that question and its corresponding info into my db, i'm checking if the tags of the current question match with the tags of the questions already in the db in order to ensure no duplicates are entered into my db. Only if ALL the tags match, i want to ignore that question, otherwise insert it. Here is the main part of my php file (t1-t5 are my tag fields and count is the total no. of questions in the form):
if ($count > 0)
{
for ($i=0; $i<$count; $i++)
{
$t1 = $_POST['field5'][$i];
$t2 = $_POST['field6'][$i];
$t3 = $_POST['field7'][$i];
$t4 = $_POST['field8'][$i];
$t5 = $_POST['field9'][$i];
$result = "SELECT * FROM paper WHERE (((`tag1` LIKE '%".$t1."%') OR (`tag2` LIKE '%".$t1."%') OR (`tag3` LIKE '%".$t1."%') OR (`tag4` LIKE '%".$t1."%') OR (`tag5` LIKE '%".$t1."%')) AND
((`tag1` LIKE '%".$t2."%') OR (`tag2` LIKE '%".$t2."%') OR (`tag3` LIKE '%".$t2."%') OR (`tag4` LIKE '%".$t2."%') OR (`tag5` LIKE '%".$t2."%')) AND
((`tag1` LIKE '%".$t3."%') OR (`tag2` LIKE '%".$t3."%') OR (`tag3` LIKE '%".$t3."%') OR (`tag4` LIKE '%".$t3."%') OR (`tag5` LIKE '%".$t3."%')) AND
((`tag1` LIKE '%".$t4."%') OR (`tag2` LIKE '%".$t4."%') OR (`tag3` LIKE '%".$t4."%') OR (`tag4` LIKE '%".$t4."%') OR (`tag5` LIKE '%".$t4."%')) AND
((`tag1` LIKE '%".$t5."%') OR (`tag2` LIKE '%".$t5."%') OR (`tag3` LIKE '%".$t5."%') OR (`tag4` LIKE '%".$t5."%') OR (`tag5` LIKE '%".$t5."%')) ) ;" ;
$sql= mysql_query($result) OR die(mysql_error()) ;
$duplicate = mysql_num_rows($sql);
if( $duplicate > 0)
{ echo "No entry entered for Entry #$i since it may lead to duplicates."; }
else
{ $insertArr[] = "('" .$_POST['field1'][$i]. "', '" .$_POST['field2'][$i]. "', '" .$_POST['field3'][$i]. "', '" .$_POST['field4'][$i]. "', '" .$_POST['field5'][$i]. "', '" .$_POST['field6'][$i]. "', '" .$_POST['field7'][$i]. "', '" .$_POST['field8'][$i]. "', '" .$_POST['field9'][$i]. "', '".$my."', '".$sem."', '".$subj."', '".$branch."')";
}
} **// for loop ends**
$query1 = "INSERT INTO paper (question, marks_allotted, complexity, chp_no, tag1, tag2, tag3, tag4, tag5, monthandyear, semester, subject_name, branch_name) VALUES " . implode(", ", $insertArr);
mysql_query($query1) or trigger_error("Insert failed: " . mysql_error());
} **//if (count>0) ends**
I first tried to enter only two questions via the form. One of them had no matching tags, the other one did. So the new one should be entered in the db, right? But it gives me the following:
No entry entered for Entry #0 since it may lead to duplicates. No entry entered for Entry #1 since it may lead to duplicates.
Notice: Undefined variable: insertArr on line 41
Warning: implode() [function.implode]: Invalid arguments passed on line 41
Notice: Insert failed: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' on line 43
I'm only a beginner. Can someone please assist me as to what changes i should make in my code? Any help is appreciated. Thanks!