mibole Posted September 28, 2007 Share Posted September 28, 2007 Hi I have an index.php file that looks like this: <?php $p = $_GET['p']; if(is_null($p)) { $p = 0; } include('connect.php'); //connects to database $IP = $HTTP_SERVER_VARS['REMOTE_ADDR']; $query = "INSERT INTO `visits` (`ip`, `page`) VALUES ('$IP', '$p')"; $result = mysql_query($query); mysql_close($connect); //Page redirection-------------------- if($p == 1 || $p == 0) { include('main.php'); } if($p == 2) { include('folio.php'); } ?> For some reason, the first time the page loads (where it should register p=0), it doesn't insert the data into the database. If I hit reload (F5), it does insert the data (with p=0). Has anyone an idea why this is happening? Any response is appreciated. Thanks. Link to comment https://forums.phpfreaks.com/topic/71012-query-works-but-not-the-first-time-the-page-loads/ Share on other sites More sharing options...
HuggieBear Posted September 28, 2007 Share Posted September 28, 2007 Change $p = $_GET['p']; if(is_null($p)) { $p = 0; } To $p = (isset($_GET['p'])) ? $_GET['p'] : 0; The reason your code doesn't work is because $p isn't null. It's assigned the value of $_GET['p']. Although $_GET['p'] may well have been empty, $p has still been assigned it's 'emptyness' hence not null. Regards Huggie Link to comment https://forums.phpfreaks.com/topic/71012-query-works-but-not-the-first-time-the-page-loads/#findComment-357063 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.