如果语句不起作用
我的代码中有 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
,
CONSTRAINTfk_referals_users1
FOREIGN KEY (users_id
) REFERENCESusers
(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
null
和""
是不同的值,因此如果它等于null
它不会等于""
和 IF将会传递真实。我推荐这个if ( $_POST['referer'] && strlen($_POST['referer']) > 0 )
。这将检查它是否为空(如果未设置则为空)以及是否存在长度大于0的字符串null
and""
are different values, so if it equalsnull
it will not equal""
and the IF will pass true. I recommend thisif ( $_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你的 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.
IF 语句检查是否设置了某个 POST 值。您的外键约束检查 POST 字段中的值是否存在于
users
表中。这两个条件不相关。 POST 字段可以设置为users
表中不存在的值。在尝试插入之前,您应该检查用户表中是否存在该值(也许可以进行 COUNT 操作?)(当然,这取决于您的设置)此外,您应该使用
empty()
作为您的即使值为空,isset()
的 IF 条件也会使您的条件变为 TRUE。更好用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 theusers
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 asisset()
will trip your condition to TRUE even if the value is empty. Better to use