0

Error Message : Error deleting record: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '1' at line 1

Success to delete but how to get rid of this error message

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
    Please check what is the content of $dropdownlist1. it isn't a number i guess and also take a look at this https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php – nbk Nov 17 '19 at 14:29

1 Answers1

0

use $dropdownlist instead of $dropdownlist1

OR USE

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

WARNING : Your code is open to SQL Injection Attack, Use Prepared Statements

BadPiggie
  • 5,471
  • 1
  • 14
  • 28