Jump to content

empty variable check not working properly


geroido

Recommended Posts

Hi

I have a variable passing over from a previous page 'numseats'. If it's empty, I want the form returned to the user, if not do something else. The problem is when I first enter this page, it recognises that the variable is empty and displays the correct code. If I then leave the input box empty again and send the data to this page again,  it thinks there's something in it and goes to the else statement. Can you see what the problem is? As long as the variable is empty, I want it to always do the first part of this code. If the user enters something, only then go to the 'else' part.

 

if (empty($numseats)) {
?>		
  <h3>Please enter the number of seats in your restaurant. numseats is empty</h3>
<form method="post" name="seats" action="updateseats.php">
<TABLE>
<TR><TD><H3>Update seating</H3></TD></TR>
  	<TR><TD>Number of seats</TD></TR>
<TR><TD><textarea rows="2" cols="20"  name="numseats" value=<?echo $numseats?>>

</textarea></TD></TR>

<TD><BR><input id="inputsubmit1" type="submit" name="savemenu" value="Save" /></TD>
<TD><BR><input id="inputsubmit1" type="submit" name="savemenu" value="Finish" /></TD></TR>
</TABLE><?
}
else{
$query = ("select clientID, NumSeats FROM $table where clientID = '".$_SESSION['userid']."'");
$results = mysql_query($query, $link) or die("Error performing query"); 

if(mysql_num_rows($results) > 0){ 

while($row = mysql_fetch_object($results)){ 
//$query = mysql_query("update $table set NumSeats = $numseats where clientID = '".$_SESSION['userid']."'");
echo $row->clientID;
echo ("<BR>");
echo $row->NumSeats;
}
}
else{ 
//$query = mysql_query("insert into $table (clientID, NumSeats) VALUES ('" .$_SESSION['userid']. "', '".$numseats."')");

echo "no records here my friend";
} 
?>
<h3>Please enter the number of seats in your restaurant. Numseats is not empty</h3>
<form method="post" name="seats" action="updateseats.php">
<TABLE>
<TR><TD><H3>Update seating</H3></TD></TR>
  	<TR><TD>Number of seats</TD></TR>
<TR><TD><textarea rows="2" cols="20"  name="numseats" value=<?echo $numseats?>>
  </textarea></TD></TR>
	<TD><BR><input id="inputsubmit1" type="submit" name="updateseats" value="Save" /></TD>
<TD><BR><input id="inputsubmit1" type="submit" name="updateseats" value="Finish" /></TD></TR>
</TABLE><?}?>

Is $numseats from a GET or POST request as you should use $_GET['numseats'] or $_POST['numseats']

Also using the empty() function is not the best approach. If you are expecting the nomber of seats to be a numeric value:

 

$numseats = $_GET['numseats'];
if(!is_numeric($numseats)) {
// back to the form
header("Location:formpage.php");
exit;
}
else {
//process $numseats
}

Hi neil

I've put that code in and it's staying in the right place if the variable is not numeric. However, if I enter a numeric value it still doesn't go to the else statement and is returning the empty form again as if it where not numeric.

 

<?

if(!is_numeric($numseats)) {

?>		
  <h3>Please enter the number of seats in your restaurant. numseats is empty</h3>
<form method="post" name="seats" action="updateseats.php">
<TABLE>
<TR><TD><H3>Update seating</H3></TD></TR>
  	<TR><TD>Number of seats</TD></TR>
<TR><TD><textarea rows="2" cols="20"  name="numseats" value=<?echo $numseats?>>

</textarea></TD></TR>

<TD><BR><input id="inputsubmit1" type="submit" name="savemenu" value="Save" /></TD>
<TD><BR><input id="inputsubmit1" type="submit" name="savemenu" value="Finish" /></TD></TR>
</TABLE><?
}
else{
$query = ("select clientID, NumSeats FROM $table where clientID = '".$_SESSION['userid']."'");
$results = mysql_query($query, $link) or die("Error performing query"); 

if(mysql_num_rows($results) > 0){ 

while($row = mysql_fetch_object($results)){ 
//$query = mysql_query("update $table set NumSeats = $numseats where clientID = '".$_SESSION['userid']."'");
echo $row->clientID;
echo ("<BR>");
echo $row->NumSeats;
}
}
else{ 
//$query = mysql_query("insert into $table (clientID, NumSeats) VALUES ('" .$_SESSION['userid']. "', '".$numseats."')");

echo "no records here my friend";
} 
?>
<h3>Please enter the number of seats in your restaurant. Numseats is not empty</h3>
<form method="post" name="seats" action="updateseats.php">
<TABLE>
<TR><TD><H3>Update seating</H3></TD></TR>
  	<TR><TD>Number of seats</TD></TR>
<TR><TD><textarea rows="2" cols="20"  name="numseats" value=<?echo $numseats?>>
  </textarea></TD></TR>
	<TD><BR><input id="inputsubmit1" type="submit" name="updateseats" value="Save" /></TD>
<TD><BR><input id="inputsubmit1" type="submit" name="updateseats" value="Finish" /></TD></TR>
</TABLE><?}?>

Not sure if this will help, I'm fairly new to php. But I was playing around and this seemed to work for me.

<?php
$numseats = $_POST['numseats'];
if (empty($numseats)) {
?>		
  <h3>Please enter the number of seats in your restaurant. numseats is empty</h3>
<form method="post" name="seats" action="seats.php">
<TABLE>
<TR><TD><H3>Update seating</H3></TD></TR>
  	<TR><TD>Number of seats</TD></TR>
<TR><TD><input name="numseats" type="text" value="<? print "$numseats"; ?>" >

</textarea></TD></TR>

<TD><BR><input type="submit" value="Save" /></TD>
<TD><BR></TD></TR>
</TABLE>
<?
}
else {
print "error";}
?>

On first load it loads the table, if I resubmit without entering it also loads the table and if I enter a value in to the field it goes to the error. I'd switch to a drop down list of numbers though, also I don't understand the point of printing the value of numseats in the text area. It's going to be false and print nothing everytime, If it evaluates to true it jumps to the else section.

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.