2016-07-16 14 views
-1

我想做一個陷阱,如果該數組在數據庫中具有相同的值。他們將繼續進行如果我建立的條件。到目前爲止,這是我的代碼。php - 檢查mysql_query是否存在相同的數組變量,然後得到結果

$a = $_POST['a']; 
    $b = $_POST['b']; 
    foreach ($_POST['b'] as $b) 
{    
    $result = mysql_query("select * from ehr_schedule3 where (sched3_time='$b' and sched2_id='$a') "); 

      $row = mysql_fetch_array($result); 
      $num = mysql_num_rows($result); 
} 
      if(strpos($b, $result)==true) //$b is the array value I will insert to the db. 
{ 
      header("Location: ../edit_sched2.php?asdfghjkl=".$a."?false"); 
       $_SESSION["A"] = "E" ; 
} 

我不知道如何在請求mysql_query陣列玩。如果只有一個相同的值,我只想繼續到if。我原本打算做一個字符串,這樣如果strpos已經證實它具有相同的價值,它會接着進行到if

+1

停止使用**棄用,並且截至PHP7刪除** mysql_ *函數。遷移到PDO並開始使用準備好的語句。 –

+0

你爲什麼要檢查?如果某個東西被'SELECT'返回,它已經是**那個'東西'... – FirstOne

+0

我的意思是'$ num'變量抱歉。我會編輯我的問題 – Noobster

回答

0

我想,有一天,你可以使用DISTINCT子句中查詢,這樣你就不會得到重複。順便說一句,你應該使用mysqli,因爲mysql_functions已被棄用。我想澄清意見,所以我可以幫助你,但我沒有50聲望。所以對我的理解,到目前爲止,這是我會做什麼:

$a = $_POST['a']; 
    $b = $_POST['b']; 
    $query = "SELECT schedule3_time, schedule2 FROM ehr_schedule3 WHERE sched3_time LIKE $a and sched2_time LIKE $b " 
    $key = mysqli_connect($host, $user, $passwd, $database) 
    or die(mysql_error); /*your credientials */ 
    foreach ($_POST['b'] as $b) 
     {    
      $result = mysqli_query($key, $query); 

      $row = mysqli_fetch_array($result); 
      $num = mysqli_num_rows($result); 
      } 
      if(strpos($b, $result)==true) { 
      $sql = "INSERT INTO tablename (firstname, lastname, email) 
      VALUES ('John', 'Doe', '[email protected]')"; /* put your table here*/ 

      if (mysqli_query($key, $query)) { 
       echo "New record created successfully"; 
       } else { 
       echo "Error: " . $sql . "<br>" . $conn->error; 
       } 

      $key->close(); 
    } //$b is the array value I will insert to the db. 
{ 
      header("Location: ../edit_sched2.php?asdfghjkl=".$a."?false"); 
       $_SESSION["A"] = "E" ; 
} 

退房this post,以幫助你與你的問題。

相關問題