我正在添加其他用户的页面上工作,当我想插入新用户时,会出现以下内容:
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[21S01]: Insert value list does not match column list:
1136 Column count doesn't match value count at row 1' in /Applications/XAMPP/xamppfiles/htdocs/app/classes/class.users.php:96 Stack trace: #0
/Applications/XAMPP/xamppfiles/htdocs/app/classes/class.users.php(96): PDO->prepare('INSERT INTO use...') #1
/Applications/XAMPP/xamppfiles/htdocs/public/pages/nieuweklant.tpl(4): DeGier\Core\Users::addCus() #2
/Applications/XAMPP/xamppfiles/htdocs/app/classes/class.template.php(20): include('/Applications/X...') #3
/Applications/XAMPP/xamppfiles/htdocs/app/classes/class.template.php(48): DeGier\Core\Template::getPage('nieuweklant') #4
/Applications/XAMPP/xamppfiles/htdocs/index.php(11): DeGier\Core\Template::render('nieuweklant') #5 {main}
thrown in /Applications/XAMPP/xamppfiles/htdocs/app/classes/class.users.php on line 96
通常,当我在准备语句中遗漏了逗号,或者该语句与我的数据库表不对应时,就会弹出此消息,但这次,我相信这些都没有发生。
这是 PDO 声明:
$q = self::$connection->prepare('INSERT INTO users VALUES ("",
:fname, :lname,
:company, :telephone, :email, :adress, :zipcode,
:country, :note)');
$q->execute(array(":fname" => $_POST['voornaam'], ":lname" =>
$_POST['achternaam'], ":company" => $_POST['bedrijf'], ":telephone" =>
$_POST['telefoon'], ":email" => $_POST['email'], ":adress" =>
$_POST['adres'], ":zipcode" => $_POST['postcode'], ":country" =>
$_POST['land'], ":note" => $_POST['aantekening']));
我不认为那里有什么问题。
这是我的数据库表:
再说一次,我相信这一切都是匹配的。
那么,为什么会在没有任何默认原因的情况下出现这么大的错误呢?
谢谢。
最佳答案
您可以尝试在要插入的列中写入内容。由于您的 ID 是自动递增的,因此当您尝试向该字段插入空字符串时可能会出错。
$q = self::$connection->prepare('INSERT INTO users (firstname, lastname, company, phone, email, adress, zipcode, country, note) VALUES ("",
:fname, :lname,
:company, :telephone, :email, :adress, :zipcode,
:country, :note)');
$q->execute(array(":fname" => $_POST['voornaam'], ":lname" =>
$_POST['achternaam'], ":company" => $_POST['bedrijf'], ":telephone" =>
$_POST['telefoon'], ":email" => $_POST['email'], ":adress" =>
$_POST['adres'], ":zipcode" => $_POST['postcode'], ":country" =>
$_POST['land'], ":note" => $_POST['aantekening']));
关于php - 插入值列表与列列表不匹配,语句和DB是否对应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29858820/