PHP MySQL为查询创建条件

时间:2012-11-09 03:56:58

标签: php mysql

我的PHP代码中有这样的代码:

    <?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结果,虽然我选择了Admin或Supervisor,但它总是返回POOL

我正在选择注册表格中的角色。因此,当我选择选项管理员时,它会返回池,当我选择spv时,它会返回池。

任何人都可以给我解决方案吗?

抱歉我的英文不好

3 个答案:

答案 0 :(得分:4)

这是错的,应该是

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

因为=会为您的变量POOL分配值$role,所以请使用==进行比较,或===比较相似的数据类型

答案 1 :(得分:3)

=是作业。 ==是比较。放if($role == "POOL")它应该可以正常工作。

答案 2 :(得分:2)

您需要在if($role = "POOL")中使用==使用if($role == "POOL")

来分配值

=是赋值运算符,==相等运算符。

  1. =是赋值运算符。 b = 1将变量b设置为等于值1。

  2. ==是相等运算符。如果左侧等于右侧,则返回true;如果不相等,则返回false