马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
我想在student.php文件中实现点击selectStu.php展示出来的按钮,调用deleteStu.php,从而实现删除数据库中相应id的学生信息。但是点击按钮,JS代码并没有如期运作。
student.php<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>学生</title>
<link rel="stylesheet" href="style/public.css">
<link rel="stylesheet" href="style/select.css">
<script src="script/deleteStu.js" async></script>
</head>
<body>
<?php include "header.php";?>
<main>
<?php include 'addStu.php';?>
<button><a href="index.php">返回</a></button>
<?php include "selectStu.php";?>
</main>
<?php include "footer.php";?>
</body>
</html>
selectStu.php<?php
//读取配置文件
$configFile = 'config.json';
$configMsg = file_get_contents($configFile);
//解析为数组
$config = json_decode($configMsg,true);
//连接数据库参数
$servername = $config['host'];
$username = $config['username'];
$password = $config['password'];
$database = $config['database'];
//创建连接
$conn = new mysqli($servername, $username, $password, $database);
//检测连接
if($conn->connect_error){
die("连接失败" . $conn->connect_error);
}
//echo "连接成功!";
//查询语句
$selectAll = "SELECT * FROM `student`";
$result = $conn->query($selectAll);
//输出数据
if($result->num_rows > 0){
while($row = $result->fetch_assoc()){
//输出信息到页面
echo "<table>"
. "<tr>"
. "<td>" . $row["id"] . "</td>"
. "<td>" . $row["name"] . "</td>"
. "<td>" . $row["gender"] . "</td>"
. "<td>" . $row["klass"] . "</td>"
. "<td><button id=". $row["id"] ."class='update'>修改</button></td>"
. "<td><button id=". $row["id"] ."class='delete'>删除</button></td>"
. "</tr>"
. "</table>" ;
}
}
?>
deleteStu.php//这是删除学生信息的PHP
<?php
//读取配置文件
$configFile = 'config.json';
$configMsg = file_get_contents($configFile);
//解析为数组
$config = json_decode($configMsg,true);
//连接数据库参数
$servername = $config['host'];
$username = $config['username'];
$password = $config['password'];
$database = $config['database'];
//创建连接
$conn = new mysqli($servername, $username, $password, $database);
//检测连接
if($conn->connect_error){
die("连接失败" . $conn->connect_error);
}
//获取要删除信息的id
$id = $_GET["q"];
//预处理及绑定
$stmt = $conn->prepare("DELETE FROM student WHERE id = ?");
$stmt->bind_param("i",$id);
//执行
$stmt->execute();
//关闭
$stmt->close();
$conn->close();
?>
deleteStu.js//删除学生信息的JS
//获取元素
let buttons = document.getElementsByClassName("delete");
console.log(buttons[0]);
//添加事件
for(let i=0;i<buttons.length;i++){
buttons[i].addEventListener("click",deleteStu);
console.log(buttons[i]);
}
//传递学生id的函数
function deleteStu(){
let xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange=function(){
if(xmlhttp.readyState==4 && xmlhttp.status==200){
console.log("传递成功!");
}
}
xmlhttp.open("GET","deleteStu.php?q="+this.id);
xmlhttp.send();
}
gpt3.5 没这么智能的,还是要靠你自己啊 
|