2016-06-21 81 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

請參閱編輯的回覆,讓我知道如果我的心是你的問題? –