初学者:从PHP/HTML/MySQL表和数据库中删除行
完整的PHP/MySQL和HTML新手,所以请容忍我,如果我没有正确解释自己。目前,我有一个订单表单,它将订单存储在我的数据库中,并显示在我的管理区域的表中。我希望在完成订单后能够从表和数据库中删除该行 目前,我的代码如下所示:初学者:从PHP/HTML/MySQL表和数据库中删除行,php,mysql,sql,Php,Mysql,Sql,完整的PHP/MySQL和HTML新手,所以请容忍我,如果我没有正确解释自己。目前,我有一个订单表单,它将订单存储在我的数据库中,并显示在我的管理区域的表中。我希望在完成订单后能够从表和数据库中删除该行 目前,我的代码如下所示: <?php //87229feely.php include('connection.php'); $result = mysql_query("SELECT * FROM orderform"); echo "<table border='1' >
<?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");
?>
有什么帮助吗?非常感谢大家。谢谢你有很多方法可以做到这一点。既然你是一个初学者,这可能是最直截了当的(尽管我不会这么做)
delete
列上添加一个按钮,并为其分配一个id
(id应该是数据库中的id)
此答案不符合安全标准,但应满足以下要求:
<?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");
?>
首先,您需要将已完成订单的ID发送到下一张表单。为此,可以添加:
echo("<input type='hidden' name='orderID' value='".$row['id']."'/>");
如果您使用的是GET方法:
<form method="GET">
也许PDO也会这样
<?php
include('connection.php');
?>
<form name="" action="delete.php" method="post">
<table border='1' >
<tr>
<th> </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();
}
}
是因为您不知道如何使用MySql和PHP删除记录,还是因为您不知道在某个UI操作发生后如何执行此操作?我不知道如何删除我会说谷歌删除MySql。也没那么难。另外,当您更高级时,请使用PDO或MySQLi而不是mysql查询。注意:第4行/web/users/l1071039/bobbin/deleterow.php中的未定义索引:id警告:无法修改标题信息-标题已由发送(输出从/web/users/l1071039/bobbin/connection.php:7开始)在第8行的/web/users/l1071039/bobbin/deleterow.php中,行正在删除,但它不会将我发送回87229feely.php?现在,87229feely.php需要与deleterow.php位于同一文件夹中。在调用header()
函数之前,还要确保没有输出任何内容(回显、打印、错误等)
<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");
}
<?php
include('connection.php');
?>
<form name="" action="delete.php" method="post">
<table border='1' >
<tr>
<th> </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>
<?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();
}
}