Samurai Legend Posted February 13, 2015 Share Posted February 13, 2015 So when I add 0 in the input boxes in staff files my outcomes is invalid input or something like that if (!$price || !$will || !$_POST['id']) { echo 'Sorry, invalid input. <br />> <a href="staff_houses.php?action=edithouse">Go Back</a>'; die($h->endpage()); } I realised it is something to do with !$_POST['id'] however I thought it will show only if it is a empty field however it includes 0s to. How can I go around this? Quote Link to comment Share on other sites More sharing options...
KyleMassacre Posted February 13, 2015 Share Posted February 13, 2015 0 => false, ! => false so that's why you are getting it So you need to find where th variables are declared and fiddle with those Quote Link to comment Share on other sites More sharing options...
Sim Posted February 13, 2015 Share Posted February 13, 2015 put this before it all: echo "Price: $price <br>"; echo "Will: $will <br>"; echo "ID: " . $_POST['id']; Your find out what the problem is at least l;] Quote Link to comment Share on other sites More sharing options...
Coly010 Posted February 13, 2015 Share Posted February 13, 2015 I had the same problem and was the reason stopped with my mccodes project. The code style in the files was disgusting to me. There are parts that have Empty($var) If $var = 0 ; then that empty will return true telling you you haven't filled the form out right. So I would change the !$price to !isset($price) etc Quote Link to comment Share on other sites More sharing options...
SRB Posted February 13, 2015 Share Posted February 13, 2015 $id = array_key_exists('id', $_POST) && (ctype_digit($_POST['id']) || is_int($_POST['id'])) ? $_POST['id'] : FALSE ; if (!$id) { exit('Nooooooooooooooooooooo'); } That is all. Quote Link to comment Share on other sites More sharing options...
Guest Posted February 13, 2015 Share Posted February 13, 2015 $id = array_key_exists('id', $_POST) && (ctype_digit($_POST['id']) || is_int($_POST['id'])) ? $_POST['id'] : FALSE ; if (!$id) { exit('Nooooooooooooooooooooo'); } That is all. No neat enough $id = array_key_exists('id', $_POST) && (ctype_digit($_POST['id']) || is_int($_POST['id'])) ? $_POST['id'] : FALSE ; if (!$id) exit('Nooooooooooooooooooooo');} Gotta 1 line it! haha Quote Link to comment Share on other sites More sharing options...
Dave Posted February 13, 2015 Share Posted February 13, 2015 No neat enough $id = array_key_exists('id', $_POST) && (ctype_digit($_POST['id']) || is_int($_POST['id'])) ? $_POST['id'] : FALSE ; if (!$id) exit('Nooooooooooooooooooooo');} Gotta 1 line it! haha Never 1 line anything! Makes it super hard to read. Spread it out, let it be free, other developers will like you more ;) Quote Link to comment Share on other sites More sharing options...
Guest Posted February 13, 2015 Share Posted February 13, 2015 Never 1 line anything! Makes it super hard to read. Spread it out, let it be free, other developers will like you more ;) I was joking. Quote Link to comment Share on other sites More sharing options...
Coly010 Posted February 13, 2015 Share Posted February 13, 2015 Never 1 line anything! Makes it super hard to read. Spread it out, let it be free, other developers will like you more ;) Yeah i have to agree, readability is a hella important to me. makes it easier to change / fix Quote Link to comment Share on other sites More sharing options...
Jax Posted February 13, 2015 Share Posted February 13, 2015 $id = array_key_exists('id', $_POST) && (ctype_digit($_POST['id']) || is_int($_POST['id'])) ? $_POST['id'] : FALSE ; if (!$id) { exit('Nooooooooooooooooooooo'); } But surely, as both $_GET and $_POST variables are passed as strings or arrays, unless god forbid you modify them, there is no need for the is_int my random friend. $id = array_key_exists('id', $_POST) && ctype_digit($_POST['id']) ? $_POST['id'] : false; # note the === to detect ONLY false, and not zero if ($id === false) { header('HTTP/1.1 400 Bad Request'); exit; } array_key_exists() is a sensible option as isset() or empty() can produce unexpected results. ctype_digit() will type check first; it expects a string before checking the contents and only returning true if the value is a string of possibly 0 leading digits. Of course, you still have to range check, and blindly casting to an int can yield unusual albeit edge case results. Using the more correct in this case === rather than ! allows the use to pass zero without the script terminating early. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.