mysql& PHP错误:列数与第1行的值数不匹配 [英] Mysql & PHP Error: Column count doesn't match value count at row 1

查看:102
本文介绍了mysql& PHP错误:列数与第1行的值数不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试将表单中的数据插入数据库时​​出现此错误.我知道这意味着什么,我只是想不出为什么要得到它.也许我已经开始太久了,错过了什么?

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?

这是我的代码:

<?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);

    ?>

推荐答案

之间缺少逗号:

'$q14b' '$name'


为什么会出现此错误:


Why do we get this error:

考虑一个简单的表temp,该表具有2个列v1v2,类型为varchar:

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)

现在让我们进行2次插入:

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)

很明显, MySQL连接了两个用空格隔开的相邻字符串,并且当这种情况发生时,您在values之后提供的参数数量减少了1,因此它的数量确实不匹配导致此错误的列数.

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.

这篇关于mysql&amp; PHP错误:列数与第1行的值数不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆