MySQL 查询因违反完整性约束而失败
该查询一直失败
违反完整性约束:1048 列“login_name”不能为空
我的插入语句是...
$insertUserQuery = 'INSERT INTO `users` (
`login_name`,
`password`,
`first_name`,
`last_name`,
`company_name`,
`company_address`,
`country`,
`email`,
`phone_number`,
`agency_type`,
`sold_before`,
`authorised`,
`current_module`
)
VALUES (
:login_name, :login_password, :first_name, :last_name, :company_name, :company_address, :country, :email, :phone_number, :agency_type, :sold_before, 0, 0);';
$bindings = array(':login_name' => $loginName,
':login_password' => sha1($password . Config::PASSWORD_SALT),
':first_name' => $firstName,
':last_name' => $lastName,
':company_name' => $companyName,
':company_address' => $companyAddress,
':country' => $country,
':email' => $emailAddress,
':phone_number' => $phone,
':agency_type' => null,
':sold_before' => null
);
print_r($bindings);
Db::query($insertUserQuery, $bindings);
我的数据库类可以找到 另一个问题。 print_r() 告诉我该数组肯定有一个值。
这可能与我使用“密码”这个词有关,它也是一个 MySQL 函数?
PDO 是否以与 SELECT 相同的方式支持 INSERT 准备好的语句?
我是否需要引用这些值,例如“:login_name”?
This query keeps failing with
Integrity constraint violation: 1048 Column 'login_name' cannot be null
My insert statement is...
$insertUserQuery = 'INSERT INTO `users` (
`login_name`,
`password`,
`first_name`,
`last_name`,
`company_name`,
`company_address`,
`country`,
`email`,
`phone_number`,
`agency_type`,
`sold_before`,
`authorised`,
`current_module`
)
VALUES (
:login_name, :login_password, :first_name, :last_name, :company_name, :company_address, :country, :email, :phone_number, :agency_type, :sold_before, 0, 0);';
$bindings = array(':login_name' => $loginName,
':login_password' => sha1($password . Config::PASSWORD_SALT),
':first_name' => $firstName,
':last_name' => $lastName,
':company_name' => $companyName,
':company_address' => $companyAddress,
':country' => $country,
':email' => $emailAddress,
':phone_number' => $phone,
':agency_type' => null,
':sold_before' => null
);
print_r($bindings);
Db::query($insertUserQuery, $bindings);
My database class can be found at another question. The print_r()
tells me that the array definitely has a value.
May it have something to do with me using the word 'password' which is also a MySQL function?
Does PDO support prepared statements with INSERT in the same fashion as it does with SELECT?
Do I need to quote around the values, example ':login_name'?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
PDO::query
不支持准备好的语句语法,是吗? 给出 PDO::prepare 和 PDOStatement::execute 读取。 您可能想要类似的东西:您还可以调用
$stmt->bindValue()
而不是构建绑定数组。 我认为显式绑定每个值会更好一些,因为您可以在那里验证类型。编辑:抱歉 jcinacio,直到我发布后我才发现您的评论几乎相同。
PDO::query
doesn't support prepared statement syntax does it? Give PDO::prepare and PDOStatement::execute a read. You probably want something like:You can also call
$stmt->bindValue()
instead of building an array of bindings. I think that explicitly binding each value is a little nicer since you can verify the types right there.EDIT: sorry jcinacio, I didn't see that your comment was almost identical until after I posted.