If语句不起作用

时间:2016-06-21 08:32:14

标签: php mysql

我遇到问题,我的if声明不起作用。我希望PHP变量在将数据库插入数据库之前匹配数据库中的字段。不知何故,当HTML输入中有写入bullsh * t并与数据库交叉检查且不匹配时,它仍然将变量保存在数据库中。

我该如何解决这个问题?

使用以下代码:

if(isset($_POST ["SettInnM"])) {
    $ManuellTagnavn = $_POST["TagnavnM"];
    //$ManuellTagnavn = strtoupper($ManuellTagnavn);
    $annexaM = substr($ManuellTagnavn,0,2);
    $annexb1M = substr($ManuellTagnavn,2,-4);
    $sequenceNrM = substr($ManuellTagnavn,4,-1);
    $paralellItem = substr($ManuellTagnavn,7);
    $dato = date("d/m/Y");
    $prosjektID = $_SESSION["prosjekt_id"];

    $sql = "Select plass from annexa where plass = '$annexaM'";
    $res = mysqli_query($db, $sql);

    $query = "Select format from annexb1 where format = '$annexb1M'";
    $res2 = mysqli_query($db, $query);
    echo "$annexaM"; 
    echo "$annexb1M"; 

    if(mysqli_num_rows($res) > 0 AND mysqli_num_rows($res2) > 0 ) {
        //Check if there are any match with one of the fielde in the database? If it matches, save in database
        $sql = "Select prosjekttag, prosjekt_id from tagnavn where prosjekttag = '$ManuellTagnavn' AND prosjekt_id = '$prosjektID'";
        $resultat = mysqli_query($db, $sql);

        if(mysqli_num_rows($resultat) == 0) {
            $sql = "Insert into tagnavn(prosjekttag, startdato, prosjekt_id, opprettav)" //ENDRET AV ZLATAN 
                ."values ('$ManuellTagnavn', '$dato', '$prosjektID', '$userRow[username]')"; //ENDRET AV KAMALAN
            $resultat = mysqli_query($db, $sql);
        } else {
            $resultat = false; 
        }
    }
}

1 个答案:

答案 0 :(得分:0)

if(mysqli_num_rows($res) > 0 AND mysqli_num_rows($res2) > 0 ) //Cehck if there are any match with one of the fielde in the database? If it matches, save in database

这部分代码赢了&#t;"检查是否与数据库中的某个字段匹配",而是因为您使用AND运算符,它将要求两个条件都为真。

if(mysqli_num_rows($resultat) == 0) 

返回结果集http://www.w3schools.com/php/func_mysqli_num_rows.asp

中的行数

在您的任务中添加

  

如果你想同时检查$ res和$ res2,你最好回复它们并查看它们是否返回任何mysqli_result对象。如果您收到错误,那么最好考虑检查查询中的错误。   否则,您可以像往常一样检查这两个条件。即。

if(mysqli_num_rows($res) > 0 && mysqli_num_rows($res2) > 0 )
  

注意:这将要求两个条件都为真。因此,如果您希望至少一个条件为真,请使用 || (或)运营商

相关问题