Jump to content

Multiple Inserts In a Table


cyberjaze

Recommended Posts

Hi Everyone,

 

I have a code that stores an ID to a cookie named "cookieuser" and inserts it to a table named "cookieuser" wheneven someone new to my site visited. When someone revisits, it should only access the cookie in the computer and retrieve data in the table based on the cookie. The problem is, the code inserts multiple rows with different IDs (which is autoincremented). The codes works for returning visitors which only reads the cookie ID value. I noticed that the error reacts differently with different browsers: When in localhost, mozilla works as expected, ie inserts 4 rows, and chrome inserts 2 rows. In live server, mozilla creates 4 rows as well. The code I used is shown below. Hope someone knows the problem. Thank you in advance.

 

if (isset($_COOKIE['cookieuser'])) {
   $cookieuserx = $_COOKIE['cookieuser'];
   $sql = "SELECT userid FROM cookieuser WHERE userid = $cookieuserx";
   $res = mysql_query($sql) or die(mysql_error());
   if(mysql_num_rows($res) == 0){
      $date = date("Y-m-d H:i:s",time());
      $sqlcookie = "INSERT INTO cookieuser (userid,lastgood,lastbad,lastneutral,lastvisit,hitcount,date_registered) VALUES('','$date','$date','$date','$date','1','$date')";
      $rescookie = mysql_query($sqlcookie) or die(mysql_error());
      $newcookieuser = mysql_insert_id();
      $expire=time()+60*60*24*360; 
      setcookie("cookieuser", $newcookieuser, $expire,"/");   
      $cookieuser = $newcookieuser;
   }else{
      $cookieuser = $_COOKIE['cookieuser'];
   }
   
}else{
   $date = date("Y-m-d H:i:s",time());
   $sqlcookie = "INSERT INTO cookieuser (userid,lastgood,lastbad,lastneutral,lastvisit,hitcount,date_registered) VALUES('','$date','$date','$date','$date','1','$date')";
   $rescookie = mysql_query($sqlcookie) or die(mysql_error());
   $newcookieuser = mysql_insert_id();
   $expire=time()+60*60*24*360; 
   setcookie("cookieuser", $newcookieuser, $expire,"/");
   $cookieuser = $newcookieuser;
}

Link to comment
https://forums.phpfreaks.com/topic/251175-multiple-inserts-in-a-table/
Share on other sites

Did not check your setcookie code but here is what you want:

<?php
if (isset($_COOKIE['cookieuser'])) 
{
   $cookieuser = $_COOKIE['cookieuser'];
}else{
   $date = date("Y-m-d H:i:s",time());
   $sqlcookie = "INSERT INTO cookieuser (userid,lastgood,lastbad,lastneutral,lastvisit,hitcount,date_registered) VALUES('','$date','$date','$date','$date','1','$date')";
   $rescookie = mysql_query($sqlcookie) or die(mysql_error());
   
   $newcookieuser = mysql_insert_id();
   $expire=time()+60*60*24*360;
   setcookie("cookieuser", $newcookieuser, $expire,"/");
   $cookieuser = $newcookieuser;
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.