2014-06-05 19 views
-1

我有一個php代碼,更新2個表中的值,我用左連接。它可以工作,但它會繼續跳過第一個條件並始終進入第二個條件。我不知道在mysql注入,所以請指教,如果我的代碼很容易mysql注入。簡單的示例查詢工作更新,但跳過第一個條件

elseif ($_POST['check']) 
{ 
    if ($row[typeofdog] = 'Labrador') 
    { 
      $id = $_POST['data']; 
      $count = count($id); 

     for($i=0;$i<$count;$i++) 
      { 

      $sql = "UPDATE animals LEFT JOIN treats ON animals.style = treats.style SET animals.bone = bone - treats.total, treats.status = 'Approved' WHERE treats.id='$id[$i]'"; 
      $result = mysql_query($sql); 

      } 

     if($result){header("location:login_success.php");} 

    } 
    else 
    { 
     $id = $_POST['data']; 
     $count = count($id); 

     for($i=0;$i<$count;$i++) 
      { 

      $sql = "UPDATE animals LEFT JOIN treats ON animals.style = treats.style SET animals.chunks = chunks - treats.total, treats.status = 'Approved' WHERE treats.id='$id[$i]'"; 
      $result = mysql_query($sql); 

      } 

     if($result){header("location:login_success.php");} 

    } 
} 
+0

你有一個語法錯誤 –

+1

你正在使用=而不是==(邏輯錯誤不是語法之一) –

+0

這裏還有一個語法錯誤。 –

回答

2

首先,=是轉讓。 ==僅供比較。

其次,使用索引typeofdog不帶引號是不正確的。 PHP explains why here.

試試這個:

if ($row['typeofdog'] == 'Labrador') 

如果這不起作用,那麼$row['typeofdog']不等於'Labrador'。在這種情況下,請在條件之前嘗試回顯$row['typeofdog'],以便查看正在比較的內容。

另外,是的,你有冒險的SQL注入。第一步解決這個問題:don't use mysql。相反使用mysqlipdo並利用prepared statements

+0

嘗試過你給你的先生,但它也不工作。 –

+0

@Patrick你在'if'之前''回顯$ row ['typeofdog'];''。你確定它等於'拉布拉多'嗎? –

相關問題