MySQL 查询因违反完整性约束而失败

发布于 2024-07-15 04:03:52 字数 1919 浏览 3 评论 0原文

该查询一直失败

违反完整性约束: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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

余生共白头 2024-07-22 04:03:52

PDO::query 不支持准备好的语句语法,是吗? 给出 PDO::preparePDOStatement::execute 读取。 您可能想要类似的东西:

$insertUserQuery = 'INSERT INTO `users` (`login_name`, ...) ' .
    'VALUES (:login_name, ...);';
$bindings = array(':login_name' => $loginName, ...);
$stmt = Db::prepare($insertUserQuery);
$stmt->execute($bindings);

您还可以调用 $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:

$insertUserQuery = 'INSERT INTO `users` (`login_name`, ...) ' .
    'VALUES (:login_name, ...);';
$bindings = array(':login_name' => $loginName, ...);
$stmt = Db::prepare($insertUserQuery);
$stmt->execute($bindings);

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文