-2

So i have table:

enter image description here

and here is code: `

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 ) {..}

`

I need to check if ip_check_id is already used.

Art4k
  • 75
  • 8
  • 1
    If that's meant to be a unique identifier for the check, then either put a unique constraint on your field and/or make it an auto-incrementing value. Alternatively, do a select from the table first to see if the id you're about to insert already exists. – tbddeveloper Sep 29 '14 at 14:37
  • So how to like check if value equals any of the values in a column? Right now IF ($check_id != $id ) is TRUE, where $id is 8. – Art4k Sep 29 '14 at 14:49
  • *"I need to check if ip_check_id is already used."* - You can use [`mysqli_num_rows()`](http://php.net/manual/en/mysqli-result.num-rows.php) - Plus, you'll need to quote your IP value `WHERE ip_check_address = '$encode'");` as IP addresses contain periods/dots. – Funk Forty Niner Sep 29 '14 at 15:29
  • mysqli_num_rows just counts how many rows there is in the column. I need to check if $id is equal to any value in the column. And $encode is used to encode ip address – Art4k Sep 29 '14 at 15:38
  • @Art4k OK. Let me edit my answer. Give me a minute. – Funk Forty Niner Sep 29 '14 at 15:39
  • @Art4k I edited my answer below, please reload it to see the changes. – Funk Forty Niner Sep 29 '14 at 15:43
  • I just noticed you accepted my answer, then un-accepted, why? – Funk Forty Niner Aug 10 '15 at 11:02

2 Answers2

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
        }
    }
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Peter Manoukian
  • 158
  • 1
  • 13
  • not working :( if($check_id != $id) is a TRUE and if($check_id == $id) FALSE :/ it's like if(8 != 8) TRUE and if (9 == 8) FALSE. – Art4k Sep 29 '14 at 14:57
  • -1 You're mixing MySQL APIs with `mysql_query` and `mysql_fetch_array` - they do **not** mix. Who upvoted this in the first place? – Funk Forty Niner Sep 29 '14 at 15:26
  • I edited your answer and removed my -1. Remember that `mysql_` and `mysqli_` functions do not mix together. – Funk Forty Niner Sep 29 '14 at 16:49
  • Sorry I did not notice that i in mysqli, merely trying to give the gentleman a working idea, did not focus on givving the accurate code, as you may see merely the approach – Peter Manoukian Sep 30 '14 at 07:02
1

Edit:

$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 answer, I misunderstood the question.

"I need to check if ip_check_id is already used."


Sidenote:

Your present code is open to SQL injection.
Use prepared statements, or PDO with prepared statements.

Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • Warning: mysqli_fetch_array() expects at least 1 parameter, 0 given on line 25: while($row = mysqli_fetch_array()) – Art4k Sep 29 '14 at 15:49
  • @Art4k I forgot the query parameter variable, and I renamed a few variables in case of conflicts. Please reload. – Funk Forty Niner Sep 29 '14 at 15:52
  • Still same problem :/ print_r($check_if_exist); just gives the same as $id = htmlspecialchars($_GET["keywordid"]); – Art4k Sep 29 '14 at 16:07
  • I think the problem may be with `htmlspecialchars` that is mostly used for viewing purposes and not for possible SQL prevention, is that what you want to use `htmlspecialchars` for? @Art4k Try removing it `$id = $_GET["keywordid"];` – Funk Forty Niner Sep 29 '14 at 16:17
  • $id = htmlspecialchars($_GET["keywordid"]); working good with sql(tested), i need to check if $id already in ip_check_id column(see picture above) like if user come to page.php?id=2 and $id is 2(because $_GET["keywordid"]). Then sql checks if there is no ip with the same id and counts as unique visitor. – Art4k Sep 29 '14 at 16:23
  • @Art4k Is `page.php?id=2` what you're actually using or is that just an example? I tend to think that you are using `page.php?keywordid=2` correct? I tested my original answer and it worked. At this point, am under the impression you want to check if one column with a certain number matches another inside another column. – Funk Forty Niner Sep 29 '14 at 16:29
  • yes i'm using keywordid. You see there is numbers in column eg(8,8,9,9,10) when i'm coming from page.php?keywordid=8 SQL need to check IF (column numbers != keywordid) – Art4k Sep 29 '14 at 16:34
  • @Art4k Just do the reverse of `if($check_if_exist == $id )` and do `if($check_if_exist != $id )` – Funk Forty Niner Sep 29 '14 at 16:36
  • $check_if_exist gives the same value as page.php?id= . Is there is any command to call ip_check_id column to print rows like 8,8,9,9,10? I have an idea. – Art4k Sep 29 '14 at 16:41
  • @Art4k Sorry, at this point, I don't know what to suggest. If you want to print the rows, just do `echo $row['column_you_want_to_echo'];` in the loop. – Funk Forty Niner Sep 29 '14 at 16:48
  • how to comma-separate them? – Art4k Sep 29 '14 at 17:02
  • Modify your query to be `SELECT ip_check_id, GROUP_CONCAT(the_column_you_want) FROM ip_check` @Art4k – Funk Forty Niner Sep 29 '14 at 17:07