PHP列数不匹配

时间:2013-09-30 08:07:31

标签: php mysql sql

我正在尝试在PHP中运行SQL查询..使用下面的代码:

$sql="INSERT into members (forename, surname, dob_day, dob_month, dob_year, gender, address, invested, patrol_type, patrol, dr_address, 

parent1_contact1_title, parent1_contact1_forename, parent1_contact1_surname, parent1_contact1_email, parent1_contact1_address, parent1_contact1_home, parent1_contact1_mobile, parent1_contact1_other, 

parent1_contact2_title, parent1_contact2_forename, parent1_contact2_surname, parent1_contact2_email, parent1_contact2_address, parent1_contact2_home, parent2_contact1_mobile, parent1_contact2_other, 

parent2_contact1_title, parent1_contact1_forename, parent1_contact1_surname, parent1_contact1_email, parent1_contact1_address, parent1_contact1_home, parent1_contact1_mobile, parent1_contact1_other, 

parent1_contact2_title, parent1_contact2_forename, parent1_contact2_surname, parent1_contact2_email, parent1_contact2_address, parent1_contact2_home, parent2_contact1_mobile, parent1_contact2_other

) values ('".addslashes($_POST["forename"])."', '".addslashes($_POST["surname"])."', '".addslashes($_POST["dob_day"])."', '".addslashes($_POST["dob_month"])."', '".addslashes($_POST["dob_year"])."', '".addslashes($_POST["gender"])."', '".addslashes($_POST["address"])."', '".addslashes($_POST["invested"])."', '".addslashes($_POST["patrol_type"])."', '".addslashes($_POST["patrol"])."', '".addslashes($_POST["dr_address"])."', 

'".addslashes($_POST["parent1_contact1_title"])."', '".addslashes($_POST["parent1_contact1_forename"])."', '".addslashes($_POST["parent1_contact1_surname"])."', '".addslashes($_POST["parent1_contact1_email"])."', '".addslashes($_POST["parent1_contact1_address"])."', '".addslashes($_POST["parent1_contact1_home"])."', '".addslashes($_POST["parent1_contact1_mobile"])."', '".addslashes($_POST["parent1_contact1_other"])."', 

'".addslashes($_POST["parent1_contact2_title"])."', '".addslashes($_POST["parent1_contact2_forename"])."', '".addslashes($_POST["parent1_contact2_surname"])."', '".addslashes($_POST["parent1_contact2_email"])."', '".addslashes($_POST["parent1_contact2_address"])."', '".addslashes($_POST["parent1_contact2_home"])."', '".addslashes($_POST["parent1_contact2_mobile"])."', '".addslashes($_POST["parent1_contact2_other"])."', 

'".addslashes($_POST["parent2_contact2_title"])."', '".addslashes($_POST["parent1_contact2_forename"])."', '".addslashes($_POST["parent1_contact2_surname"])."', '".addslashes($_POST["parent1_contact2_email"])."', '".addslashes($_POST["parent1_contact2_address"])."', '".addslashes($_POST["parent1_contact2_home"])."', '".addslashes($_POST["parent1_contact2_mobile"])."', '".addslashes($_POST["parent1_contact2_other"])."'

) ";

但我收到的错误是:Column count doesn't match value count at row 1

我无法找到错误的位置 - 有人能找到吗?

5 个答案:

答案 0 :(得分:1)

我将您的查询复制到文本编辑器中。我使用逗号作为分隔符将它分成行,我有43列和35个值 在表格中使用较少的列,并以通用模式拆分记录,如下所示:

  Table students:
    id, forename, surname, details, parent1_id, parent2_id, etc..
  Table parents:
    id (used in table students), name, contact details, etc..

答案 1 :(得分:0)

您的插入查询不止一次包含这些列:

parent1_contact2_title, parent1_contact2_forename, parent1_contact2_surname, parent1_contact2_email, parent1_contact2_address, parent1_contact2_home, parent2_contact1_mobile, parent1_contact2_other

但是不要在查询中出现两次。因此,您指定的列数与您提供的值不同。

答案 2 :(得分:0)

列名中有错误,其中一些是重复的:

$sql="INSERT into members (forename, surname, dob_day, dob_month, dob_year, gender, address, invested, patrol_type, patrol, dr_address, 

parent1_contact1_title, parent1_contact1_forename, parent1_contact1_surname, parent1_contact1_email, parent1_contact1_address, parent1_contact1_home, parent1_contact1_mobile, parent1_contact1_other, 

parent1_contact2_title, parent1_contact2_forename, parent1_contact2_surname, parent1_contact2_email, parent1_contact2_address, parent1_contact2_home, parent2_contact1_mobile, parent1_contact2_other, 

parent2_contact1_title, parent1_contact1_forename, parent1_contact1_surname, parent1_contact1_email, parent1_contact1_address, parent1_contact1_home, parent1_contact1_mobile, parent1_contact1_other, 

parent1_contact2_title, parent1_contact2_forename, parent1_contact2_surname, parent1_contact2_email, parent1_contact2_address, parent1_contact2_home, parent2_contact1_mobile, parent1_contact2_other

) 

例如parent1_contact1_forename重复,将列名修改为相关的

答案 3 :(得分:0)

这是因为表中的列数和表中提供的数值不同。错误表示sql语句中指定的列数与提供的值的数量相匹配。
例如,如果我有一个名为 test 的表格,列如下:
1)。 ID
2)。命名
3)。年龄
4)。性别
现在让我们在此表中创建插入数据

$con = mysqli_connect("host","user","pass","dbName");
if(!$con)
die();
$res = mysqli_query($con,"INSERT INTO test(id,name,age,sex) VALUES(abc,xyz,pqr)");
//the query statement will generate an error similar to your's because the number
//of parameters to be filled does not match the values supplied

答案 4 :(得分:0)

这些是您的数据库字段:

$sql="INSERT into members (
forename, 
surname, 
dob_day, 
dob_month, 
dob_year, 
gender, 
address, 
invested, 
patrol_type, 
patrol, 
dr_address, 
parent1_contact1_title, 
parent1_contact1_forename, 
parent1_contact1_surname, 
parent1_contact1_email, 
parent1_contact1_address, 
parent1_contact1_home, 
parent1_contact1_mobile, 
parent1_contact1_other, 
parent1_contact2_title, 
parent1_contact2_forename, 
parent1_contact2_surname, 
parent1_contact2_email, 
parent1_contact2_address, 
parent1_contact2_home, 
parent2_contact1_mobile, 
parent1_contact2_other, 
parent2_contact1_title, 
parent1_contact1_forename, 
parent1_contact1_surname, 
parent1_contact1_email, 
parent1_contact1_address, 
parent1_contact1_home, 
parent1_contact1_mobile, 
parent1_contact1_other, 
parent1_contact2_title, 
parent1_contact2_forename, 
parent1_contact2_surname, 
parent1_contact2_email, 
parent1_contact2_address, 
parent1_contact2_home, 
parent2_contact1_mobile, 
parent1_contact2_other

这些是您要存储的值:

'".addslashes($_POST["forename"])."', 
 '".addslashes($_POST["surname"])."', 
 '".addslashes($_POST["dob_day"])."', 
 '".addslashes($_POST["dob_month"])."', 
 '".addslashes($_POST["dob_year"])."', 
 '".addslashes($_POST["gender"])."', 
 '".addslashes($_POST["address"])."', 
 '".addslashes($_POST["invested"])."', 
 '".addslashes($_POST["patrol_type"])."', 
 '".addslashes($_POST["patrol"])."', 
 '".addslashes($_POST["dr_address"])."',
 '".addslashes($_POST["parent1_contact1_title"])."', 
 '".addslashes($_POST["parent1_contact1_forename"])."', 
 '".addslashes($_POST["parent1_contact1_surname"])."', 
 '".addslashes($_POST["parent1_contact1_email"])."', 
 '".addslashes($_POST["parent1_contact1_address"])."', 
 '".addslashes($_POST["parent1_contact1_home"])."', 
 '".addslashes($_POST["parent1_contact1_mobile"])."', 
 '".addslashes($_POST["parent1_contact1_other"])."', 
 '".addslashes($_POST["parent1_contact2_title"])."', 
 '".addslashes($_POST["parent1_contact2_forename"])."', 
 '".addslashes($_POST["parent1_contact2_surname"])."', 
 '".addslashes($_POST["parent1_contact2_email"])."', 
 '".addslashes($_POST["parent1_contact2_address"])."', 
 '".addslashes($_POST["parent1_contact2_home"])."', 
 '".addslashes($_POST["parent1_contact2_mobile"])."', 
 '".addslashes($_POST["parent1_contact2_other"])."',
 '".addslashes($_POST["parent2_contact2_title"])."', 
 '".addslashes($_POST["parent1_contact2_forename"])."', 
 '".addslashes($_POST["parent1_contact2_surname"])."', 
 '".addslashes($_POST["parent1_contact2_email"])."', 
 '".addslashes($_POST["parent1_contact2_address"])."', 
 '".addslashes($_POST["parent1_contact2_home"])."', 
 '".addslashes($_POST["parent1_contact2_mobile"])."', 
 '".addslashes($_POST["parent1_contact2_other"])."'

由于存在大量重复数据,您的复制/粘贴已经变得混乱,或者您的数据库结构需要进行审核。