首页 > 解决方案 > 将数据库中的特定行加载到 html 表中

问题描述

如您所见,我已经开发了一个 html/php 页面来创建订单,并且在尝试将数据从表(MySQL)加载到我在 html 中创建的表中时遇到问题。我想要的是用户将 ID(标识)数字化为辅助键,例如订单号,然后整行进入我的表。他可以在其中编辑、删除和保存该行。到目前为止,我有表格,并且能够在 html 页面中加载整个 tabe。

HTML:

<p>What order number would you like to load?<input type="text" id="SKU" maxlength="10" size="10" placeholder="Search for SKU..." title="Type an SKU..."><button onclick="myCreateFunction()">LOAD</button></p>
<table id="itemsTable"> <tr> ID <th>Quantity</th> 
                                              <th>Item</th> 
                                            <th>SKU</th> 
                                            <th>Item Name</th> 
                                            <th>Item Price</th> 
                                            <th>Subtotal</th> 
                                            <th>Cartons Scanned</th> 
                                            <th>Individually Scanned</th> 
                                            <th>Current Inventory</th> 
                                            <th>Location Selected</th> 
                                            <th>Image</th> 
                                            <th>Edit</th>
                                    </tr> 
                        <tr>
                              
                        </tr> 
</table> 

现在的php:

<?php
//Step2
$query = "SELECT * FROM item_new WHERE SKU=input ";
mysqli_query($db, $query) or die('Error querying database.');
$result = mysqli_query($db, $query);
$all_property = array();  //declare an array for saving property

//showing property
echo '<table id="itemsTable" class="data-table">
        <tr class="data-heading">';  //initialize table tag
while ($property = mysqli_fetch_field($result)) {
    echo '<td>' . $property->name . '</td>';  //get field name for header
    array_push($all_property, $property->name);  //save those to array
}
echo '</tr>'; //end tr tag

//showing data selected by the user (input)
while ($row = mysqli_fetch_array($result)) {
    echo "<tr>";
    foreach ($all_property as $item) {
        echo '<td>' . $row[$item] . '</td>'; //get items using property value
    }
    echo '</tr>';
}
echo "</table>";

数据库表

脚本

<script>
function myCreateFunction() {
  var input 
  input = document.getElementById("SKU");
  
}

标签: phphtmlmysql

解决方案


您可以为您的解决方案使用以下代码块

将有两种获取数据的方法,一种是使用有点复杂的 AJAX,另一种是使用表单。

<p>What order number would you like to load?</p>
<form type="post" action=''>
    <input type="text" name='input_field' id="SKU" maxlength="10" size="10" placeholder="Search for SKU..." title="Type an SKU...">
    <input type='submit' name='search'>
</form>
<table id="itemsTable">                 
    <tr> 
        <th>ID</th> 
        <th>Quantity</th> 
        <th>Item</th> 
        <th>SKU</th> 
        <th>Item Name</th> 
        <th>Item Price</th> 
        <th>Subtotal</th> 
        <th>Cartons Scanned</th> 
        <th>Individually Scanned</th> 
        <th>Current Inventory</th> 
        <th>Location Selected</th> 
        <th>Image</th> 
        <th>Edit</th>
    </tr> 

<?php

//Check if form is submitted
if(isset($_POST['search'])){
    $query = "SELECT * FROM item_new WHERE SKU='".$_POST['input_field']."'";
    mysqli_query($db, $query) or die('Error querying database.');
    $result = mysqli_query($db, $query);
    $all_property = array(); 

    //showing property
    while ($property = mysqli_fetch_field($result)) {
        array_push($all_property, $property->name);  //save those to array
    }

    //showing data selected by the user (input)
    while ($row = mysqli_fetch_array($result)) {
        echo "<tr>";
        foreach ($all_property as $item) {
            echo '<td>' . $row[$item] . '</td>'; //get items using property value
        }
        echo "<a href='myeditpage?id=".$_row['id']."' >Edit product</a>"
        echo '</tr>';
    }

}

?>
</table>

在上面的例子中,我们首先让用户使用 input 输入数据,当用户点击搜索时,它会将表单提交到当前 URL。之后,我们检查用户是否提交了表单,如果提交了,我们将获取数据并在表格上打印。


推荐阅读