简体   繁体   English

如果声明PHP

[英]If statement PHP

In this code, the first and second controls don't work! 在此代码中,第一个和第二个控件不起作用!

My idea is that if one of the fiels is empty, there's an error. 我的想法是,如果其中一个字段为空,则会出现错误。 In other word, all the fields are expect. 换句话说,所有字段都是期望的。

if(!empty($nome) && !empty($cognome) == 0)
    header("location: ./content_reg_comeunprodigio_no.html");
else if($dati_pers == 'n')
    header("location: ./content_reg_comeunprodigio_trattamentodati.html");
else
{
    $query = "INSERT INTO persons_comeunprodigio (nome, cognome)
                  VALUES ('$nome', '$cognome')";

    mysqli_query($db, $query) or die(mysqli_error());

    header("location: ./content_reg_comeunprodigio_ok.html");
}

Thank u in advance! 预先谢谢你!

Change the if to 将if更改为

if(empty($nome) || empty($cognome) { if(empty($ nome)|| empty($ cognome){

} }

if you are testing for empty fields. 如果要测试空白字段。 If the fields are not empty, !empty() will return true in either case (>0) and you are expecting an equality of 0, which I think is the opposite of your intent. 如果字段不为空,则!empty()在两种情况下(> 0)都将返回true,并且您期望等于0,我认为这与您的意图相反。

You do not give enough information about $dati_pers to analyze. 您没有提供足够的有关$ dati_pers的信息来进行分析。

Best I can tell, your comparison !empty($nome) && !empty($cognome) == 0 is the culprit. 据我所知,您的比较!empty($nome) && !empty($cognome) == 0是元凶。 Because !empty($cognome) will only == 0 if $cognome is not empty... probably not what you want. 因为!empty($cognome)仅在$cognome不为空时== 0 ,可能不是您想要的。

You might try replacing your if/else if/else block with two if blocks. 您可以尝试用两个if块替换if / else if / else块。 Added die(); 增加了die(); after the header, so PHP processing stops. 在标头之后,因此PHP处理停止。 You can keep your if/else if/else block as the commenter suggests if you like. 您可以按照评论者的建议保留if / else if / else块。

if( empty($nome) || empty($cognome) ){
    // $nome or $cognome is empty
    header("location: ./content_reg_comeunprodigio_no.html");
    die();
}
if($dati_pers == 'n') {
    header("location: ./content_reg_comeunprodigio_trattamentodati.html");
    die();
}

$query = "INSERT INTO persons_comeunprodigio (nome, cognome)
              VALUES ('$nome', '$cognome')";

mysqli_query($db, $query) or die(mysqli_error());

header("location: ./content_reg_comeunprodigio_ok.html");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM