我正在尝试将一些数据发送到我存储在服务器上的MySQL数据库。当我按下提交按钮时,我的PHP脚本让我知道与数据库的连接已成功,但数据的插入却没有。我在错误日志中找不到任何问题,或者在我通过谷歌浏览器的开发人员工具检查代码时出现明显的问题。
以下是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表称为“用户”,该列称为“评级”。该列设置为int(3)[这应该与滑块最大值100?]匹配。
任何人都可以找出数据未插入数据库的原因吗?
答案 0 :(得分:1)
更改此
$ sql =“INSERT INTO用户值('$ rating')”;
到这个
$sql="INSERT INTO user (`rating`) VALUES ('$rating')";
rating将是db中该字段的列名,如果该表有多个列,则该语句需要说明需要在哪个列中放置。
也显示语句的错误使用此
if(mysqli_query($conn,$sql) or die(msqli_error($conn))){
... your code here > it worked
}else{
... your code here > it failed
}