我试图在 PHP 中创建一个订单表,我可以在下拉列表框中为现有 sql 数据库中的每个订单选择司机和卡车。 我设法在表内创建了下拉列表,但我不知道如何更新数据库。 我正在尝试使用 isset 函数,但我可能没有做对。
这是我制作的代码+截图:
<!DOCTYPE html>
<html>
<head>
<title>Orders</title>
<link rel="stylesheet" type="text/css" href="table-general.css">
<style type="text/css">
body
{
font-family: Arial, Verdana, sans-serif;
font-size: 90%;
color: #666;
background-color: #f8f8f8;
}
</style>
</head>
<body>
<h1>Orders</h1>
<a href="orders.php">Done</a></br></br>
<table class="general">
<tr class="head">
<th>Order_ID</th>
<th>Customer_ID</th>
<th>Driver_ID</th>
<th>Truck_ID</th>
<th>Date</th>
<th>Project_Name</th>
<th>Project_Place</th>
<th>Amount</th>
</tr>
<?php
mysql_connect("localhost", "root", "");
mysql_select_db("login");
$result = mysql_query("select * from orders_table") or die("Failed".mysql_error());
$result2 = mysql_query("select * from trucks_table") or die("Failed".mysql_error());
if(mysql_num_rows($result2))
{
$select= '<select name="select">';
while($record2=mysql_fetch_array($result2))
{
$select.='<option value="'.$record2['TruckID'].'">'.$record2['TruckID'].'</option>';
}
}
$select.='</select>';
while($record = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $record['Order_ID'] . "</td>";
echo "<td>" . $record['Customer_ID'] . "</td>";
echo "<td>" . $record['Driver_ID'] . "</td>";
echo "<td>" . $select . "</td>";
if(isset($_POST['select']))
{
$t=$_POST['select'];
$sql = mysql_query("update orders_table set TruckID='$t' where Order_ID='".$record['Order_ID']."' ");
}
echo "<td>" . $record['Date'] . "</td>";
echo "<td>" . $record['Project_Name'] . "</td>";
echo "<td>" . $record['Project_Place'] . "</td>";
echo "<td>" . $record['Amount'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
</body>
</html>
最佳答案
您可能需要学习更多关于 HTTP FORMS 的知识,如何使用 POST 或 GET 从客户端向服务器发送数据,以及如何使用 PHP 在服务器上使用这些数据。
请参阅以下教程: http://www.w3schools.com/php/php_forms.asp
简而言之 - 要将数据从 HTML 页面发送到服务器,您需要将输入、选择、文本区域包装在
<form action="url"> </form>
其中 url 指向您的服务器 php 脚本,您在其中处理表单数据
那么你需要包括
<input type="Submit">
这将生成一个按钮,当用户点击该按钮时,数据将被发送回服务器
关于php - 如何在 PHP 中使用下拉列表框更新表中的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37140016/