duanpang1987 2017-11-09 16:39
浏览 42
已采纳

在表单提交后使用数据库值更新前端表

I have this form which deletes a value from database on form submit and under it a table which shows all the values in the database.

But after the form submit the data table ("remove-table") is not being updated.

Removing works as intended but I would like to have the table to update automatically after the form submit.

<?php session_start(); ?>
<?php include 'head.php';?>

<div class="container">
    <div class="row">
        <div class="col-md-12">

        <form method="POST" action="#" class="remove-files-form">
            <div class="form-group">
                <span>ID (number): </span>
                <input class="form-control remove-control" id="removeTextField" type="text" size="8" name="product_id" placeholder="Insert ID here f.e. 1" required>
                <input class="form-control btn btn-primary"  id="removeButton" type="submit" value="Remove from database by id">
            </div>
        </form>

    <?php
        $mysqli = new mysqli($dbConn,  $dbUser,  $dbPass, $dbName);

        if ($mysqli->connect_errno) {
            printf("Connect failed: %s
", $mysqli->connect_error);
            exit();
        }

        $query = "SELECT id, thumbnailUrl, title, folderName, subfolder FROM MOCK_DATA ORDER by ID ASC";

        if ($result = $mysqli->query($query)) {

            echo "<div class='table-overflow-wrap'>
                  <table class='table remove-table'>";
            echo "<thead>
                      <tr>
                        <th>ID</th>
                        <th>Image</th>
                        <th>Image title</th>
                        <th>Main directory</th>
                        <th>Sub directory</th>
                      </tr>
                  </thead>";

            /* Fetch associative array */
            while ($row = $result->fetch_assoc()) {
                echo "<tr id='$row[id]'>";
                echo "<td><strong>$row[id]</strong></td>";
                echo "<td>
                        <div class='remove-table-thumb' style=\"background-image: url('$row[thumbnailUrl]') \">
                        </div>
                      </td>";
                echo "<td>$row[title]</td>";
                echo "<td>$row[folderName]</td>";
                echo "<td>$row[subfolder]</td>";
                echo "</tr>";   
            }
            echo "</table></div>";

            /* Free result set */
            $result->free();
        }
    ?>

      </div><!-- col -->
   </div><!-- row -->
</div><!-- container -->

<?php
    /* Delete data from database */
    /* File removal from directory todo */

    $sql = "DELETE FROM MOCK_DATA WHERE id=$_POST[product_id]";

    if ($mysqli->query($sql) === TRUE) {
        echo "
            <div class='alert alert-success alert-dismissable $_POST[product_id]'>
                <a href='#' class='close' data-dismiss='alert' aria-label='close'>&times;</a>
                 ID = $_POST[product_id], removed from database!
            </div>";
    }

    /* Close connection */
    $mysqli->close();
?>

<?php include 'footer.php';?>
  • 写回答

1条回答 默认 最新

  • dongping4273 2017-11-09 17:25
    关注

    Change the order and put the DELETE part before the SELECT.

    <?php 
    session_start();
    include 'head.php';
    
    if( $_SERVER['REQUEST_METHOD'] == "POST" && $_POST['product_id'] ) {
        /* Delete data from database */
        /* File removal from directory todo */
    
        $sql = "DELETE FROM MOCK_DATA WHERE id=$_POST[product_id]";
    
        if ($mysqli->query($sql) === TRUE) {
            echo "
            <div class='alert alert-success alert-dismissable $_POST[product_id]'>
                <a href='#' class='close' data-dismiss='alert' aria-label='close'>&times;</a>
                 ID = $_POST[product_id], removed from database!
            </div>";
        }
        else {
             // SHOW ERROR DELETE MESSAGE
        }
    }
    
    ?>
    
    <div class="container">
    <div class="row">
        <div class="col-md-12">
    
            <form method="POST" action="#" class="remove-files-form">
                <div class="form-group">
                    <span>ID (number): </span>
                    <input class="form-control remove-control" id="removeTextField" type="text" size="8" name="product_id" placeholder="Insert ID here f.e. 1" required>
                    <input class="form-control btn btn-primary"  id="removeButton" type="submit" value="Remove from database by id">
                </div>
            </form>
    
            <?php
            $mysqli = new mysqli($dbConn,  $dbUser,  $dbPass, $dbName);
    
            if ($mysqli->connect_errno) {
                printf("Connect failed: %s
    ", $mysqli->connect_error);
                exit();
            }
    
            $query = "SELECT id, thumbnailUrl, title, folderName, subfolder FROM MOCK_DATA ORDER by ID ASC";
    
            if ($result = $mysqli->query($query)) {
    
                echo "<div class='table-overflow-wrap'>
                  <table class='table remove-table'>";
                echo "<thead>
                      <tr>
                        <th>ID</th>
                        <th>Image</th>
                        <th>Image title</th>
                        <th>Main directory</th>
                        <th>Sub directory</th>
                      </tr>
                  </thead>";
    
                /* Fetch associative array */
                while ($row = $result->fetch_assoc()) {
                    echo "<tr id='$row[id]'>";
                    echo "<td><strong>$row[id]</strong></td>";
                    echo "<td>
                        <div class='remove-table-thumb' style=\"background-image: url('$row[thumbnailUrl]') \">
                        </div>
                      </td>";
                    echo "<td>$row[title]</td>";
                    echo "<td>$row[folderName]</td>";
                    echo "<td>$row[subfolder]</td>";
                    echo "</tr>";
                }
                echo "</table></div>";
    
                /* Free result set */
                $result->free();
            }
            ?>
    
        </div><!-- col -->
    </div><!-- row -->
    </div><!-- container -->
    
    <?php
    include 'footer.php';
    /* Close connection */
    $mysqli->close();
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵