删除最后一条记录而不是选择一条记录,如何添加编辑功能

时间:2014-02-14 08:40:32

标签: php mysqli edit sql-delete

当我尝试删除特定记录而不是删除表中的最后一条记录时,我正在从数据库表中将数据提取到页面中。

这是fetchtable.php。如何在其中添加编辑功能?请尽可能详细解答:

<?php
require 'conn.php';
$tfetch = "select ID,firstname,lastname,gender,email, password from signup";
if ($stmt = mysqli_prepare ($conn,$tfetch))
{
    mysqli_stmt_execute ($stmt); 
    mysqli_stmt_bind_result ($stmt, $id, $fn, $ln, $gen, $email, $pass );
    mysqli_stmt_store_result ($stmt);

}
mysqli_close ($conn);
?>

这是我正在抓取的页面:

<?php
include'fetchtable.php';
if(isset($_POST['del']))
{
    require 'conn.php';
    $id=$_POST['del_id'];
    $stmt = "DELETE FROM signup WHERE ID =$id";
    mysqli_query($conn,$stmt);
    mysqli_execute($stmt);
    $row=mysqli_affected_rows($conn);
    if($row==1)
    {
        echo " sucess ! record was deleted ";
    }
    else
    {
        echo " record was not deleted ";
    }
    mysqli_close($conn);
}
?>

<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post">
    <?php
    echo "<table border='1' cellpadding='2' cellspacing='2'";
    echo "<tr><td>ID</td><td>First Name</td><td>Last Name</td><td>Gender</td><td>Email</td><td>Password</td><td>Delete</td>";
    while (mysqli_stmt_fetch($stmt))
    {
        echo"<tr>";
        echo "<td>".$id."</td>";
        echo "<td>". "$fn" ."</td>";
        echo "<td>". "$ln" ."</td>";
        echo "<td>". "$gen"."</td>";
        echo "<td>". "$email"."</td>";
        echo "<td>". "$pass" ."</td>";
        echo '<td> <input type="hidden" name="del_id" value="'.$id.'" /> <input type="submit" name="del" value="delete" /> </td>';
        echo"</tr>";
    }
    ?>
</form>

编辑:找到解决方案只是在表单中包含while循环而不是在表单中包含

1 个答案:

答案 0 :(得分:0)

删除最后一条记录b / c变量$id在fetch和delete中都相同。

$delete_id = $_POST['del_id'];
$del_stmt = "DELETE FROM signup WHERE ID =$delete_id";
mysqli_query($conn,$del_stmt);
mysqli_execute($del_stmt);