2013-02-15 99 views
0

我正在使用单选按钮进行安全检查,即使我认为不应该出现错误,也会返回错误。任何想法我做错了什么?使用单选按钮

这是更新

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 
    } 
} 

类,然后HTML表单

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" /> 
} 

并返回错误有效状态。我想也许输入保存所有字符串或字符,所以我重做了错误检查说

if ($this->iStatus != '0') { /*blah*/ } 

但也不能工作。所以我很困惑

+0

一个愚蠢的问题,你确定表单是POST类型而不是GET吗?如果用$ _REQUEST替换$ _POST,会得到什么结果? – Vasilis 2013-02-15 22:30:51

+0

不,它是后,我喜欢在盒子外面的想法。我在表格中的其他字段工作正常,但是当我添加这些单选按钮后,它不会提交。 – Alex 2013-02-15 22:33:46

回答

1

你的逻辑是向后返回true:

if ($this->iStatus != 0 || $this->iStatus != 1 || $this->iStatus != 2) 

应该是:

if ($this->iStatus != 0 && $this->iStatus != 1 && $this->iStatus != 2) 

全面测试:

<?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>'; 
}?> 
+0

谢谢!你是对的我把比较运算符从==改变为!=,忘记了也改变逻辑运算符。谢谢! – Alex 2013-02-15 22:36:13

1

错误是在if语句中。你应该替换||与& &

否则你总是会得到错误信息

因为即使是0你的if语句,因为= 1为真

+0

这不是一个PHP错误,只是我有“你的想法必须有一个有效的地位”的错误,我试过了,当你打印$ iStatus的价值时仍然是同样的问题 – Alex 2013-02-15 22:26:18

+0

你会得到什么? – Vasilis 2013-02-15 22:27:35

+0

我得到一个0,所以是的,我不知道怎么回事 – Alex 2013-02-15 22:29:09