2012-11-09 28 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