Jump to content

mysql insert problem :/


lukerodham

Recommended Posts

Hey guys for some reason this code is not working i can't see a problem myself could someone please have a look and point the issue out to me. what i mean by it not working is it won't insert into the database or show a mysql_error.

 

thanks in advance :)

 

$guestip = $_SERVER['REMOTE_ADDR'];
$time = date('G:i');
$date = date("y-m-d");

$query = mysql_query("SELECT * FROM IP_Address") or die(mysql_error());
while($row = mysql_fetch_assoc($query)){
    
    if($guestip != $row['ip']){
        //insert into db.
        mysql_query("INSERT INTO IP_Address(id, ip, date, time) VALUES(NULL,'$questip','$date','$time')") or die(mysql_error());
        echo "inserted in to database"; echo mysql_error();
    }else{
        // add hit count and update time and date.
        echo "already in db";
    }
}

Link to comment
Share on other sites

There's a substantial fault in the logic of what you are doing here.

 

Does your IP_Address table have anything in it?

 

Consider what happens when IP_Address is empty --  will you ever do an insert?

 

This is not the way to solve this problem -- looking at every row in the table, to determine whether or not there is a row with the same IP address in there is a really bad idea. I'm not sure what the purpose of this table is, but you can determine in advance whether or not there is already a row in the table by using a WHERE clause.

Link to comment
Share on other sites

ok well what i was aiming for was to create a unique ip database, for a stats page and with the WHERE clause would i then just use the mysql_num_row function to check if there is already an entry for the $guestip. if you understand what i mean.

Link to comment
Share on other sites

Yes you can do that, OR you can do SELECT count(*) from IP_Address where ip = ...

 

One other thing -- you're needlessly wasting storage.  You should use an integer in the database and convert the ip using [m]ip2long[/p]. 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.