使用下拉列表从数据库中删除数据,但收到错误消息

时间:2019-11-17 14:23:19

标签: php html mysql

错误消息:删除记录时出错:您的SQL语法有错误;检查与您的MariaDB服务器版本相对应的手册以获取正确的语法,以在第1行的'1'附近使用

删除成功,但如何摆脱此错误消息

Output.php

        mysqli_query($link, "CREATE TABLE Student (
            Student_Name VARCHAR(100),
            IC_Number VARCHAR(15),
            Matric_Number VARCHAR(10),
            PRIMARY KEY (Matric_Number)
        )");

        $stdname = $_POST["stdname"];
        $icno = $_POST["icno"];
        $matricno = $_POST["matricno"];

        $data = "INSERT INTO Student (Student_Name, IC_Number, Matric_Number) 
        VALUES ('$stdname', '$icno', '$matricno')";

delete_form.php

<!DOCTYPE HTML>
<html>
<body>
<form action="todelete.php" method="post">
<h2>Delete Student</h2>
    <select name = "dropdownlist">
    <?php
        $link = mysqli_connect("localhost", "root", "") or die(mysqli_connect_error());

        mysqli_select_db($link, "myDataBase") or die(mysqli_error($link));

        $result = mysqli_query($link, "SELECT Matric_Number FROM Student");

        while($row = mysqli_fetch_array($result)){ 
            echo "<option value ='" . $row['Matric_Number'] . "'>" . $row['Matric_Number'] . '</option>';
        } 

        mysqli_close($link); 
    ?>
    <input type="submit" value="Delete">
    </select>
<form>  
<br><br>
<a href= "view_student.php">Click here to list the table</a>
</body> 
</html>

todelete.php

<!DOCTYPE HTML>
<html>
<body>
    <?php
        $link = mysqli_connect("localhost", "root", "") or die(mysqli_connect_error());

        mysqli_select_db($link, "myDataBase") or die(mysqli_error($link));

        if(isset($_POST['dropdownlist'])){
        $dropdownlist1 =  $_POST['dropdownlist'];

        $result = mysqli_query($link, "DELETE FROM Student WHERE Matric_Number = '$dropdownlist1'");

        if (mysqli_query($link, $result)){
            echo "Record deleted successfully";
        } else {
            echo "Error deleting record: " . mysqli_error($link);
        }
        }
        mysqli_close($link);
    ?>
<br><br>
<a href= "view_student.php">Click here to list the table</a>
</body> 
</html>

1 个答案:

答案 0 :(得分:0)

使用$dropdownlist代替$dropdownlist1

或使用

mysqli_query($link, "DELETE FROM Student WHERE Matric_Number = '".$dropdownlist1."'");
  

警告::您的代码已向SQL Injection Attack开放,请使用Prepared Statements