从html格式的单选按钮设置mysql值

时间:2011-05-10 22:01:08

标签: php html forms radio-button

我想要一个网页上的两个单选按钮(用php编写)代表“是”和“否”。当我加载页面时,我希望它从mysql数据库中获取一个值并设置相应的单选按钮。当我点击另一个按钮时,我希望它更新数据库并重新加载页面。

我正在尝试使用简单的html表单,但没有运气。到目前为止我的代码(根本不工作:(是:

if (!isset($_POST['submit'])) {
    $sql = "SELECT challenge_me FROM contestants WHERE id=$id";
    $res = (mysql_fetch_assoc(mysql_query($sql, $db)));
    $challenge_me = $res["challenge_me"];

}else{
    $sql = "UPDATE contestants SET challenge_me='" . $_POST['YesNo'] . "' WHERE id='$id'";
    if(!mysql_query($sql, $db))
        echo mysql_error(), "<br/>Query '$sql'";
    $challenge_me = $_POST['YesNo'];
}

    echo'<form method="post" action="' . $PHP_SELF . '">';
    echo '<input type="hidden" name="submit" value="submit">';          
if($challenge_me == 1){
    echo'<input type="radio" name="YesNo" value="1" onClick="this.form.submit();" checked>Yes ';
    echo'<input type="radio" name="YesNo" value="0" onClick="this.form.submit();">No ';
}else{
    echo'<input type="radio" name="YesNo" value="1" onClick="this.form.submit();">Yes ';
    echo'<input type="radio" name="YesNo" value="0" onClick="this.form.submit();" checked>No ';
}
echo'</form>';  

if (!isset($_POST['submit'])) { $sql = "SELECT challenge_me FROM contestants WHERE id=$id"; $res = (mysql_fetch_assoc(mysql_query($sql, $db))); $challenge_me = $res["challenge_me"]; }else{ $sql = "UPDATE contestants SET challenge_me='" . $_POST['YesNo'] . "' WHERE id='$id'"; if(!mysql_query($sql, $db)) echo mysql_error(), "<br/>Query '$sql'"; $challenge_me = $_POST['YesNo']; } echo'<form method="post" action="' . $PHP_SELF . '">'; echo '<input type="hidden" name="submit" value="submit">'; if($challenge_me == 1){ echo'<input type="radio" name="YesNo" value="1" onClick="this.form.submit();" checked>Yes '; echo'<input type="radio" name="YesNo" value="0" onClick="this.form.submit();">No '; }else{ echo'<input type="radio" name="YesNo" value="1" onClick="this.form.submit();">Yes '; echo'<input type="radio" name="YesNo" value="0" onClick="this.form.submit();" checked>No '; } echo'</form>';

1 个答案:

答案 0 :(得分:1)

您的脚本似乎没有定义$ id,$ id从哪里获取其值?这可能是你问题的根源。您的脚本可能未在$ id

中传递任何值