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();
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 有偿,谁有移远的EC200S固件和最新的Qflsh工具。
  • ¥15 找一个QT页面+目标识别(行人检测)的开源项目
  • ¥15 有没有整苹果智能分拣线上图像数据
  • ¥20 有没有人会这个东西的
  • ¥15 cfx考虑调整“enforce system memory limit”参数的设置
  • ¥30 航迹分离,航迹增强,误差分析
  • ¥15 Chrome Manifest扩展引用Ajax-hook库拦截请求失败
  • ¥15 用Ros中的Topic通讯方式控制小乌龟的速度,走矩形;编写订阅器代码
  • ¥15 LLM accuracy检测
  • ¥15 pycharm添加远程解释器报错