如何将单选按钮的值插入数据库?

I have this code:

<label style="cursor:pointer;"><input type="radio" name="rads" value="'.$correct.'" id = "radio">'.$answer.'</label> 
<button onclick="myFunction()">Value</button>';

where $correct is a row in my database that shows if a question is correct or wrong and has a value of either 1 or 0 ($correct = $row['correct'] enter image description here

I'm trying to increment data in another table in the database anytime a user clicks on a radio button. enter image description here

For example, if a question is wrong the value will be '0' and when a user clicks on it, the 'wrong' row will be incremented and vice-versa.

I tried using PHP to insert the values:

$vote = isset($_GET['rads']);
               if($vote == 1){
                  $sql = mysqli_query($connection, "UPDATE stats SET question_id='$question_id', correct=correct + 1, wrong=0") ;
               } elseif($vote == 0) {
                   $sql = mysqli_query($connection, "UPDATE stats SET question_id='$question_id', correct=0, wrong=wrong + 1") ;
               }

but only the 'wrong' column was incrementing. I tried using js to print out the value of the selected radio button:

<p id =" demo"></p>
<script>
function myFunction() {
    var x = document.getElementById("radio").value;
    document.getElementById("demo").innerHTML = x;
}
</script>

But nothing appeared when I clicked the button. I also tried:

var try = document.getElementsByName('rads');
for (var i = 0, len = try.length; i < length; i++) {
if (try[i].checked == 0) {
    // increment wrong in db

    else if (try[i].checked == 1){
        //increment correct in db
    }

    break;

}

I'm not really sure what to do anymore. Sorry if I'm not clear.

查看全部
dpvmjk0479
dpvmjk0479
2015/02/26 13:31
  • php
  • html
  • mysql
  • javascript
  • jquery
  • 点赞
  • 收藏
  • 回答
    私信
满意答案
查看全部

3个回复