我是网络开发的初学者。我创建了一个简单的表,显示我插入db中的注释的id,答案和日期。我需要一个简单的脚本,显示每个注释的删除按钮。我想知道如何获取我需要删除的每一行的id。 这是我的PHP代码:
<html>
<body>
<?php
include '..//config.php';
$result = mysql_query("SELECT * FROM **** Order by `id` ASC");
echo "<center><table width='400' border='1'>
<tr>
<th bgcolor='#D6D6D6' >ID</th>
<th bgcolor='#D6D6D6'>Answer</th>
<th bgcolor='#D6D6D6'>Date</th>
<th bgcolor='#D6D6D6'>Preview</th>
<th bgcolor='#D6D6D6'>Delete</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td bgcolor='#3399FF' scope='col width ='10%'>" . $row['id'] . "</td>";
echo "<td width ='10%'>" . $row['answer'] . "</td>";
echo "<td bgcolor='#3399FF' scope='col width ='10%'>" . $row['date'] . " </td>";
if ($row['hidden'] > 'http://www.********/upload/upload/') {
echo "<td width ='10%'><a href =" . $row['hidden'] . "><center>View</center></a></td>"; } else {echo "<td width ='10%'>" . $row[''] . "</td>";}
echo "<td width ='10%'><a href = 'dlt.php'>Delete</a></td>";
$row_id = $row['id'];
}
echo "</tr width ='10%'>";
echo "</table></center>";
?>
</body>
</html>
答案 0 :(得分:1)
从您的问题中不清楚您的要求是什么,但我假设您想知道当您点击“删除”链接(链接到{{}时如何通过条目的id
1}})。
您可以使用条目的dlt.php
将查询字符串参数传递给dlt.php
。
我已经清理了您的代码并在下面进行了修改:
id
然后在 dlt.php 文件中,您可以使用以下行检索要删除的<!Doctype html>
<html>
<body>
<?php
include '../config.php';
$result = mysql_query("SELECT * FROM **** Order by `id` ASC");
?>
<center>
<table width='400' border='1'>
<tr>
<th bgcolor='#D6D6D6'>ID</th>
<th bgcolor='#D6D6D6'>Answer</th>
<th bgcolor='#D6D6D6'>Date</th>
<th bgcolor='#D6D6D6'>Preview</th>
<th bgcolor='#D6D6D6'>Delete</th>
</tr>
<?php
while ($row = mysql_fetch_array($result)) {
$row_id = $row['id'];
?>
<tr>
<td bgcolor='#3399FF' scope='col' width='10%'><?php echo $row_id; ?></td>
<td width='10%'><?php $row['answer']; ?></td>
<td bgcolor='#3399FF' scope='col' width ='10%'><?php $row['date']; ?></td>
<td width ='10%'>
<?php
// I am not exactly sure what this line was meant to do in your code
if ($row['hidden'] > 'http://www.********/upload/upload/') {
echo "<a href='" . $row['hidden'] . "'><center>View</center></a>";
}
?>
</td>
<td width ='10%'><a href='dlt.php?id=<?php echo $row_id; ?>'>Delete</a></td>
</tr>
<?php
}
?>
</table>
</center>
</body>
</html>
:
id
由于您不熟悉Web开发(或一般开发),因此您应该对以下几点非常小心:
if (isset($_GET['id']) {
$delete_id = $_GET['id'];
}
else {
// no 'id' has been passed.
die('Nothing to delete!');
}
$result = mysql_query("DELETE FROM **** WHERE `id` = $delete_id");
函数已被弃用。您应该使用mysql_*
或mysqli
代替。我会提供更多积分,但这份名单实际上可能会很长。这些应该对初学者有好处。