繁体   English   中英

在PHP / HTML表中显示SQL表

[英]Display SQL table in PHP/HTML Table

我想让我的SQL表通过PHP填充到HTML表中。 但是,我只是创建了表头。 填充有什么问题? 这是我的代码:

 <?php
$con=mysqli_connect("server.com","username","password");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$result = mysqli_query($con,"SELECT * FROM Orders");

echo "<table>";
echo "<table border='1'>
<tr>
<th>ID</th>
<th>orderNumber</th>
<th>Price</th>
<th>customerName</th>
<th>salesRep</th>
<th>DatePicker</th>
<th>shipMethod</th>
<th>trackingNumber</th>
<th>Statuscheck</th>
<th>Edit</th>
</tr>";

while($row = mysqli_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['ID'] . "</td>";
  echo "<td>" . $row['orderNumber'] . "</td>";
  echo "<td>" . $row['Price'] . "</td>";
  echo "<td>" . $row['customerName'] . "</td>";
  echo "<td>" . $row['salesRep'] . "</td>";
  echo "<td>" . $row['DatePicker'] . "</td>";
  echo "<td>" . $row['shipMethod'] . "</td>";
  echo "<td>" . $row['trackingNumber'] . "</td>";
  echo "<td>" . $row['Statuscheck'] . "</td>";
  echo "<td>" . $row['Edit'] . "</td>";
  echo "</tr>";
  }
echo "</table>";

?>
</body>
</html>

“编辑”是这样,因此我可以拥有一个编辑超链接以使用SQL UPDATE。 它实际上不是数据库中SQL表的一部分。 任何帮助是极大的赞赏。 谢谢!

好的,这就是问题所在,编辑页面不会预填充已经填写的字段,更新按钮也不会实际更新数据库。 这是编辑页面的代码。

$query = "SELECT orderNumber, customerName, salesRep, DatePicker, shipMethod, trackingNumber, StatusCheck FROM Orders WHERE id = '$id'";
$result = @mysql_query($query);

mysql_fetch_object($result);
?>
<form name="update order" method="post" action="edit.php?a=edit&id=<? echo($ID) ?>&update=1">
  <table width="50%" border="0" cellspacing="0" cellpadding="0">
    <tr> 
      <td width="50%">Order Number</td>
      <td><input name="orderNumber" type="text" id="orderNumber" value="<? echo($row->orderNumber) ?>"></td>
    </tr>
    <tr> 
      <td>Customer Name</td>
      <td><input name="customerName" type="text" id="customerName" value="<? echo($row->customerName) ?>"></td>
    </tr>
    <tr> 
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr> 
      <td>Sales Rep</td>
      <td><input name="headline" type="text" id="headline" value="<? echo($row->headline) ?>"></td>
         <tr> 
      <td>Must Ship By</td>
      <td><input name="DatePicker" type="text" id="DatePicker" value="<? echo($row->DatePicker) ?>"></td>
         <tr> 
      <td>Shipping Method</td>
      <td><input name="shipMethod" type="text" id="shipMethod" value="<? echo($row->shipMethod) ?>"></td>
         <tr> 
      <td>Tracking Number</td>
      <td><input name="trackingNumber" type="text" id="trackingNumber" value="<? echo($row->trackingNumber) ?>"></td>
    </tr>
    <tr> 
      <td>Status</td>
      <td><input name="StatusCheck" type="radio" name="status" value="PROCESSING"> PROCESSING<br><input name="StatusCheck" type="radio" name="status" value="PROCESSING"> PICKED<br><input name="StatusCheck" type="radio" name="status" value="PROCESSING" value="<? echo($row->StatusCheck) ?>"> SHIPPED<br>  value="<? echo($row->StatusCheck) ?>"></td>
    </tr>
    <tr> 
      <td colspan="2"><div align="center">
          <input name="hiddenField" type="hidden" value="update">
          <input name="add" type="submit" id="add" value="Update">
        </div></td>
    </tr>
  </table>
  </form>
<?php

?>

您的$result变量为空,对其进行测试。 原因是因为您没有设置任何数据库名称,您可以从中获取信息。 粘贴此:

mysqli_select_db($con, 'your_database_name');

$con=mysqli_connect("server.com","username","password");

并且应该没问题(当然,如果那里有任何记录)。

您的代码嘘声相当冗长,因此,我将举一个较小的示例。 假设您有ID和名称的记录。 创建编辑/更新链接,将while循环更改为类似于以下内容的内容

while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['ID'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td><a href='editform.php?id=" . $row['ID'] . "'>Edit</a></td>";
echo "</tr>";
}

这会将编辑链接附加到所有行。 并按如下所示创建editform.php:

<?php

    if(isset($_POST["ID"])&&isset($_POST["name"])){
        //this is if the user is applying the edit
        $ID = $_POST["ID"];
        $name = $_POST["name"];

        //go ahead and save this detail on your db.....
    }else if(isset($_POST["ID"])){
        //this is if the user is just entering the editform from the table
        $ID = $_POST["ID"];
        $name = ;//get it from the data base with a query something like SELECT * FROM table WHERE id = $id;
    }else{
        //in case the user access this page directly
        $ID = "";
        $name = "";
    }

?>

<form method="POST" action="">
    <input type="text" name="ID" value="<?php echo $ID?>"/>
    <input type="text" name="name" value="<?php echo $name?>"/>    
    <input type="submit" />
</form>

您也可以在同一页面上处理更新,我建议这样做。 另外,您可以使用JavaScript创建编辑表单,但有些复杂。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM