无法使用 (PHP、AJAX、JQ 将数据插入 MySQL 数据库

Unable to Insert Data into MySQL Database using (PHP, AJAX, JQ)

我正在尝试将一些数据发送到我存储在服务器上的 MySQL 数据库。当我按下提交按钮时,我的 PHP 脚本让我知道与数据库的连接已成功,但数据插入还没有。我在错误日志中找不到任何问题,或者当我通过 Google Chrome 的开发人员工具检查代码时发现明显的问题。

下面是HTML文件;

<html>
<head>
    <title>Home</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="viewport" content="width=device-width">
    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css"/>

    <script type="text/javascript" src="http://code.jquery.com/jquery-1.11.3.min.js"></script>

    <script type="text/javascript" src="http://example.com/JS/functions.js"></script> 

    <script type="text/javascript" src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>

    //CSS
    <style>
        .ui-page { 
           background: #4990E2;
        }
               p {
           color: #ffffff;
        }
    </style>
</head>
       div data-role="page" id="page1">
        <div data-role="header" data-theme="a">
    <h1>Welcome</h1>
      </div>
                  <!-- Main page content goes under this line -->
 <div data-role="main" class="ui-content">
  <br>
  <br>
  <br>
 <form id="myForm" method="post" action="include.php">
   <label for="rating">How are you feeling?</label>
   <input type="range" name="rating" id="rating" value="50" min="0" max="100" data-popup-enabled="true">
   <input id="submit" type="submit" data-inline="true" value="Submit">
 </form>
  <br>
  <br>
      <span id="result"></span>
  <br>
      <p>Some text</p>
 </div>

 <div data-role="footer" data-position="fixed" data-theme="b">
    <div data-role="navbar">
            <ul>
                <li><a href="#page1" class="ui-btn-active ui-state-persist"  data-transition="none">Home</a></li>
                <li><a href="#page2" data-transition="none">Example</a></li>
                <li><a href="#page3" data-transition="none">Example</a></li>
                <li><a href="#page4" data-transition="none">Example</a></li>
            </ul>
        </div>
    </div>
</div>
                                <!-- Page ends here -->
                                <!--                -->

下面是PHP文件(db.php);

<?php

//put your connection code here
    $servername = 'localhost';
    $username = 'user_admin';
    $password = '12345-678';
    $dbname = 'my_DB';

//create connection
    $conn = new mysqli($servername, $username, $password, $dbname);

    // Check connection
    if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
    } 
    echo "Connected successfully, <br>";
?>

下面是PHP文件(include.php);

<?php

include 'db.php';

$rating = $_POST['rating'];

$sql="INSERT INTO user Values('$rating')";
if(mysqli_query($conn,$sql)){
       echo "Successfully Inserted";
 }  else {
        echo "Did NOT insert";
 }
?>

下面是Javascript文件(functions.js);

// AJAX-ify the slider
$(document).on("pagecreate","#page1",function()
{

$("#submit").click( function() {

$.post( $("#myForm").attr("action"),
   $("#myForm :input").serializeArray(),
     function(info){ $("#result").html(info);
   });
});

$("#myForm").submit( function() {
return false;
  });
});

MySQL table 称为 'user',列称为 'rating'。该列设置为 int(3) [这应该与滑块最大值 100 匹配吗?]。

谁能指出为什么数据没有被插入到数据库中?

改变这个

$sql="INSERT INTO user Values('$rating')";

至此

$sql="INSERT INTO user (`rating`) VALUES ('$rating')";

rating 将是数据库中该字段的列名,如果 table 有多个列,则语句需要说明它需要放在哪一列中。

也可以使用这个

来显示语句的错误
if(mysqli_query($conn,$sql) or die(msqli_error($conn))){
    ... your code here > it worked
}else{
    ... your code here > it failed
}