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
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;
}
?>

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.