我试图使用DELETE语句删除一行。但是,由于我是PHP的新手,我不知道自己做错了什么。我的代码不断回显错误信息,我不知道如何让它工作。
感谢。
<!DOCTYPE html>
<html lang="en">
<head>
<title>peak | delete student</title>
<meta charset="UTF-8">
</head>
<body>
<div style="margin-left: 300px;">
<form method="post">
<input type="text" name="student_number" placeholder="student number"
id="student number"><br>
<input type="submit" value="delete">
</form>
</div>
<?php
include 'config.php';
$id = '';
if( isset( $_GET['id'])) {
$id = $_GET['id'];
}
if(isset($_POST["student_number"])) {
$sql= "DELETE FROM `Students` (`first name`,`last name`,`student
number`,`gender`,`address`,`phone number`,`mark`) WHERE `student number`='".$id."'";
$result = mysqli_query($conn,$sql);
if($result){echo "student deleted successfully!";}
else {echo "error. unable to delete student.";}
}
?>
</body>
</html>
答案 0 :(得分:0)
您可以使用这种方式删除记录。这里,不需要在删除查询中包含字段名称。
<?php
$con=mysqli_connect("localhost","root","","test");
if(!$con)
die("could not connect:".mysqli_error());
else
echo "connected<br>";
if(isset($_GET["student_number"]))
{
$id = $_GET["student_number"];
$sel="select * from students where student_number='$id'";
$result=mysqli_query($con,$sel);
$row=mysqli_fetch_assoc($result);
$checkid=$row['student_number'];
if($id==$checkid)
{
$sql="delete from students where student_number='$id'";
$result=mysqli_query($con,$sql);
if($result)
{
echo "student deleted successfully!";
}
else
{
echo "error. unable to delete student.";
}
}
else
{
echo "no record found";
}
}
?>
<html>
<head>
<title>peak | delete student</title>
</head>
<body>
<div style="margin-left: 300px;">
<form method="get" action="">
<input type="text" name="student_number" placeholder="student number"
id="student number"><br>
<input type="submit" name="delete" value="delete">
</form>
</div>
</body>
</html>
答案 1 :(得分:0)
我想出了如何删除。我正在使用id和post,所以我删除了id方法,只使用了帖子
<!DOCTYPE html>
<html lang="en">
<head>
<title>peak | delete student</title>
<meta charset="UTF-8">
</head>
<body>
<div style="margin-left: 300px;">
<form method="post">
<input type="text" name="student_number" placeholder="student number"
id="student number"><br>
<input type="submit" value="delete">
</form>
</div>
<?php
include 'config.php';
if(isset($_POST["student_number"])) {
$sql= "DELETE FROM `Students` WHERE `student number`=".$_POST["student_number"];
$result = mysqli_query($conn,$sql);
if($result){echo "success!";}
else {echo "error.";}
}
?>
</body>
</html>