如何使用IF值等于/不等于列中的任何值

时间:2014-09-29 14:34:22

标签: php mysql mysqli

所以我有桌子:

enter image description here

这是代码:`

mysqli_query($con,"INSERT INTO ip_check (ip_check_address, ip_check_id) VALUES ($encode , $id)");
$ip = $_SERVER['REMOTE_ADDR'];
$encode = ip2long($ip);
$id = htmlspecialchars($_GET["keywordid"]);
$check_id = mysqli_query($con,"SELECT ip_check_id FROM ip_check 
WHERE ip_check_address = $encode");
if ($check_id != $id ) {..}

`

我需要检查是否已经使用了ip_check_id。

2 个答案:

答案 0 :(得分:1)

    mysqli_query($con,"INSERT INTO ip_check (ip_check_address, ip_check_id) 
    VALUES ($encode , $id)");

    $ip = $_SERVER['REMOTE_ADDR'];
    $encode = ip2long($ip);
    $id = htmlspecialchars($_GET["keywordid"]);
    $check_id = mysqli_query($con,"SELECT ip_check_id FROM ip_check 
    WHERE ip_check_address = $encode");
    $query = @mysqli_query($check_id);


    while($row = @mysqli_fetch_array())
    {
         $check_id = $row['ip_check_id'];

         if ($check_id== $id ) 
        {

            //here it is equal, returns ture
        }
        else
        {
        // here returns false
        }
    }

答案 1 :(得分:1)

修改

$check_id = mysqli_query($con,"SELECT ip_check_id FROM ip_check 
WHERE ip_check_address = $encode");

while($row = mysqli_fetch_array($check_id))
    {
         $check_if_exist = $row['ip_check_id'];

         if ($check_if_exist != $id ) 
        {

            // it does not equal
        }


    }

Origninal回答,我误解了这个问题。

  

“我需要检查是否已经使用了ip_check_id。”


<强> 旁注:

您现有的代码向SQL injection开放 使用prepared statementsPDO with prepared statements