简体   繁体   English

初学者:从表和数据库PHP / HTML / MySQL中删除行

[英]Beginner: Delete row from Table and Database PHP/HTML/MySQL

Complete newbie to PHP/MySQL and HTML so please bear with me if I dont explain myself properly. 完成PHP / MySQL和HTML的新手,所以如果我不能正确解释自己的话,请忍受我。 At present I have an order form which stores the order in my database and shows it on a table in my admin area. 目前,我有一个订单,该订单将订单存储在数据库中,并在管理区域的表中显示。 I would like to be able to delete the row from the table and my database when I have completed the order. 完成订单后,我希望能够从表和数据库中删除该行。

At present my code looks like this: 目前,我的代码如下所示:

<?php
//87229feely.php
include('connection.php');
$result = mysql_query("SELECT * FROM orderform");
echo "<table border='1' >
<tr>
<th><u>ID</th>
<th><u>Date</th>
<th><u>Product</th>
<th><u>Product Comments</th>
<th><u>Name</th>
<th><u>Address</th>
<th><u>Age</th>
<th><u>Delivery</th>
<th><u>Delete</th>

</tr>";

while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['id']. "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td>" . $row['product'] . "</td>";
echo "<td>" . $row['productcomments'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['address'] . "</td>";
echo "<td>" . $row['age'] . "</td>";
echo "<td>" . $row['delivery'] . "</td>";
echo "<td><a href=\"deleterow.php?id=".$row['id']."\">delete</a></td>";

echo "</tr>";
}
echo "</table>";
?>

    <?php
//deleterow.php
    include('connection.php');
    $id = $_GET['id']; //this needs to be sanitized 
    if(!empty($id)){
        $result = mysql_query("DELETE FROM orderform WHERE id=".$id.";");
    }
    header("Location: 87229feely.php");
    ?>

Any help? 有什么帮助吗? All greatly appreciated. 所有人都表示赞赏。 Thanks 谢谢

Many ways to do this. 有很多方法可以做到这一点。 Since you're a beginner, this would probably be the most straight-forward (albeit not how I would do it) 由于您是初学者,所以这可能是最简单的方法(尽管不是我会做的那样)

  1. Create a form around the table 在表格周围创建一个表单
  2. Add a button on the delete column for each row and assign an id to it (the ID should be the ID from your database) <input type="submit" name="submit" value"/*YOUR ID*/"> delete列的每一行上添加一个按钮,并为其分配一个id (该ID应该是数据库中的ID) <input type="submit" name="submit" value"/*YOUR ID*/">
  3. Add some processing script before the table is being parsed 在解析表之前添加一些处理脚本
 if (isset($_POST['submit'])) { $sql = "DELETE FROM table WHERE id='/*YOUR ID*/'"; mysql_query($sql); } 

This answer does not meet security standards but should do the job: 此答案不符合安全性标准,但应执行以下操作:

<?php
//index.php
include('connection.php');
$result = mysql_query("SELECT * FROM orderform");
echo "<table border='1' >
<tr>
<th><u>ID</th>
<th><u>Date</th>
<th><u>Product</th>
<th><u>Product Comments</th>
<th><u>Name</th>
<th><u>Address</th>
<th><u>Age</th>
<th><u>Delivery</th>
<th><u>Delete</th>

</tr>";

while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['id']. "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td>" . $row['product'] . "</td>";
echo "<td>" . $row['productcomments'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['address'] . "</td>";
echo "<td>" . $row['age'] . "</td>";
echo "<td>" . $row['delivery'] . "</td>";
echo "<td><a href=\"delete.php?id=".$row['id']."\">delete</a></td>";

echo "</tr>";
}
echo "</table>";
?>

<?php
//delete.php
include('connection.php');
$id = $_GET['id']; //this needs to be sanitized 
if(!empty($id)){
    $result = mysql_query("DELETE FROM orderform WHERE id=".$id.";");
}
header("Location: index.php");
?>

First of all ,you need to send ID of completed order to next form. 首先,您需要将完成订单的ID发送到下一张表格。 You cam do this by adding: 您可以通过添加以下内容来做到这一点:

echo("<input type='hidden' name='orderID' value='".$row['id']."'/>");

That will create a hidden field with value of ID. 这将创建一个具有ID值的隐藏字段。

If your form uses POST: 如果您的表单使用POST:

then: 然后:

if(isset($_POST['submit'])){
$orderID = $_POST['orderID'];
mysql_query("DELETE FROM table WHERE id=$oderID");
}

If you are using GET method: 如果您使用的是GET方法:

<form method="GET">

You could either use hidden field as mentioned above or you could parse ID of order in GET url: 您可以如上所述使用隐藏字段,也可以解析GET网址中的订单ID:

<form action='action.php?id=".$row['id']."'>

and after submitting: 并提交后:

if(isset($_GET['submit']) && isset($_GET['id')){
$orderID = $_GET['id'];
mysql_query("DELETE FROM table WHERE id=$orderID");
}

Maybe something like this with PDO 也许用PDO这样的事情

<?php
include('connection.php');
?>
<form name="" action="delete.php" method="post"> 
    <table border='1' >
        <tr>
            <th>&nbsp;</th>
            <th><u>ID</th>
            <th><u>Date</th>
            <th><u>Product</th>
            <th><u>Product Comments</th>
            <th><u>Name</th>
            <th><u>Address</th>
            <th><u>Age</th>
            <th><u>Delivery</th>
            <th><u>Delete</th>
        </tr>
        <?php

        try {
            $conn = new PDO('mysql:host=localhost;dbname=myDatabase', $username, $password);
            $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);   

            $row = $conn->query('SELECT * FROM orderform');

            while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
                echo "<tr>";
                echo "<td><input type=\"checkbox\" name=\"id[]\" id=\"checkAll\" value=\"".$row['id']."\" /></td>" 
                echo "<td>" . $row['id']. "</td>";
                echo "<td>" . $row['date'] . "</td>";
                echo "<td>" . $row['product'] . "</td>";
                echo "<td>" . $row['productcomments'] . "</td>";
                echo "<td>" . $row['name'] . "</td>";
                echo "<td>" . $row['address'] . "</td>";
                echo "<td>" . $row['age'] . "</td>";
                echo "<td>" . $row['delivery'] . "</td>";
                echo "</tr>";
            }
        } catch(PDOException $e) {
            echo 'ERROR: ' . $e->getMessage();
        }

        ?>
    </table>
    <input type="submit" name="submit" value="submit" />
</form>

delete.php delete.php

<?php

$id 
if(isset($_POST['submit']) {


    include('connection.php');

    try {
        $conn = new PDO('mysql:host=localhost;dbname=myDatabase', $username, $password);
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);   

        $sql="DELETE FROM orderform WHERE id IN (".implode(',',$conn->quote($id)).")";
        $stmt->exec($sql);

    } catch(PDOException $e) {
        echo 'ERROR: ' . $e->getMessage();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM