0

I want two radio buttons on a webpage (written in php) representing "yes" and "no". When I load the page I want it to fetch a value from a mysql db and set the corresponding radio button. And when I click on the other button, I want it to update the database and reload the page.

I'm trying to do this with a simple html form, but no luck. The code I have so far (that is not working at all :( is:

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>';  

3
  • 1
    Not really an answer to your question, but you shouldn't be using string concatenation to build an SQL query. Your script is currently vulnerable to SQL injection based on using $_POST['YesNo'] without at least escaping it or using mysql statements. Commented May 10, 2011 at 22:09
  • How is it not working? What is happening? Commented May 10, 2011 at 22:12
  • Hmm, seems like I solved it. It didn't work if the hidden post was named "submit". That prevented the form to do anything when I clicked on the radio buttons. Renaming it to something else worked well... And thanks for the comment about sql injection! Commented May 11, 2011 at 5:41

1 Answer 1

1

Your script doesnt seem to define $id, where does $id get its value from? That could be the source of your problem. Your script might not be passing any value in $id

Sign up to request clarification or add additional context in comments.

4 Comments

also, $PHP_SELF is blank i'm assuming. I think you are trying to use $_SERVER['PHP_SELF']. this probably isn't affe4cting the code however because leaving the action blank will automatically submit the form to itself.
Well, $id comes as an argument to the function (I removed a couple of lines of code that I thought wasn't interesting for my question).
@faximan have you resolved the problem yet? have you tried @dqhendricks suggestion, that is another possible cause
Yeah, thanks. It works now, all that was needed was to change the name of the hidden input from "submit" to "submitValue".Guess "submit" is some kind of reserved word.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.