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

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

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 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法