douni1396 2019-04-21 17:02
浏览 1178
已采纳

如何使按钮修改数据库中的数据

I have a page which connects to the database and gets some data including title and content.
I use a while loop to echo the data inside this page, what I'm trying to do is to have 2 buttons under every result that comes from database which is the "Edit" and "Delete" buttons. The problem is I don't know how to make buttons remove or edit the right column.
EDIT
Added the PHP part:

$sql = "SELECT * FROM posts ORDER BY date DESC, time DESC $limit";
$query = mysqli_query($conn, $sql);
<?php
while ($row = mysqli_fetch_array($query, MYSQLI_ASSOC)) {
    ?>
    <section>
        <b>Title: </b>
        <p><?php echo $row['name'] ?></p><br>
        <b>Content: </b><br>
        <p><?php echo $row['content'] ?></p><br>
//want to have two buttons here which can be used to edit or delete the colunm that they are under it
    </section>
    <?php
}
  • 写回答

1条回答 默认 最新

  • doutu7123 2019-04-21 18:11
    关注

    Assuming you have an id column in posts table i.e. you have a unique id value associated with each row, create a <form>...</form> block encapsulating both Edit and Delete buttons, like this:

    $sql = "SELECT * FROM posts ORDER BY date DESC, time DESC $limit";
    $query = mysqli_query($conn, $sql);
    <?php
    while ($row = mysqli_fetch_array($query, MYSQLI_ASSOC)) {
        ?>
        <section>
            <b>Title: </b>
            <p><?php echo $row['name'] ?></p><br />
            <b>Content: </b><br>
            <p><?php echo $row['content'] ?></p><br />
            <form action="YOURPAGE.php?id=<?php echo $row['id']; ?>" method="post">
                <input type="submit" name="edit" value="Edit" />&nbsp;
                <input type="submit" name="delete" value="Delete" />
            </form>
        </section>
        <?php
    }
    

    So once you hit Edit or Delete button of a particular row, the form will get submitted and you will be able get that particular row id using $_GET['id'] on YOURPAGE.php page. Based on that row id, perform the edit or delete operation accordingly.

    YOURPAGE.php

    <?php
        /* get row id */
        $id = isset($_GET['id']) ? $_GET['id'] : null;
    
        if(isset($id) && isset($_POST['edit'])){
            /* Edit button has been clicked */
    
            /* create a prepared statement */
            $stmt = mysqli_prepare($conn, "SELECT name, content FROM posts WHERE id = ?")
            if($stmt){
                /* bind parameters */
                mysqli_stmt_bind_param($stmt, "i", $id);
    
                /* execute query */
                mysqli_stmt_execute($stmt);
    
                /* bind result variables */
                mysqli_stmt_bind_result($stmt, $name, $content);
    
                /* fetch value */
                mysqli_stmt_fetch($stmt);
    
                /* close statement */
                mysqli_stmt_close($stmt);
    
                ?>
                <form action="" method="post">
                    <b>Title: </b><input type="text" name="title" value="<?php echo $name; ?>" /><br />
                    <b>Content: </b><br />
                    <textarea name="content" ><?php echo $content; ?></textarea><br />
                    <input type="submit" name="editrow" value="Edit" />
                </form>
                <?php
            }else{
                /* error */
            }
        }
    
        if(isset($id) && isset($_POST['editrow'])){
            $title = $_POST['title'];
            $content = $_POST['content'];
    
            /* edit row details based on the row id i.e. $id */
        }
    
        if(isset($id) && isset($_POST['delete'])){
            /* Delete button has been clicked */
            /* Delete row based on the row id i.e. $id */
        }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

    报告相同问题?

    悬赏问题

    • ¥15 Pycharm无法自动补全,识别第三方库函数接收的参数!
    • ¥15 STM32U575 pwm和DMA输出的波形少一段
    • ¥30 android百度地图SDK海量点显示标题
    • ¥15 windows导入environment.yml运行conda env create -f environment_win.yml命令报错
    • ¥15 这段代码可以正常运行,打包后无法执行,在执行for内容之前一直不断弹窗,请修改调整
    • ¥15 C语言判断有向图是否存在环路
    • ¥15 请问4.11到4.18以及4.27和4.29公式的具体推导过程是怎样的呢
    • ¥20 将resnet50中的卷积替换微ODConv动态卷积
    • ¥15 通过文本框输入商品信息点击按钮将商品信息列举出来点击加入购物车商品信息添加到表单中
    • ¥100 这是什么压缩算法?如何解压?