<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Update</title>
<script src="./jquery-3.6.0.min.js"></script>
</head>
<body>
<input type="text"><button>提交</button>
</body>
<script>
var button = document.querySelector("button");
button.addEventListener("click", function() {
var addedAge = document.querySelector("input").value;
$.ajax({
type: 'get',
url: "http://localhost/allSim/setAddedAge.php",
data: {
addedAge: addedAge
},
success: function(data) {
console.log(data);
/*data = JSON.parse(data) ;*/
if (data.msg) {
console.log(data.msg);
} else {
console.log(data);
}
}
});
})
</script>
</html>
php:
<?php
$addedAge=$_get["addedAge"];
$con=mysqli_connect("localhost","root",'','sim4_history_saves_sim');
if($con){
mysqli_query($con,"set names utf8");
$sql="update gameupdateddata set value=$addedAge where item=addedAge" ;
$result=mysqli_query($con,$sql);
if($result->num_rows>0){
$data=mysqli_fetch_all($result,MYSQLI_ASSOC);
mysqli_close($con);
echo json_encode($data);
}else{
echo json_encode(array("msg"=>"查询结果不存在"));
}
}else{
echo json_encode(array("msg"=>"数据库连接错误!"));
}
?>
我想实现一个改数据的功能
html文件是一个输入框和按钮,输入要改的值,点击按钮,更改数据库的值
数据库的值没有被改,我把data打印出来是
<br />
<b>Warning</b>: Undefined variable $_get in <b>C:\xampp\xampp\htdocs\allSim\setAddedAge.php</b> on line <b>2</b><br />
<br />
<b>Warning</b>: Trying to access array offset on value of type null in <b>C:\xampp\xampp\htdocs\allSim\setAddedAge.php</b> on line <b>2</b><br />
<br />
<b>Fatal error</b>: Uncaught mysqli_sql_exception: 由于目标计算机积极拒绝,无法连接。 in C:\xampp\xampp\htdocs\allSim\setAddedAge.php:3
Stack trace:
#0 C:\xampp\xampp\htdocs\allSim\setAddedAge.php(3): mysqli_connect('localhost', 'root', '', 'sim4_history_sa...')
#1 {main}
thrown in <b>C:\xampp\xampp\htdocs\allSim\setAddedAge.php</b> on line <b>3</b><br />
update.html:31 <br />
<b>Warning</b>: Undefined variable $_get in <b>C:\xampp\xampp\htdocs\allSim\setAddedAge.php</b> on line <b>2</b><br />
<br />
<b>Warning</b>: Trying to access array offset on value of type null in <b>C:\xampp\xampp\htdocs\allSim\setAddedAge.php</b> on line <b>2</b><br />
<br />
<b>Fatal error</b>: Uncaught mysqli_sql_exception: 由于目标计算机积极拒绝,无法连接。 in C:\xampp\xampp\htdocs\allSim\setAddedAge.php:3
Stack trace:
#0 C:\xampp\xampp\htdocs\allSim\setAddedAge.php(3): mysqli_connect('localhost', 'root', '', 'sim4_history_sa...')
#1 {main}
thrown in <b>C:\xampp\xampp\htdocs\allSim\setAddedAge.php</b> on line <b>3</b><br />
我不太明白错在哪里了