正在尝试从mysql数据库+php中删除记录但不断收到错误信息

6ju8rftf  于 2021-06-21  发布在  Mysql
关注(0)|答案(2)|浏览(300)

我正试图使用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>
lfapxunr

lfapxunr1#

我知道怎么删除了。我使用id和post,所以我删除了id方法,只使用post

<!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>
rt4zxlrg

rt4zxlrg2#

您可以用这种方法删除记录。在这里,删除查询中不需要包含字段名。

<?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>

相关问题