Somenath Sinha Somenath Sinha - 3 months ago 11
PHP Question

Why does the lack of !empty($_GET) generate an error?

My PHP Code is :

<?php

if ((!empty($_POST)) && ($_POST['action'] == 'addRunner'))
{
$fname = htmlspecialchars($_POST['txtFirstName']);
$lname = htmlspecialchars($_POST['txtLastName']);
$gender = htmlspecialchars($_POST['ddlGender']);
$minutes = htmlspecialchars($_POST['txtMinutes']);
$seconds = htmlspecialchars($_POST['txtSeconds']);

if(preg_match('/[^\w\s]/i', $fname) || preg_match('/[^\w\s]/i', $lname))
{
fail('Invalid name provided.');
}
if( empty($fname) || empty($lname) )
{
fail('Please enter a first and last name.');
}
if( empty($gender) )
{
fail('Please select a gender.');
}

$time = $minutes.":".$seconds;
$query = "INSERT INTO runners SET first_name='$fname', last_name='$lname',
gender='$gender', finish_time='$time'";
$result = db_connection($query);
if ($result)
{
$msg = "Runner: ".$fname." ".$lname." added successfully" ;
success($msg);
}
else
{
fail('Insert failed.');
}
exit;
}
elseif($_GET['action'] == 'getRunners')
{

$query = "SELECT first_name, last_name, gender, finish_time FROM runners ORDER BY finish_time ASC";
$result = db_connection($query);

$runners = array();

while($row = mysqli_fetch_array($result))
{
array_push($runners, array('fname' => $row['first_name'], 'lname' => $row['last_name'], 'gender' => $row['gender'], 'time' => $row['finish_time']));
}

echo json_encode(array("runners" => $runners));
exit;
}

function db_connection($query)
{
$dbc = mysqli_connect('127.0.0.1','runner_db_user','runner_db_password','race_info')
or die('Error connecting to Database');

return mysqli_query($dbc,$query);
}

function fail($message)
{
die(json_encode(array('status' => 'fail', 'message' => $message)));
}
function success($message)
{
die(json_encode(array('status' => 'success', 'message' => $message)));
}

?>


NOTE that the original code provided in the book read

if ($_POST['action'] == 'addRunner')


instead of

if ((!empty($_POST)) && ($_POST['action'] == 'addRunner'))


However, the original code generates an error saying


Notice: Undefined index: action in E:\wamp\www\Z_jquery\ch9\service.php on line 3


I'm guessing this is because unless a POST request is generated, the value of $_POST['action'] isn't assigned a value. Is that correct?

But, even though the code doesn't contain a step to check if
$_GET['action']
is empty or not, the code still runs perfectly, even if a POST Request occurs (and presumably
$_GET['action']
is NULL). Why does this happen?

Why does the following work:

elseif($_GET['action'] == 'getRunners')


and, why don't I have to write:

elseif((!empty($_GET)) && ($_GET['action'] == 'getRunners'))



Note that http://localhost/z_jquery/ch9/service.php?action=addRunners generates an empty page, as expected, and no warnings or errors.

Answer

I'm guessing this is because unless a POST request is generated, the value of $_POST['action'] isn't assigned a value. Is that correct?

Yes this is correct. Unless the request is a POST request, the $_POST array is null.


even if a POST Request occurs (and presumably $_GET['action'] is NULL). Why does this happen? Why does the following work: elseif($_GET['action'] == 'getRunners')

This works because if the the request is a POST request it will enter the first if block and not actually run the else if or even attempt to be evaluated by the else if condition as it entered the first if block.


Note that http://localhost/z_jquery/ch9/service.php?action=addRunners generates an empty page, as expected, and no warnings or errors.

That would not generate any warning as your else if is looking for a GET parameter called action. If you ran this, http://localhost/z_jquery/ch9/service.php?mytest=addRunners you will get an undefined index warning as the parameter action doesn't exist in the GET array

Comments