我试图在 php 中使用 ajax 插入值,但数据未插入数据库中。我从本网站其他问题中回答的问题中获取了这段代码。谁能建议我在哪里犯错..?
<script>
$("#submit").click(function() {
var name= $("#name").val();
var password= $("#password").val();
$.ajax({
type: "POST",
url: "insert.php",
data: "name=" + name+ "&password=" + password,
success: function(data) {
alert("sucess");
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
<?php
//------insert.php------
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "dbname";
// Create connection
$conn = new mysqli($servername, $username, $password,$dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$name=$_POST['name'];
$pass=$_POST['password'];
$sql= mysqli_query($conn,"INSERT INTO insert_tbl(name,pass) VALUES('".$name."','".$pass."')");
?>
原文由 Dhruvi Mistry 发布,翻译遵循 CC BY-SA 4.0 许可协议
也可以加载
在你的脚本标签之前或使用