cyberjaze Posted November 15, 2011 Share Posted November 15, 2011 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; } Quote Link to comment https://forums.phpfreaks.com/topic/251175-multiple-inserts-in-a-table/ Share on other sites More sharing options...
sunfighter Posted November 15, 2011 Share Posted November 15, 2011 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; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/251175-multiple-inserts-in-a-table/#findComment-1288390 Share on other sites More sharing options...
cyberjaze Posted November 16, 2011 Author Share Posted November 16, 2011 Hi Sunfighter, Thank you for the reply. But i didn't see much difference. What's the difference aside from removing the if(mysql_num_rows($res) == 0) part? Quote Link to comment https://forums.phpfreaks.com/topic/251175-multiple-inserts-in-a-table/#findComment-1288576 Share on other sites More sharing options...
cyberjaze Posted November 21, 2011 Author Share Posted November 21, 2011 I think this is such a hard question... Hope someone knows the problem. Quote Link to comment https://forums.phpfreaks.com/topic/251175-multiple-inserts-in-a-table/#findComment-1289882 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.