我的删除按钮不起作用

时间:2015-01-19 13:43:10

标签: php

我陷入了删除功能,我想知道为什么我的删除按钮无法正常工作,我已经编辑了我的代码。有人可以帮助我。

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

这是表单方法

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

这里是以表格形式显示数据,SELECT *正在运行

$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>";
}

这是删除按钮代码

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

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


}

1 个答案:

答案 0 :(得分:0)

试试这个:

         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();
}