Jump to content

[SOLVED] Not running my else statement


jmr3460

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/161280-solved-not-running-my-else-statement/
Share on other sites

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.

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.

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

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!

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.