2014-09-29 36 views
-2

所以我有表:如何使用IF值等于/不平等的任何值的一列

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。

+1

如果这意味着是检查的唯一标识符,那么可以在您的字段上添加一个唯一约束并/或将其设置为自动递增值。或者,先从表中进行选择,看看您要插入的ID是否已经存在。 – Hammerstein 2014-09-29 14:37:29

+0

那么如何检查值是否等于列中的任何值?现在IF($ check_id!= $ id)是TRUE,其中$ id是8. – Art4k 2014-09-29 14:49:43

+0

*“我需要检查ip_check_id是否已经被使用。”* - 您可以使用['mysqli_num_rows()'](http:/ /php.net/manual/en/mysqli-result.num-rows.php) - 另外,你需要引用你的IP值'WHERE ip_check_address ='$ encode'“);'因为IP地址包含句点/点 – 2014-09-29 15:29:56

回答

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

+0

警告:mysqli_fetch_array()期望至少有1个参数,0在第25行给出:while($ row = mysqli_fetch_array()) – Art4k 2014-09-29 15:49:54

+0

@ Art4k我忘记了查询参数变量,并且在发生冲突时我重命名了一些变量。请重新加载。 – 2014-09-29 15:52:58

+0

仍然同样的问题:/ print_r($ check_if_exist);只是给出相同的$ id = htmlspecialchars($ _ GET [“keywordid”]); – Art4k 2014-09-29 16:07:29

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 
     } 
    } 
+0

不工作:(如果($ check_id!= $ id)是一个TRUE,if($ check_id == $ id)FALSE:/就像if(8!= 8)TRUE和if(9 == 8)FALSE。 – Art4k 2014-09-29 14:57:49

+0

-1您将MySQL API与'mysql_query'和'mysql_fetch_array'混合在一起 - 它们不会**混合。谁首先提出这个问题? – 2014-09-29 15:26:06

+0

我编辑了你的答案并删除了我的-1。请记住'mysql_'和'mysqli_'函数不能混合在一起。 – 2014-09-29 16:49:43