到目前为止,我已经创建了表并获得了成功的消息。但是,当我插入表格时,我得到:将样本记录插入“用户”时出错:列数与第1行的值计数不匹配。
这是创建表格的代码:
mysql_select_db($dbName, $dbConnection);
$sql = "CREATE TABLE ".$dbTable." (ID int(6) auto_increment NOT NULL, Username varchar(20) NOT NULL, Password varchar(20) NOT NULL, UserType int(1) NOT NULL, FirstName varchar(15) NOT NULL, LastName varchar(15) NOT NULL, DOB DATETIME NOT NULL, Phone varchar(15) NOT NULL, Department varchar(15),PRIMARY KEY(ID))";
if(mysql_query($sql, $dbConnection))
{
echo("Table '".$dbTable."' created<br />");
}
else
{
echo("Error creating table '".$dbTable."': ".mysql_error()."<br />");
}
这是插入代码:
$sql = "INSERT INTO ".$dbTable." (Username, Password, UserType, FirstName, LastName, DOB, Department) VALUES (1, 'admin', 'admin', 1, 'Admin', 'Admin', '1900-01-01', '12345656', 'IT')";
if(mysql_query($sql, $dbConnection))
{
echo("Added sample record to '".$dbTable."' table<br />");
}
else
{
echo("Error inserting sample record into '".$dbTable."': ".mysql_error()."<br />");
}
答案 0 :(得分:1)
这一行是问题所在:
$sql = "INSERT INTO ".$dbTable." (Username, //1
Password, //2
UserType, //3
FirstName, //4
LastName, //5
DOB, //6
Department) //7
VALUES (1, //1
'admin', //2
'admin', //3
1, //4
'Admin',//5
'Admin', //6
'1900-01-01', //7
'12345656', //8 You dont have column for this
'IT')"; //9 You dont have column for this
即,列数和值不匹配。您有7列和9个值。所以你需要添加2个列名。还要记住,您不需要为自动递增列提供值
正确的查询可能是这样的:
$sql = "INSERT INTO ".$dbTable." (Username, Password, UserType, FirstName, LastName, DOB, Phone, Department)
VALUES ('admin', 'admin', 1, 'Admin', 'Admin', '1900-01-01', '12345656', 'IT')";
注意我删除了ID,因为它是自动递增的
答案 1 :(得分:0)
您忘记添加电话号码列。插入值的计数应与您在插入查询中指定的列数相匹配。
答案 2 :(得分:0)
您似乎试图将9个值插入7列。在您的插入声明中,您遗漏了Phone
和PRIMARY KEY(ID)
列
答案 3 :(得分:0)
问题出在您的INSERT语句中......
试试这个
$sql = "INSERT INTO ".$dbTable." (Username, Password, UserType, FirstName, LastName, DOB, Phone , Department) VALUES ('admin', 'admin', 1, 'Admin', 'Admin', '1900-01-01', '12345656', 'IT')";
正如错误本身所说,您没有提供确切数量的值,因为您指定的列数量
简而言之。
('col1', 'col2') VALUES ('val','val2')
两者必须是相同的列数
在您的情况下,您忘记添加值12345656
答案 4 :(得分:0)
您不会在values..
中添加ID,并且列
INSERT INTO ".$dbTable." (Username, Password, UserType, FirstName, LastName, DOB, phone, Department) VALUES ('admin', 'admin', 1, 'Admin', 'Admin', '1900-01-01', '12345656', 'IT')";