I want to insert differently i.e. multiple entries into the database, but input is taken from the same form.
I have a form called questionForm. It is a static form, and has a text field for inserting question and corresponding text fields like marks allotted for that question, complexity level of that question, tag1 to tag8 (keywords in that question) present in that question, etc.
Now i want that each of these questions be inserted in my database when the form is submitted. But not before the tags of each of the question entered in the form are compared to the tags of each question in the database to ensure that there are no duplicates. How do i do this.
I know to insert an entry into the db via a simple form. Could someone assist me please.
I have reached almost halfway with the coding now. I am able to insert the entries into the form now, but it does not check properly for duplicates. Here's my question form and php file -
Question form
Test Quest
Enter Subject Details.
Month and Year: Semester: Subject: Branch:<h3>Enter Questions</h3><br>
<h3>Question 1: Five marks each.<br></h3>
a) <input type="text" name="field1[]" size=45>* Marks: <input type="text" name="field2[]"> Complexity:<input type="text" name="field3[]"> Chapter No.: <input type="text" name="field4[]"> Tags: <input type="text" name="field5[]" size=10> <input type="text" name="field6[]" size=10> <input type="text" name="field7[]" size=10> <input type="text" name="field8[]" size=10> <input type="text" name="field9[]" size=10> <br>
b) <input type="text" name="field1[]" size=45>* Marks: <input type="text" name="field2[]"> Complexity:<input type="text" name="field3[]"> Chapter No.: <input type="text" name="field4[]"> Tags: <input type="text" name="field5[]" size=10> <input type="text" name="field6[]" size=10> <input type="text" name="field7[]" size=10> <input type="text" name="field8[]" size=10> <input type="text" name="field9[]" size=10> <br>
<p><input type="submit" name="submit" value="Submit" />
<input type='reset' name='Cancel' value='Cancel' /></p>
</form>
</body>
</html>
quest.php
include('connectionfile.php');
$cnt = count($_POST['field1']);
$my= $_POST['my'];
$sem= $_POST['sem'];
$subj= $_POST['subj'];
$branch= $_POST['branch'];
if ($cnt > 0) {
for ($i=0; $i<$cnt; $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 (`tag1` LIKE '%".$t2."%') OR (`tag1` LIKE '%".$t3."%') OR (`tag1` LIKE '%".$t4."%') OR (`tag1` LIKE '%".$t5."%')) AND ((`tag2` LIKE '%".$t2."%') OR (`tag2` LIKE '%".$t3."%') OR (`tag2` LIKE '%".$t4."%') OR (`tag2` LIKE '%".$t5."%')) AND ((`tag3` LIKE '%".$t3."%') OR (`tag3` LIKE '%".$t4."%') OR (`tag3` LIKE '%".$t5."%')) AND ((`tag4` LIKE '%".$t4."%') OR (`tag4` LIKE '%".$t5."%')) AND ((`tag5` LIKE '%".$t5."%')) ) ;" ; // which checks if the tags in that question match with the tags of any other question in the db.
$sql= mysql_query($result) OR die(mysql_error()) ;
$duplicates = mysql_num_rows($sql);
if( $duplicates > 0)
echo "No entry entered for Entry #$i since it may lead to duplicates."; // no entry is inserted in insertArr[] if it leads to duplication
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."')";
}
$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());
}
echo("<pre>\n");
print_r($_POST);
echo("</pre>\n");
mysql_close($id_link);
?>
On running, gives me the following despite the fact that the questions that i enter have no matching tags with the existing ones in the db: 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
Guide me please