我刚刚建立了一个本地MAMP环境,我试图将一个名称插入到数据库表中作为测试。我在控制台或PHP日志中没有出现任何错误,但就我而言,我无法将数据插入到数据库中。
HTML:
<form action="" method="GET" name="nameSubmit">
<input type="input" name="userName" class="user-name">
<input type="submit" value="Submit" class="name-submit">
</form>
JS:
var userSubmitButton = $('.name-submit'),
userNameInput = $('.user-name'),
userName;
userSubmitButton.click(function(e){
e.preventDefault();
userName = userNameInput.val();
console.log(userName);
$.ajax({
url: "nameSubmit.php",
type : "GET",
dataType : "json",
data : {type : "nameSubmit", name : userName},
success : function(response){
alert(JSON.stringify(response));
},
error : function(err){
alert(JSON.stringify(err));
}
});
});
PHP:
<?php
$mysqli = mysqli_connect("localhost", "root", "root", "test_name");
if (isset($_GET['type'])){
if($_GET['type'] == "nameSubmit"){
$name = $_GET['name'];
$stmt = $mysqli->prepare("INSERT INTO names (name) VALUES ('$name')");
echo "$name";
}
}
?>
响应表明已完成所有任务并返回$name
答案 0 :(得分:0)
你永远不会执行:
$stmt = $mysqli->prepare("INSERT INTO names (name) VALUES ('$name')");
$stmt->execute();