如果语句不起作用

发布于 2024-10-26 17:12:36 字数 1348 浏览 3 评论 0原文

我的代码中有 2 个 sql 查询,其中第二个查询包含在 if 语句中,该语句仅在某个 post 值存在时才执行。

但我仍然收到以下错误消息:

SQLSTATE[23000]:完整性约束 违规:1452 无法添加或更新 子行:外键约束 失败(数据库1表2, 约束fk_referals_users1 外键 (users_id) 参考 users (id) ON 删除 无操作 更新无操作)

显然,if 语句不起作用,如果起作用,则不会执行第二个 sql 查询。

这是脚本的问题部分:

$STH = $DBH -> prepare( "insert into database1.table1 (display_name, email, password) values ( :display_name, :email, :password )" );

$STH -> bindParam( ':display_name', $_POST['display_name'], PDO::PARAM_STR, 100 );
$STH -> bindParam( ':email', $_POST['email'], PDO::PARAM_STR, 100 );
$STH -> bindParam( ':password', $_POST['password'], PDO::PARAM_STR, 100 );

$STH -> execute();

if( isset( $_POST['referer'] ) or ( $_POST['referer'] != null ) or ( $_POST['referer'] != "" ) ) {
    $STH = $DBH -> prepare( "insert into database1.table2 ( username, status, users_id ) values ( :username, :status, :users_id )" );

    $strStatus = 1;

    $STH -> bindParam( ':username', $_POST['display_name'], PDO::PARAM_STR, 100 );
    $STH -> bindParam( ':status', $strStatus, PDO::PARAM_INT, 1 );
    $STH -> bindParam( ':users_id', $_POST['referer'], PDO::PARAM_INT, 1 );

    $STH -> execute();
}

I have 2 sql queries in my code where the second one is wrapped around an if statement which should only execute if a certain post value exists.

But I still get the following error message:

SQLSTATE[23000]: Integrity constraint
violation: 1452 Cannot add or update a
child row: a foreign key constraint
fails (database1.table2,
CONSTRAINT fk_referals_users1
FOREIGN KEY (users_id) REFERENCES
users (id) ON DELETE NO ACTION ON
UPDATE NO ACTION)

Clearly that if statement is not working, if it was, it would not get to the second sql query.

Here is the problem section of the script:

$STH = $DBH -> prepare( "insert into database1.table1 (display_name, email, password) values ( :display_name, :email, :password )" );

$STH -> bindParam( ':display_name', $_POST['display_name'], PDO::PARAM_STR, 100 );
$STH -> bindParam( ':email', $_POST['email'], PDO::PARAM_STR, 100 );
$STH -> bindParam( ':password', $_POST['password'], PDO::PARAM_STR, 100 );

$STH -> execute();

if( isset( $_POST['referer'] ) or ( $_POST['referer'] != null ) or ( $_POST['referer'] != "" ) ) {
    $STH = $DBH -> prepare( "insert into database1.table2 ( username, status, users_id ) values ( :username, :status, :users_id )" );

    $strStatus = 1;

    $STH -> bindParam( ':username', $_POST['display_name'], PDO::PARAM_STR, 100 );
    $STH -> bindParam( ':status', $strStatus, PDO::PARAM_INT, 1 );
    $STH -> bindParam( ':users_id', $_POST['referer'], PDO::PARAM_INT, 1 );

    $STH -> execute();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

最好是你 2024-11-02 17:12:36

null"" 是不同的值,因此如果它等于 null 它不会等于 "" 和 IF将会传递真实。我推荐这个 if ( $_POST['referer'] && strlen($_POST['referer']) > 0 )。这将检查它是否为空(如果未设置则为空)以及是否存在长度大于0的字符串

null and "" are different values, so if it equals null it will not equal "" and the IF will pass true. I recommend this if ( $_POST['referer'] && strlen($_POST['referer']) > 0 ). This will check if it is null or not (if it is not set it is null) and if there is a string with a length greater than 0

陌伤ぢ 2024-11-02 17:12:36

你的 if 条件之一是解析为 true。

将复合 if 语句分成 3 个嵌套的 if,并在每个级别添加回显以查看哪些语句失败

One of your if conditions is resolving to true.

Break your compound if statement into 3 nested ifs and add an echo at each level to see which one(s) are failing.

橪书 2024-11-02 17:12:36

IF 语句检查是否设置了某个 POST 值。您的外键约束检查 POST 字段中的值是否存在于 users 表中。这两个条件不相关。 POST 字段可以设置为 users 表中不存在的值。在尝试插入之前,您应该检查用户表中是否存在该值(也许可以进行 COUNT 操作?)(当然,这取决于您的设置)

此外,您应该使用 empty() 作为您的即使值为空,isset() 的 IF 条件也会使您的条件变为 TRUE。更好用

if (!empty($_POST['referer'])) {
   //your code here
}

The IF statement checks whether a certain POST value is set. Your foreign key constraint checks whether the value in the POST field is present in the users table. These two conditions are not related. The POST field can be set with a value that is not present in the users table. You should check whether the value is present in the users table (do a COUNT maybe?) before trying to insert (this depends on your setup, of course)

Also, you should use empty() for your IF condition as isset() will trip your condition to TRUE even if the value is empty. Better to use

if (!empty($_POST['referer'])) {
   //your code here
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文