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 用ucinet怎么计算子群体个数和核心边缘结构拟合度
  • ¥15 nrf52810-c三个a 程序
  • ¥15 lego-loam跑出来的roll误差很大
  • ¥50 求一个半透明没有锯齿的圆角窗体的实现例子
  • ¥15 STM32cubeMX里的FreeRTOS无法释放内存
  • ¥15 CATIA有些零件打开直接单机确定终止
  • ¥15 请问有会的吗,用MATLAB做
  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度