php - 将数据库中的特定行加载到 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");
}
解决方案
您可以为您的解决方案使用以下代码块
将有两种获取数据的方法,一种是使用有点复杂的 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。之后,我们检查用户是否提交了表单,如果提交了,我们将获取数据并在表格上打印。
推荐阅读
- c# - C# Linq - 从 VALUE 开始按 ASC 排序
- azure - Azure Cosmos DB 建议
- angular - 单击链接会触发“未捕获(承诺中):错误:未找到‘未定义’的 NgModule 元数据”
- github - Github 公共仓库登录要求
- python - 附加具有多列索引和重叠列名的 DataFrame
- php - 如何使用 functions.php 作为启用 WP-PostRatings 的手段?
- xaml - Microsoft.CognitiveServices.Speech.Core.dll 未找到异常
- asp.net-core - .net 核心中的 Urlhelper 和 IActionContextAccessor
- java - 找不到类型类 java.lang.String 的 PersistentEntity
- python - 从一个目录中读取多个图像并将它们转换为 .csv 文件