php - 如何在 PHP 中使用下拉列表框更新表中的数据?

标签 php mysql phpmyadmin

我试图在 PHP 中创建一个订单表,我可以在下拉列表框中为现有 sql 数据库中的每个订单选择司机和卡车。 我设法在表内创建了下拉列表,但我不知道如何更新数据库。 我正在尝试使用 isset 函数,但我可能没有做对。

这是我制作的代码+截图:

screen

<!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/

相关文章:

php - 使用 DB::raw 和 Eloquent 使用相同查询的不同结果

mysql - phpMyAdmin 数据库导出文件大小随每次试验而变化

php - 意外丢失 session 数据

php - Laravel Horizo​​n 不执行挂起的作业 - Kubernetes 和 Docker 环境

php - 一种在 php 中将值连接到字符串的简单方法

PHP MySQL 信息更新不起作用

php - 防止重复行插入 php/mysql

mysql - 如何使用从 phpmyadmin 中的 "json DEFAULT NULL' fufnction 生成的 SQL 文件修复导入时的 MySQL 错误 1064,错误接近 'Export'

php - 我的mysql表结构应该如何?

php - 使用 Ghostscript 删除图像中的白色边框