单击按钮将用户ID插入mysql数据库

时间:2018-02-07 14:34:50

标签: mysql insert

我有一个问答应用程序。我创建了单击按钮,用户可以使用它来回答问题。单击这些按钮时,答案将进入数据库。我也试图在用户回答问题的同时将用户的ID输入数据库。

以下是 abc.php 的代码(按钮所在的页面):

<input type="radio" name="response" value="A">A<br/><br/>
<input type="radio" name="response" value="B">B<br/><br/>
<input type="radio" name="response" value="C">C<br/><br/>
<h3 id="result"></h3>
<br/>
<script>
$(document).ready(function() {
    $('input[type="radio"]').click(function() {
        var response = $(this).val();
        $.ajax({
            url: "insert.php",
            method: "POST",
            data: { response: response },
            success: function(data) {
                $('#result').html(data);
            }
        });
    });
});    
</script>

insert.php

if (isset($_POST["response"])) {
    $query = "INSERT INTO response(response) VALUES (:response)";
    $statement = $conn->prepare($query);
    $statement->execute(array(':response' => $_POST["response"]));
    $count = $statement->rowCount();
    if ($count > 0) {
        echo "Your answer has been submitted!";
    } else {
        echo "There was an error!";
    }
}
?>

0 个答案:

没有答案