2012-11-09 89 views
0

條件我有這樣的代碼在我的PHP代碼:PHP MySQL的創建查詢

<?php 
    require('../../server.php'); 
    $role = strtoupper($_POST['role']); 
    $pool = strtoupper($_POST['pool']); 
    $psh = strtoupper($_POST['comp']); 

    if($role = "POOL") 
    { 
     $query2 = "INSERT INTO m_login (email, password, role, company_id) 
          VALUES ('$email', '$pass', '$role', '$pool')"; 
    } 
    else 
    { 
     $query2 = "INSERT INTO m_login (email, password, role, company_id) 
          VALUES ('$email', '$pass', '$role', '$psh')"; 
    } 

    if (mysql_query($query2)) 
    { 
     $whatdo = strtoupper("add user ").$id; 
     include_once('../../serverlog.php'); 
     $querys = "INSERT INTO m_log (user_id, description, waktu) VALUES ('$user', '$whatdo', '$input')"; 
     if(mysql_query($querys)) 
     { 
      echo'<script>alert("Penambahan data berhasil!");</script> 
      <meta http-equiv="refresh" content="0; url=index.php" />'; 
     } 
     else 
     { 
      echo mysql_error(); 
     } 
    } 
    else 
    { 
     echo'<script>alert("Failed!");</script> <br/>'.mysql_error().'<meta http-equiv="refresh" content="10; url=index.php" />'; 
    } 
?> 

我的問題是,我錯了創建QUERY2條件?因爲當我跑的程序,我的數據總是POOL結果的角色,雖然我有選擇管理或監事時,它總是返回POOL

我使用選擇在登記表中的作用。所以當我選擇選項admin時,它返回池,當我選擇spv時,它返回池。

誰能給我解決?

對不起,我英文不好

+1

'if($ role =「POOL」)'should should'if($ role ==「POOL 「)' – itachi

+0

對不起,沒有閱讀..我明白了。謝謝..:d –

+0

@CrossVander你的代碼是開放的SQL禁令 –

回答

4

這是不對的,應該是

if($role == "POOL") { 
    /*Code goes here*/ 
} 

因爲=將分配值POOL到您的變量$role,所以使用==比較,或===比較類似的數據類型

+0

噢,我的。 ..我忘了這一點..感謝隊友...:d –

+0

@CrossVander歡迎你:) –

3

=是分配。 ==是比較。把if($role == "POOL"),它應該工作正常。

2

照顧你分配在if($role = "POOL")使用價值,而不是==應該if($role == "POOL")

=是賦值運算符,==平等的運營商。

  1. =是賦值運算符。 B = 1將設置變量b等於值1。

  2. ==是等於運算符。如果左側等於右側,則返回true,如果不相等,則返回false