jmr3460 Posted June 7, 2009 Share Posted June 7, 2009 Why is this script not running the else statement? <?php $host = "localhost"; $user = "user"; $pass = "pass"; $database = "counter"; $ip = getenv('REMOTE_ADDR'); $date = date("M-d-Y h:i:s", time()+3600); if (!isset($_COOKIE['tuesday'])) { setcookie("tuesday","baseball",time()+7200,"/"); mysql_connect($host, $user, $pass); mysql_select_db($database) or die(mysql_error()); mysql_query("INSERT INTO comments values('','$date','$ip','','')") or die(mysql_error()); $data = mysql_query("SELECT * FROM comments WHERE id = LAST_INSERT_ID()"); $info = mysql_fetch_array($data); echo $info['id']; } else { mysql_connect($host, $user, $pass); mysql_select_db($database) or die(mysql_error()); mysql_query("INSERT INTO comments values('','$date','$ip','','')") or die(mysql_error()); $data = mysql_query("SELECT * FROM comments WHERE id = LAST_INSERT_ID()"); $info = mysql_fetch_array($data); return $info['id']; } ?> Please help? Quote Link to comment https://forums.phpfreaks.com/topic/161280-solved-not-running-my-else-statement/ Share on other sites More sharing options...
Daniel0 Posted June 7, 2009 Share Posted June 7, 2009 Because !isset($_COOKIE['tuesday']) evaluates to TRUE. Quote Link to comment https://forums.phpfreaks.com/topic/161280-solved-not-running-my-else-statement/#findComment-851043 Share on other sites More sharing options...
XaeroDegreaz Posted June 7, 2009 Share Posted June 7, 2009 Perhaps the tuesday cookie has already been set once and still resides in your cookies (perhaps from another testing of the script?) Try clearing your cookies. Quote Link to comment https://forums.phpfreaks.com/topic/161280-solved-not-running-my-else-statement/#findComment-851045 Share on other sites More sharing options...
jmr3460 Posted June 7, 2009 Author Share Posted June 7, 2009 I am sorry I should have explained better. Yes the first time I run the script I get the id number of the last insert id. This part works great. Basically if cookie is not set I want to set cookie then insert data and retrieve last insert id , but if cookie is set I want to just get the last insert id from the table. Quote Link to comment https://forums.phpfreaks.com/topic/161280-solved-not-running-my-else-statement/#findComment-851052 Share on other sites More sharing options...
Ken2k7 Posted June 7, 2009 Share Posted June 7, 2009 Is the code you posted inside a function? What's the purpose of return in your else block? Quote Link to comment https://forums.phpfreaks.com/topic/161280-solved-not-running-my-else-statement/#findComment-851056 Share on other sites More sharing options...
jmr3460 Posted June 7, 2009 Author Share Posted June 7, 2009 No it could be though. I hope to use it as a counter. If the visitor is visiting for the first time(depending on how long I set the cookie time for) I want to set a cookie, insert data and retrieve the LAST_INSERT_ID id from the table (This works after ken2k7 explained to me last night), else I just want to retrieve the LAST_INSERT_ID of the table. I also am planning to use the DB table to report ip's and times of visits in log.txt file. Quote Link to comment https://forums.phpfreaks.com/topic/161280-solved-not-running-my-else-statement/#findComment-851064 Share on other sites More sharing options...
Ken2k7 Posted June 7, 2009 Share Posted June 7, 2009 Wait, then why are you also INSERTing data in your else case? Quote Link to comment https://forums.phpfreaks.com/topic/161280-solved-not-running-my-else-statement/#findComment-851066 Share on other sites More sharing options...
jmr3460 Posted June 7, 2009 Author Share Posted June 7, 2009 I am sorry I have been trying to find out if the else statement is even running I added that line to see if it would add data and it doesn't. I have taken it out now here is the new code: <?php $host = "localhost"; $user = "user"; $pass = "pass"; $database = "counter"; $ip = getenv('REMOTE_ADDR'); $date = date("M-d-Y h:i:s", time()+3600); if (!isset($_COOKIE['tuesday'])) { setcookie("tuesday","baseball",time()+7200,"/"); mysql_connect($host, $user, $pass); mysql_select_db($database) or die(mysql_error()); mysql_query("INSERT INTO comments values('','$date','$ip','','')") or die(mysql_error()); $data = mysql_query("SELECT * FROM comments WHERE id = LAST_INSERT_ID()"); $info = mysql_fetch_array($data); echo $info['id']; } else { mysql_connect($host, $user, $pass); mysql_select_db($database) or die(mysql_error()); $data = mysql_query("SELECT * FROM comments WHERE id = LAST_INSERT_ID()"); $info = mysql_fetch_array($data); return $info['id']; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/161280-solved-not-running-my-else-statement/#findComment-851067 Share on other sites More sharing options...
Ken2k7 Posted June 7, 2009 Share Posted June 7, 2009 Try SELECT * FROM comments ORDER BY id DESC LIMIT 1; as the SQL for the else case. Quote Link to comment https://forums.phpfreaks.com/topic/161280-solved-not-running-my-else-statement/#findComment-851071 Share on other sites More sharing options...
jmr3460 Posted June 7, 2009 Author Share Posted June 7, 2009 WOW Ken2k7 that did it. I think I have a counter with your help. I have tried to do the search thing for about a month or so. and all my looking and trying to make other scripts do what I needed gave me enough knowledge to just try and make one of my own. I am truly grateful for your help. here is what I have got: <?php $host = "localhost"; $user = "user"; $pass = "pass"; $database = "counter"; $ip = getenv('REMOTE_ADDR'); $date = date("M-d-Y h:i:s", time()+3600); if (!isset($_COOKIE['February'])) { setcookie("February","baseball",time()+7200,"/"); mysql_connect($host, $user, $pass); mysql_select_db($database) or die(mysql_error()); mysql_query("INSERT INTO comments values('','$date','$ip','','')") or die(mysql_error()); $data = mysql_query("SELECT * FROM comments WHERE id = LAST_INSERT_ID()"); $info = mysql_fetch_array($data); echo $info['id']; } else { mysql_connect($host, $user, $pass); mysql_select_db($database) or die(mysql_error()); $data = mysql_query("SELECT id FROM comments ORDER BY id DESC LIMIT 1;"); $info = mysql_fetch_array($data); echo $info['id']; } ?> Now all I have to do now is to find out how to either make it a function so I can add to some pages. This is my next lesson I guess. Thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/161280-solved-not-running-my-else-statement/#findComment-851073 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.