Jump to content
Larry Ullman's Book Forums

Recommended Posts

Have a select situated like so:

 

$number = range(0,5);

<select name="amount">

foreach ($number as $key => $value) {
   echo  '<option value="' . $key . '">' . $value . '</option>';
}

</select>

 

The handling script contains:

 

if (!empty($_POST['amount'])) {

	$a = mysqli_real_escape_string($dbc, $_POST['amount']);

	$b = (integer)$a;

} else {

	$b = false;
}

 

It works swimmingly enough, until a '0' $key-value buggers the handling script.

 

The script swears on a stack of manuals it's being fed something other than the integer 'zero'.

 

How does one get '0' (zero) to be - and stay as- the *number* zero, and not a NULL or FALSE, etc.

 

 

 ~ David

Share this post


Link to post
Share on other sites

I don't see anything in your code that does what you say it does ("it's being fed something other than the integer 'zero'.") Perhaps if you could explain what you mean by that in terms of actual code and/or errors.

 

That being said, there's no reason why you should send the amount through mysqli_real_escape_string() at all. 

Share this post


Link to post
Share on other sites

$_POST['amount] is a string. The empty() function will return true if the variable is an empty string, false, array(), NULL,  0, or an unset variable. Instead of

if (!empty($_POST['amount'])) {

try

if (isset($_POST['amount'])) {

 

Share this post


Link to post
Share on other sites

Perhaps if you could explain what you mean . . . in terms of actual code and/or errors.

 

No error is shown, instead, the php code merely stops executing.

 

The query itself runs just fine on the database. I can put real, live zero's in their respective columns

until the hens come home, so the query is proven sound. In addition, any number between 1 and 5

works just fine when run through the php script.

 

However, when a 'zero' gets selected in the drop-down and runs through the php script, it (the script)

wonks out right at the point where it should be sending the query. The 'else' error displays instead.

Seems the script won't recognize 'zero' as an integer, even though it's been specifically cast as one.

 

I try to avoid cluttering the place up with buckets of code, but if it helps clarify, here's an abbreviated set:

 

if ($_SERVER['REQUEST_METHOD'] == 'POST') {

	if (isset($_POST['amount'])) {

		$a = $_POST['amount'];

		$b = (integer)$a;

	} else {

		$b = false;
	}

	if ($ {

		$query = "Make me handsome and rich!";

	} else {

		echo '<p>Oops, not this time, Sorry!</p>';

	}

} // end main submit

// main page

$number = range(0,5);

echo '<select name="amount">';

foreach ($number as $key => $value) {
   echo  '<option value="' . $key . '">' . $value . '</option>';
}

echo '</select>';

 

 

That being said, there's no reason why you should send the amount through mysqli_real_escape_string() at all.

 

Good point. My 'copy and paste' habits betray me here.

 

 

 ~ David

Share this post


Link to post
Share on other sites

 

try

if (isset($_POST['amount'])) {

 

Gave that a try and doggone if she still don't stall out just before sending the query.

 

I did, however, update the sample code above (or below or wherever it is in relation to this post) so it reflects your suggestion.

 

 ~ David

Share this post


Link to post
Share on other sites

When b is equal to zero the conditional

if ($

interprets that as boolean false. You want to use the function is_int(). Or better still look into the filter_var functions which gives you the option of specifying a range of valid values.

Share this post


Link to post
Share on other sites

Yes, as margaux said, both 0 and false are interpreted as "falsey" values when you use "if ($b)". There are any number of solutions, some of which margaux and I have proposed above.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...