PHP - API SQL - insert into
有冇人知我錯咩呢。。。
HTML- <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
- <script>
- $(document).ready(function () {
- $('#wdb').submit(function(e){
- e.preventDefault(); //prevent normal submit!
- if ($("#yourAnswer").val() != "" ){
- $.ajax({
- type: 'GET',
- url: 'wdb.php?username='+$('#username').val()&'question='+$('#question').val()&'answer='+$('#answer').val()&'yourAnswer='+$('#yourAnswer').val(),
- dataType: 'json',
- success: function (result) {
-
- }
- });
- } else {
- $("#message").html("Please enter your answer!!");
- $("#tfUser").focus();
- }
- });
- });
- </script>
複製代碼 api.php- <?php
- extract($_GET);
- require_once 'connectdb.php';
- $sql = "SELECT * FROM QuestionsLog";
- $stmt = $pdo->prepare($sql);
- $stmt->execute([$username]);
- $result = array('error' => false);
- if($stmt->rowCount() <= 0){
- $result['error'] = true;
- $sql = "INSERT INTO QuestionsLog (username, question, answer, yourAnswer) VALUES (?,?,?,?);";
- $username = $_GET['$username'];
- $question = $_GET['$question'];
- $answer = $_GET['$answer'];
- $yourAnswer = $_GET['$yourAnswer'];
- $stmt = $pdo->prepare($sql);
- $stmt->execute([$tfUser, $question, $answer, $yourAnswer]);
- $result = array('error' => false);
- } else {
- $arr = $stmt->fetch();
- $result['username, question, answer, yourAnswer'] = $arr['username, question, answer, yourAnswer'];
- }
- echo json_encode($result);
複製代碼 |
|
|