I came across a PHP question paper which provided a code sample associated with the functionality of a product cart. When I went through the code sample, I came across certain PHP if conditions which checks, if a variable to which a $_POST[key] value was assigned, is false. The following are the code samples associated with problem I am facing.
nicepage.php
$id=$_GET['previd'];
$SQL = "select * from pro where prId=".$id;
$runSQL = mysql_query($SQL) or die(mysql_error());
$details = mysql_fetch_array($runSQL);
echo "<p>Catalogue Item Number: ".$details['prId'];
echo "<p>". strtoupper($details['prName']);
echo "<p>".$details['prDescrip'];
echo "<p><img src=images/".$details['prPicName'].">";
echo "<p>£".$details['prPrice'];
echo "<p>".$details['prQuantity']." items for you to get!" ;
echo "<form method=post action=greatpage.php>" ;
echo "<p>Enter how many: ";
echo "<input type=text name=newqu size=5 maxlength=3>";
echo "<input type=submit value='Get them'>";
echo "<input type=hidden name=newid value=".$id.">";
echo "</form>" ;
echo "</center>";
echo "</body>";
echo "</html>";
greatpage.php
$theid=$_POST['newid'];
$thequ=$_POST['newqu'];
if (!$theid)
{
echo "<p>Nothing new is added, show the stuff from before";
}
else
{
if (!$thequ or $thequ==0)
{
echo "<p>Error!";
echo "<p><a href=nicepage.php>Enter correct value!</a>";
exit;
}
else
{
$theSQL="select prQuantity from pro where prId=".$theid;
$runtheSQL=mysql_query($theSQL) or die (mysql_error());
$info=mySQL_fetch_array($runtheSQL);
$ourqu=$info['prQuantity'];
if ($thequ > $ourqu)
{
echo "<p>Not good!";
echo "<p><a href=nicepage.php> Do it again!</a>";
exit;
}
else
{
echo "<p>Great, item added!";
$_SESSION['storage'][$theid]=$thequ;
}
}
}
The above code samples provide parts of the PHP scripts associated with the issue.
I am a relatively new PHP programmer, hence pardon me for any mistakes, I make.
I would like to learn why the $theid
and $thequ
are checked if they are equal to false rather than using a function like isset($_POST['key'])
to check if $_POST
global variables are set before assigning to the two PHP $theid
and $thequ
variables.
And in the above code sample, which situations may lead to the !$theid
and !$thequ
being true?
I would be extremely grateful to anyone who can solve this conundrum.