【问题标题】:Identificate row's id and delete the object from db识别行的 id 并从 db 中删除对象
【发布时间】:2012-12-09 18:45:34
【问题描述】:

我是网络开发的初学者。我制作了一个简单的表格,显示了我插入到数据库中的笔记的 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>

【问题讨论】:

    标签: mysql delete-row corresponding-records


    【解决方案1】:

    你的问题不是很清楚你的要求是什么,但我假设你想知道当你点击“删除”链接(链接到dlt.php)时如何通过条目的id .

    您可以使用条目的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>
    

    然后在您的 dlt.php 文件中,您可以使用以下行检索要删除的 id

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

    由于您是 Web 开发(或一般开发)的新手,因此您应该非常小心以下几件事:

    1. 编写带有适当缩进的干净代码。它可以帮助您快速发现和调试问题。
    2. 编写符合 W3C 标准的代码。
    3. 了解您要使用的技术 API 并注意已弃用的函数。例如,在 PHP 中 mysql_* 函数已被弃用。您应该改用mysqliPDO

    我会加分,但列表实际上可能会很长。这些应该适合初学者。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-09
      • 2019-01-10
      • 2019-03-28
      • 1970-01-01
      • 2021-01-31
      相关资源
      最近更新 更多