dongtangu6144 2013-09-24 17:56
浏览 218
已采纳

从表中显示的数据库中插入特定行的删除按钮

Hello I have the following code displaying a database results, in the 3th TD of the table I would like to insert delete button which will delete the table record of the data which is next to the button, what should be the code in the 3th TD of the tabe for the delete button ?

<?php
$con=mysqli_connect("localhost","table","password","database");
if (mysqli_connect_errno())
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM recetas_galletas");
echo "<table border='1'>
<tr>
    <th>Title</th>
    <th>Description</th>
</tr>";

while($row = mysqli_fetch_array($result))
{
    echo "<tr>";
    echo "<td>" . $row['title'] . "</td>";
    echo "<td>" . $row['description'] . "</td>";
    echo "<td>" . DELETE BUTTON "</td>";
    echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
  • 写回答

3条回答 默认 最新

  • duanjiashai9140 2013-09-24 18:25
    关注

    There are several ways to archive this, first and and most important is that you need a field on your database table where you can identify the record you want to delete, for example a primary key in form of an ID or a unique key.

    You can do this by creating a link with a text to a delete.php page or you can use JQuery and AJAX or you can use a inner form.

    You will also want to have only authorized users to use those pages so you will need a login page with session as well.

    You can see here an example of login page with sessions.

    The simplest one is a link to the delete page, see example here:

    <?php
    $con = mysqli_connect("localhost","table","password","database");
    // Check connection
    if (mysqli_connect_errno())
    {
        die("Failed to connect to MySQL: " . mysqli_connect_error());
    }
    
    if (!$result = mysqli_query($con,"SELECT * FROM recetas_galletas"))
    {
        die("Error: " . mysqli_error($con));
    }
    ?>
    <table border='1'>
    <tr>
    <th>Title</th>
    <th>Description</th>
    </tr>
    <?php
    while($row = mysqli_fetch_array($result))
    {
    ?>
    <tr>
    <td><?php echo $row['title']; ?></td>
    <td><?php echo $row['description']; ?></td>
    <td><a href="delete.php?id=<?php echo $row['id']; ?>">Delete</a></td>
    </tr>
    <?php
    }
    mysqli_close($con);
    ?>
    </table>
    

    Then on your delete page you would have something like this:

    <?php
    // Your database info
    $db_host = '';
    $db_user = '';
    $db_pass = '';
    $db_name = '';
    
    if (!isset($_GET['id']))
    {
        echo 'No ID was given...';
        exit;
    }
    
    $con = new mysqli($db_host, $db_user, $db_pass, $db_name);
    if ($con->connect_error)
    {
        die('Connect Error (' . $con->connect_errno . ') ' . $con->connect_error);
    }
    
    $sql = "DELETE FROM recetas_galletas WHERE id = ?";
    if (!$result = $con->prepare($sql))
    {
        die('Query failed: (' . $con->errno . ') ' . $con->error);
    }
    
    if (!$result->bind_param('i', $_GET['id']))
    {
        die('Binding parameters failed: (' . $result->errno . ') ' . $result->error);
    }
    
    if (!$result->execute())
    {
        die('Execute failed: (' . $result->errno . ') ' . $result->error);
    }
    
    if ($result->affected_rows > 0)
    {
        echo "The ID was deleted with success.";
    }
    else
    {
        echo "Couldn't delete the ID.";
    }
    $result->close();
    $con->close();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 MATLAB中streamslice问题
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序