tags:

views:

50

answers:

2

Hi guys,

I'm getting this error when trying to insert data from a form into a database. I know what it means, I just can't figure out why I'm getting it. Perhaps I've been starting at it for too long and have missed something?

Here is my code:

<?php
            $q1 = mysql_escape_string($_POST['q1']);
            $q2 = mysql_escape_string($_POST['q2']);
            $q3 = mysql_escape_string($_POST['q3']);            
            $q4 = mysql_escape_string($_POST['q4']);
            $q5 = mysql_escape_string($_POST['q5']);            
            $q6 = mysql_escape_string($_POST['q6']);
            $q7 = mysql_escape_string($_POST['q7']);
            $q8 = mysql_escape_string($_POST['q8']);
            $q9 = mysql_escape_string($_POST['q9']);
            $q10 = mysql_escape_string($_POST['q10']);
            $q11a = mysql_escape_string($_POST['q11a']);
            $q11b = mysql_escape_string($_POST['q11b']);
            $q11c = mysql_escape_string($_POST['q11c']);
            $q11d = mysql_escape_string($_POST['q11d']);
            $q11e = mysql_escape_string($_POST['q11e']);
            $q11f = mysql_escape_string($_POST['q11f']);
            $q11g = mysql_escape_string($_POST['q11g']);
            $q11h = mysql_escape_string($_POST['q11h']);            
            $q12 = mysql_escape_string($_POST['q12']);
            $q13 = mysql_escape_string($_POST['q13']);
            $q14a = mysql_escape_string($_POST['q14a']);
            $q14b = mysql_escape_string($_POST['q14b']);
            $name = mysql_escape_string($_POST['name']);            
            $email = mysql_escape_string($_POST['email']);


            require_once('connection.php');

            $sql="INSERT INTO survey (Question1, Question2, Question3, Question4, Question5, Question6, Question7, Question8, Question9, Question10, Question11A, Question11B, Question11C, Question11D, Question11E, Question11F, Question11G, Question11H, Question12, Question13, Question14A, Question14B, name, email) VALUES ('$q1', '$q2', '$q3', '$q4', '$q5', '$q6', '$q7', '$q8', '$q9', '$q10', '$q11a','$q11b', '$q11c','$q11d', '$q11e', '$q11f','$q11g','$q11h','$q12', '$q13', '$q14a', '$q14b' '$name', '$email')";

            if (!mysql_query($sql,$conn))
              {
              die('Error: ' . mysql_error());
              }

              mysql_close($conn);

    ?>
+6  A: 

There is a comma missing between:

'$q14b' '$name'

Why do we get this error:

Consider a simple table temp with 2 columns v1 and v2 of type varchar:

mysql> desc temp;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| v1    | varchar(10) | YES  |     | NULL    |       |
| v2    | varchar(10) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

Now lets do 2 inserts:

mysql> insert into temp(v1,v2) values('foo' 'bar');  // missing comma..gives err.
ERROR 1136 (21S01): Column count doesnt match value count at row 1

mysql> insert into temp(v1,v2) values('foo' 'bar','faz'); // missing comma..no er
Query OK, 1 row affected (0.00 sec)

mysql> select * from temp; // 'foobar' got inserted in v1 !!
+--------+------+
| v1     | v2   |
+--------+------+
| foobar | faz  |
+--------+------+
1 row in set (0.00 sec)

From these its very clear that MySQL concatenates two adjacent strings which are separated by space and when this happens the number of arguments you provide after values reduces by 1 and thus its count does not match the number of columns resulting in this error.

codaddict
Now that's a detailed answer
Hammerite
+1 for researching space as a concatenation
Mark Baker
+2  A: 

Missing comma

'$q14b' '$name'

should be

'$q14b', '$name'
Mark Baker