用Php&删除一行MySQL的

时间:2015-04-14 13:49:17

标签: php mysql forms

我是php和SQL的新手,只是为了自己理解从我的数据库中访问,更新和删除数据的项目而无所事事。

我已设法显示所选数据,创建一个按钮来删除特定的ID但在删除所需的行或记录时需要一些帮助,而不是在我的删除p​​hp脚本中的ID中进行硬编码。

以下是我的脚本示例:

<?php

    $sql = "SELECT id, firstname, lastname, joinDate FROM customers";
    $result = $conn->query($sql);

    if ($result->num_rows > 0) {
        // output data of each row

        while($row =  $result->fetch_assoc()) {
            echo 
                "<div class='trow'>" . 
                $row["id"]. ": " . 
                $row["firstname"] . " " . 
                $row["lastname"]. " " . 
                $row["joinDate"]. " " . 
                "<span class='deleteMember'>
                    <form action='deleteMember.php' method='POST'>
                        <button type='submit'>Delete</button>
                    </form>
                </span>" . " " . 
                "<span class='editMember'><a href='#'>Edit</a></span>" . 
                "<br></div>";
        }

    } else {
        echo "0 results";
    }

    $conn->close();

?>

这是delete.php

<?php

    // sql to delete a record
    $sql = "DELETE FROM customers WHERE id='6' ";

    if ($conn->query($sql) === TRUE) {
       header("Location: index.php");
    } else {
        echo "Error deleting record: " . $conn->error;
    }

    $conn->close();

?>

我希望它能够删除你从中删除按钮的行,而不只是删除我在delete.php脚本中指定的行。我了解如何通过发布ID但不确定如何操作来实现它。

4 个答案:

答案 0 :(得分:0)

代替您的表单使用此

<a href="deleteMember.php?id=<?php echo $row['id'];?>">DELETE</a>

并且在您的删除查询中必须如下所示

$sql = "DELETE FROM customers WHERE id='".$_GET['id']."' ";

答案 1 :(得分:0)

这样做

<?php

    // sql to delete a record
    $sql = "DELETE FROM customers WHERE id='".$_GET['id']."' ";

    if ($conn->query($sql) === TRUE) {
       header("Location: index.php");
    } else {
        echo "Error deleting record: " . $conn->error;
    }

    $conn->close();

?>

<?php

    $sql = "SELECT id, firstname, lastname, joinDate FROM customers";
    $result = $conn->query($sql);

    if ($result->num_rows > 0) {
        // output data of each row

        while($row =  $result->fetch_assoc()) {
            echo 
                "<div class='trow'>" . 
                $row["id"]. ": " . 
                $row["firstname"] . " " . 
                $row["lastname"]. " " . 
                $row["joinDate"]. " " . 
                "<span><a href='deleteMember.php?id=".$row['id']."'>Delete</a></span>" . 
                "<span class='editMember'><a href='#'>Edit</a></span>" . 
                "<br></div>";
        }

    } else {
        echo "0 results";
    }

    $conn->close();
?>

答案 2 :(得分:0)

无需为删除或编辑目的添加额外的form元素。尝试这种方式为Eelete或编辑操作传递id

while($row =  $result->fetch_assoc()) 
       {
            $id=$row['id'];// capture your row id & pass to your delete & edit
            echo 
             "<div class='trow'>" . 
                 $row["id"]. ": " . 
                 $row["firstname"] . " " . 
                 $row["lastname"]. " " . 
                 $row["joinDate"]. " " . 
                 "<span class='deleteMember'>
                    <a href='deleteMember.php?id=<?=$id;?>'>Delete</a>
                 </span>" . " " . 
                 "<span class='editMember'>
                     <a href='editMember.php?id=<?=$id;?>'>Edit</a>
                 </span>" . 
                 "<br>
           </div>";
        }

修改 然后抓住相关页面上的id进行操作。

// deleteMember.php

<?php
    $id=$_GET['id'];
    // sql to delete a record
    $sql = "DELETE FROM customers WHERE id='".$id."'";

    if ($conn->query($sql) === TRUE) {
       header("Location: index.php");
    } else {
        echo "Error deleting record: " . $conn->error;
    }

    $conn->close();

?>

注意:请使用PDOMYSQLi的准备好的语句来避免SQL Injection和手动转义。

答案 3 :(得分:0)

或留在您的帖子表格中:

 while($row =  $result->fetch_assoc()) {
            echo 
                "<div class='trow'>" . 
                $row["id"]. ": " . 
                $row["firstname"] . " " . 
                $row["lastname"]. " " . 
                $row["joinDate"]. " " . 
                "<span class='deleteMember'>
                    <form action='deleteMember.php' method='POST'>
                        <input type='hidden' name='myid' value='".$row['id']."' />
                        <button type='submit'>Delete</button>
                    </form>
                </span>" . " " . 
                "<span class='editMember'><a href='#'>Edit</a></span>" . 
                "<br></div>";
        }

在你的delete.php中:

<?php
    $id=(int) $_POST['myid'];

    // sql to delete a record
    $sql = "DELETE FROM customers WHERE id=".$id;

    if ($conn->query($sql) === TRUE) {
       header("Location: index.php");
    } else {
        echo "Error deleting record: " . $conn->error;
    }

    $conn->close();

?>