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

时间:2014-03-20 15:08:27

标签: php mysql sql

完成PHP / MySQL和HTML的新手,所以如果我没有正确解释自己,请耐心等待。目前我有一个订单表格,它将订单存储在我的数据库中,并在我的管理区域的表格中显示。我希望能够在完成订单后从表和我的数据库中删除该行。

目前我的代码如下:

<?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");
    ?>

有任何帮助吗?非常感谢。感谢

4 个答案:

答案 0 :(得分:0)

许多方法都可以做到这一点。既然你是初学者,这可能是最直接的(虽然我不怎么做)

  1. 围绕表格创建表单
  2. 在每个行的delete列上添加一个按钮,并为其分配id(ID应该是数据库中的ID)<input type="submit" name="submit" value"/*YOUR ID*/">
  3. 在解析表之前添加一些处理脚本
  4. if (isset($_POST['submit'])) {
        $sql = "DELETE FROM table WHERE id='/*YOUR ID*/'";
        mysql_query($sql);
    }
    

答案 1 :(得分:0)

这个答案不符合安全标准,但应该完成这项工作:

<?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");
?>

答案 2 :(得分:0)

首先,您需要将已完成订单的ID发送到下一个表单。你可以通过添加:

来做到这一点
echo("<input type='hidden' name='orderID' value='".$row['id']."'/>");

这将创建一个ID值的隐藏字段。

如果您的表单使用POST:

然后:

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

如果您使用的是GET方法:

<form method="GET">

您可以使用上面提到的隐藏字段,也可以在GET网址中解析订单ID:

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

并在提交后:

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

答案 3 :(得分:0)

也许像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

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