David Posted March 29, 2013 Share Posted March 29, 2013 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 Link to comment Share on other sites More sharing options...
Larry Posted March 29, 2013 Share Posted March 29, 2013 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. Link to comment Share on other sites More sharing options...
margaux Posted March 30, 2013 Share Posted March 30, 2013 $_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'])) { Link to comment Share on other sites More sharing options...
David Posted March 31, 2013 Author Share Posted March 31, 2013 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 Link to comment Share on other sites More sharing options...
David Posted March 31, 2013 Author Share Posted March 31, 2013 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 Link to comment Share on other sites More sharing options...
HartleySan Posted March 31, 2013 Share Posted March 31, 2013 Change "if ($b)" to "if ($b !== false)". That should work. 1 Link to comment Share on other sites More sharing options...
margaux Posted March 31, 2013 Share Posted March 31, 2013 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. Link to comment Share on other sites More sharing options...
HartleySan Posted March 31, 2013 Share Posted March 31, 2013 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. Link to comment Share on other sites More sharing options...
Recommended Posts