2016-06-21 139 views
1

我遇到问题,其中我的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; 
     } 
    } 
} 
+0

$ query =“从annexb1中选择格式,其中format ='”。$ annexb1M。“'”;写这样的查询 –

回答

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 

你的代码不会“与数据库中的一个字段任何匹配检查”,这部分相当,因为你使用的是AND运营商将要求两个条件都是真实的。

if(mysqli_num_rows($resultat) == 0) 

返回结果中的行数设置http://www.w3schools.com/php/func_mysqli_num_rows.asp

除了你的追求

,如果你想同时检查$资源和$ RES2这是为了更好的你可以回显他们两个,看看他们是否返回任何mysqli_result对象。如果您失误,那么您最好考虑检查查询中的错误。 否则,您可以继续并检查两项条件。即

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

注:这将需要两个条件是真实的。所以如果你想至少有一个条件是真的使用|| (OR)运营商

+0

你有什么建议,我可以用数据库中的值进行交叉检查,如果匹配在IF语句中进行? –

+0

请参阅编辑的回复,让我知道如果我的心是你的问题? –