0

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!

Edited by beginnerpals

4
Contributors
10
Replies
35
Views
4 Years
Discussion Span
Last Post by beginnerpals
Featured Replies
  • Yay! Solved! Just changed the query1 to: $query1 = "INSERT INTO paper (question, marks_allotted, complexity, chp_no, tag1, tag2, tag3, tag4, tag5, monthandyear, semester, subject_name, branch_name) VALUES " . implode(", ", $insertArr); It entered the question that was a new one, and ignored the question that was a duplicate! Thank you … Read More

0

Im only a beginner myself, but until someone better qualified answers, I can see spaces where I don't think they should be

'" .$_POST['field1'][$i]. "',

I think should be

'".$_POST['field1'][$i]."',

ect, and this

VALUES " . implode(", ", $insertArr);

I think should be

VALUES(". implode(", ", $insertArr));

Hope it helps.

0

Just a thought here: why don't you just use one field that you name "tags" (in your database). You can insert a comma separated list of tags into that field. When you retrieve it, you can explode it on the comma to create an array with tags. In that case you also don't have to check 5 separate fields for tags, you can suffice by asking your database to return a value "WHERE tags LIKE '%...%'".

For the rest, what is that semicolon (;) doing at the end of your SELECT query?

And for these error:

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

Could you show us what's on line 41 and 43? :)

Edited by minitauros

0

And I'm sure there's an extra closing bracket there.

((`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."%')) ) ;" ;
0

Oh by the way, it doesn't matter how many spaces you put between the dots, a tip for both of you then ^^. Whatever you find most comfortable to read!

0

$query1 = "INSERT INTO paper (question, marks_allotted, complexity, chp_no, tag1, tag2, tag3, tag4, tag5, monthandyear, semester, subject_name, branch_name) VALUES " . implode(", ", $insertArr);
you stated there something that is ALMOST correct syntax. this is the error for line 43, it is the mysql error.

kindly place opening parenthesis "(" after the word VALUES then after the implode function, kindly place a period followed by opening double quote then followed by the closing parenthesis and last is the closing double quote before the semicolon.

it will look like this

Values (".implode(", "$insertArr).")";

0

now that I repaired the $query1 of yours you'll have to echo $query1 and make the mysql_query() to comment it for now temporarily. The reason I want to echo $query1 is to display the string of $query1 and see if implode function is working to your idea

0

TonyG_cyprus Don't think extra spaces make a difference. Made the changes in the implode function and removed the extra braces, semicolon :) .

Thanks for the suggestion Minitauros :) but i'm already in a fix right now, so once this works i'd try to incorporate all tags into one field. Thanks to masterjiraya for pointing out errors in my query1 :).

Changed the query1 as you all suggested, and echoed it. Gives me this:

INSERT INTO paper (question, marks_allotted, complexity, chp_no, tag1, tag2, tag3, tag4, tag5, monthandyear, semester, subject_name, branch_name) VALUES (('what is rabbit?', '5', 'Easy', '3', 'rabbit', '', '', '', '', 'December 2012', '7', 'System Security', 'Computer Engineering'), ('what is worm?', '5', 'Hard', '3', 'worm', '', '', '', '', 'December 2012', '7', 'System Security', 'Computer Engineering'))
Notice: Insert failed: Column count doesn't match value count at row 1 in quest1.php on line 37 (i.e. the line of code where query1 is written)

Plus, do i need to define insertArr at the start? If yes, is $insertArr[]; the correct syntax for that? New to the language so please bear with me if i ask/say something stupid :) Thanks again!

1

Yay! Solved! Just changed the query1 to:

$query1 = "INSERT INTO paper (question, marks_allotted, complexity, chp_no, tag1, tag2, tag3, tag4, tag5, monthandyear, semester, subject_name, branch_name) VALUES " . implode(", ", $insertArr);

It entered the question that was a new one, and ignored the question that was a duplicate!
Thank you very much guys :)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.