0

我建立了一个带有异常错误的检查,以便在没有选择任何值时让它知道。他显示了消息,但他也说没有选择值(在通知中),我该如何防止该错误?这是关于名称标签的“距离”。

PHP

if(isset($_POST["btnCreateGroup"])){
        try {
        $group = new Group();
        $group->GroupDistance = mysql_real_escape_string($_POST["distance"]);
        $group->GroupName = mysql_real_escape_string($_POST["groupname"]);
        //$group->Groupduration = mysql_real_escape_string($_POST["name"]);
        $group->CreateGroup();

        } catch(exception $e) {
        $feedback = $e -> getMessage();
    }
}


public function __set($p_sProperty, $p_vValue)
        {
            switch($p_sProperty)
            {
                case "GroupName":
                if(!empty($p_vValue))
                {
                    $this->m_sGroupName = $p_vValue;
                }
                else
                {
                    throw new Exception("Ow! Give me a name to your group");
                }
                break;

                case "GroupDistance":
                if(!empty($p_vValue))
                {
                    $this->m_iGroupDistance = $p_vValue;
                }
                else
                {
                    throw new Exception("Ow! Give me a distance to run, please");
                }
                break;



            }
        }

HTML

<select name="distance">
                        <option selected="selected" disabled="disabled" value="none">Distance</option>
                        <option value="1000">1000 meter</option>
                        <option value="1500">1500 meter</option>
                        <option value="2000">2000 meter</option>
                        <option value="2500">2500 meter</option>
                    </select>
4

1 回答 1

2

只需检查该值是否已设置,如果未设置,请使用默认值:

$distance  = (isset($_POST["distance"]))  ? $_POST["distance"]  : '';
$groupname = (isset($_POST["groupname"])) ? $_POST["groupname"] : '';
$name      = (isset($_POST["name"]))      ? $_POST["name"]      : '';

$group->GroupDistance = mysql_real_escape_string($distance);
$group->GroupName     = mysql_real_escape_string($groupname);
$group->Groupduration = mysql_real_escape_string($name);
于 2014-03-08T14:08:04.433 回答