1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

PHP, to store record into Database but check for records first

Discussion in 'PHP & Perl' started by JesterJoker, Sep 1, 2009.

  1. JesterJoker

    JesterJoker Regular Member

    Joined:
    Jan 13, 2008
    Messages:
    237
    Likes Received:
    28
    I'm storing database variables into a table with PHP/mysql

    I can post my table setup and the code im using now for storing them.

    I mostly care about checking the IP I'm storing into the table against IP addresses in the table all ready, if there then don't write the data again, or update the record with updated time, and values.

    I need this setup this way so I can put reports.



    Thanks.

    Jester.
     
  2. cyklotrial

    cyklotrial Regular Member

    Joined:
    Oct 13, 2008
    Messages:
    248
    Likes Received:
    82
    Location:
    Wonderland
    example table:
    ID | Ip | DATE

    PHP:
    $ip=$_SERVER['REMOTE_ADDR'];//check IP
    $sql mysql_connect ('server''user','password')or die(mysql_error();//connect to database
    $baza=mysql_select_db('IP_db')or die(mysql_error());//chose database

    $sql_query mysql_query("SELECT Ip FROM Ip_table WHERE Ip='$ip'")or die(mysql_error());//check IP in database
    $rows=mysql_num_rows($sql_query);//how many find


    if ($rows)//if IP exist in database
            
    {
        
    $sql_query mysql_query("UPDATE Ip SET DATE=CURDATE()' WHERE Ip='$ip'");//update last visit date
            
    mysql_close($sql);//disconnect with database
            
    }
    else
    //if IP does not exist in database
            
    {
            
    $sql_query mysql_query("INSERT INTO Ip_table VALUES ('','$ip',CURDATE())")or die(mysql_error());//save IP in database
            
    mysql_close($sql);//disconnect with database        
            
    }
    I think u want somethink like this.
    Code can have couple bugs (i've didint test it :) )
     
  3. JesterJoker

    JesterJoker Regular Member

    Joined:
    Jan 13, 2008
    Messages:
    237
    Likes Received:
    28
    thanks ill start messing with that.

    I have multiple records with same IP address now and they have an ID per occurance. I will have to work around that with the code but gives me somewhere to start.

    I can post my whole code up if you want to see it

    thanks again..
     
  4. cyklotrial

    cyklotrial Regular Member

    Joined:
    Oct 13, 2008
    Messages:
    248
    Likes Received:
    82
    Location:
    Wonderland
    yeah post your code but not here :) use PM
     
  5. JesterJoker

    JesterJoker Regular Member

    Joined:
    Jan 13, 2008
    Messages:
    237
    Likes Received:
    28
    I messaged you.

    I have an error it writes to Database either way.. so something is getting skipped in checking it,.. also i thought might be an issue with names of mysql query so i just made names for them. i hope that works right.. LOL.. i havnt tried making anything in php for a while