Jump to content

Query works - but not the first time the page loads?


mibole

Recommended Posts

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.


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

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.