-2

I'm stuck in the delete function, I wonder why my delete button is not functioning, and I already edited my code.

<?php  
if ($_SERVER["REQUEST_METHOD"] == "POST") 
{
    $semester = ($_POST["semester"]);
    $level = ($_POST["level"]);
}
?>

Here is the form method:

<form method="post" action="<?php echo($_SERVER["PHP_SELF"]);?>"     enctype="multipart/form-data"> 

Here is to display the data in table form, and SELECT * is functioning

$sql = mysqli_query ($connection, "SELECT * FROM subject");

    echo " <table>
    <th>Semester</th>
    <th>Level</th>
    </tr>";

while($record = mysqli_fetch_assoc ($sql)){
    echo "<tr>";
    echo "<td>" . $record['semester'] . "</td>";
    echo "<td>" . $record['level'] . "</td>";
    echo "<td>" . "<input type=submit name=delete value=Delete>" . "</td>";
    echo "</tr>";
}

This is the delete button code

if (isset($_POST['delete']))
{

    $delete = mysqli_query ($connection, "DELETE FROM subject WHERE semester = '($_POST[semester])'");


}
halfer
  • 19,824
  • 17
  • 99
  • 186
minimomo
  • 581
  • 1
  • 6
  • 16

1 Answers1

0

Try this :

         while($record = mysqli_fetch_assoc ($sql)){
           echo "<tr>";
           echo '<form action="mypage.php" method="post">';
           echo "<td>" . $record['semester'] . "</td>";
           echo "<td>" . $record['level'] . "</td>";
           echo "<td>" . $record['course'] . "</td>";
           echo "<td>" . $record['subject'] . "</td>";
           echo "<td>" . $record['section'] . "</td>";
        // And add field form hidden
           echo '<input type="hidden" name="semester" value="'.$record['semester'].'">';
           echo "<td>" . '<input type="submit" name="delete" value="Delete">' . "</td>";
           echo "</form>";
           echo "</tr>";
        }

   if (isset($_POST['delete']) && isset($_POST['semester']))
{
$stmt = $connection->prepare('DELETE FROM subject WHERE semester = ?');
// if $_POST['semester'] is integer else see http://php.net/manual/en/mysqli-stmt.bind-param.php
$stmt->bind_param('i', $_POST['semester']);

$stmt->execute();
}
toto21
  • 612
  • 5
  • 9