Reputation:
I'm doing a security check with my radio buttons, and it's returning an error even though I don't think there should be. Any idea what I'm doing wrong?
This is the class to update
public function set ($iStatus)
{
$this->iStatus = $iStatus;
}
public function create ()
{
if ($this->iStatus != 0 || $this->iStatus != 1 || $this->iStatus != 2)
{
echo "Your idea must have a valid status";
}
else
{
//update the database
}
}
and then the html form
if (isset($_POST["submit"]))
{
$class->set($_POST["status"]);
$class->create();
}
else
{
<input type="radio" name="status" value="0" checked/>
<input type="radio" name="status" value="1" />
<input type="radio" name="status" value="2" />
}
And it returns the error valid status. I figured maybe inputs save everything as strings or chars so I redid the error checking to say
if ($this->iStatus != '0') { /*blah*/ }
but that didn't work either. So I am confused
Upvotes: 0
Views: 76
Reputation: 16828
your logic is backwards:
if ($this->iStatus != 0 || $this->iStatus != 1 || $this->iStatus != 2)
should be:
if ($this->iStatus != 0 && $this->iStatus != 1 && $this->iStatus != 2)
full test:
<?php
class foo{
private $iStatus;
public function set($iStatus){
$this->iStatus = $iStatus;
}
public function create(){
if ($this->iStatus != 0 && $this->iStatus != 1 && $this->iStatus != 2){
echo "Your idea must have a valid status";
}else{
echo "All good";
}
}
}
if (isset($_POST["submit"])){
$class = new foo;
$class->set($_POST["status"]);
$class->create();
}else{
echo '
<form method="post" action="./">
<input type="radio" name="status" value="0" checked>
<input type="radio" name="status" value="1">
<input type="radio" name="status" value="2">
<input type="radio" name="status" value="3">
<input name="submit" type="submit">
</form>';
}?>
Upvotes: 1
Reputation: 2841
the error is in the if statement. You should substitute the || with &&
otherwise you will always get the error message
Because even if it is 0 your if statement returns true because != 1 is true
Upvotes: 1