gpt4 book ai didi

php - 如何在 php 中将一个表单元素值传递给下一个表单元素以进行 mysql 查询?

转载 作者:行者123 更新时间:2023-11-29 06:06:58 26 4
gpt4 key购买 nike

我正在尝试执行以下操作:

  1. 用户选择座位区
  2. 基于该区域的特定座位可用。

数据库和表已就位且正确。

我的问题是获取用户为区域选择的值并使用它来查询座位。这是代码:

<form method="post">
<?php

$sql = "select Name from Zone";
$handle = $conn->prepare($sql);
$handle->execute(array());
$res = $handle->fetchAll();
echo "<select name='Zone'>";
foreach($res as $row) {
echo "<option>".$row['Name']."</option>";
}
echo "</select>";
?>

<?php
$zone = $_POST['Zone'];
$sql = "select RowNumber, Zone from Seat WHERE Zone =" .$zone;
$handle = $conn->prepare($sql);
$handle->execute(array());
$conn = null;
$res = $handle->fetchAll();
echo "<select name='Seat'>";

foreach($res as $row) {
echo "<option>".$row['RowNumber']."</option>";

}
echo "</select>";

?>
</form>

这真的让我很沮丧,任何有助于取得突破的有用提示都会非常有帮助。提前致谢。

[编辑:因为这是一项评估,所以我选择将表单分解成其组成部分,并将提交时的数据从一个元素传递到下一个元素。即:

选择区域 -> 提交 ->使用选定区域查询数据库中的相关座位并填充下一个下拉列表。

我已经让这个方法起作用了。它很粗糙,但可以完成工作并且是我自己的想法(这是一个评估)。

然而,Craig 和 RamRaider 为遇到此挑战的任何其他人提供了更优雅的解决方案。]

最佳答案

考虑这个使用 HTML、PHP、MySQL 和一个平面 Javascript 命令的工作示例:

index.php

<?php
// Include the class that handles the database interactivity
require_once 'Database.php';
// Initialise the database
$Database = new Database();
// Get the list of Zones
$Zones = $Database->getZones();

// ZONE
if (isset($_POST['Zone'])) {
// You could validate and whitelist entries here if you wanted
if (!in_array($_POST['Zone'], $Zones)) {
$response = 'Sorry but that was not a valid selection';
}
// Passed validation
else {
// Get the corresponding Seats
$Seats = $Database->getSeats($_POST['Zone']);
// Store the Zone selection
$selectedZone = $_POST['Zone'];
// Set the response
$response = 'Viewing seats for '.$_POST['Zone'];
}
}
// SEAT
if (isset($_POST['Seat'])) {
printf('Zone that was chosen: '.$selectedZone);
printf('<br>');
printf('Seat that was chosen: '.$_POST['Seat']);
exit;
}
// This deals with initally loading the page
if (!isset($_POST['Zone']) && !isset($_POST['Seat'])) {
// Open variables
$response = '';
$selectedZone = 'n/a';
$Seats = array();
}

// You could move the code from here onwards into another file
// So you have a template like:
// require_once 'view.php'; which has a form that posts to index.php

// Start generating the page html
$page = '
<!DOCTYPE html>
<html>
<head>
<title>Awesome Page!</title>
</head>
<body>

<form method="POST" action="index.php">
';

// If theres a response to display
if (strlen($response) > 0) {
$page .= '
<p>'.$response.'</p>
';
}

// Dealing with the Zone selection
$page .= '
<p>Zones</p>
<select name="Zone" onchange="this.form.submit()">
<option value="">Please select an option</option>
';
// Itterate over the Zones
foreach ($Zones as $name) {
// If this is the selected Zone
if ($selectedZone == $name) {
$page .= '
<option selected value="'.$name.'">'.$name.'</option>
';
}
// This is not a selected Zone
else {
$page .= '
<option value="'.$name.'">'.$name.'</option>
';
}
}
$page .= '
</select>
';

// Dealing with the Seat selection
if (count($Seats) > 0) {
$page .= '
<p>Seats</p>
<select name="Seat" onchange="this.form.submit()">
<option value="">Please select an option</option>
';
// Itterate over the Seats
foreach ($Seats as $RowNumber) {
$page .= '
<option value="'.$RowNumber.'">Row Number: '.$RowNumber.'</option>
';
}
$page .= '
</select>
';
}
// Theres no Seats yet as Zone is not selected
else {
$page .= '
<p>Please select a Zone first.</p>
';
}
$page .= '
</form>

</body>
</html>
';

// Display the page
echo $page;

数据库.php

<?php
class Database
{
// Active connection
private $link;

// This fires when you call new Database();
public function __construct()
{
$this->doConnect();
}

private function doConnect()
{
// Define database details
$DBHost = 'localhost';
$DBUser = 'username';
$DBPass = 'password';
$DBName = 'database_name';
// Create a database connection for PHP to use
$this->link = mysqli_connect($DBHost, $DBUser, $DBPass);
// Preform from tasks to ensure the connection is active
if (!$this->link) {
echo 'Error: Unable to connect to MySQL' . '<br>';
echo 'Debugging errno: ' . mysqli_connect_errno() . '<br>';
echo 'Debugging error: ' . mysqli_connect_error() . '<br>';
exit;
}
// Sets encoding type to uft8
if (!mysqli_set_charset($this->link, 'utf8')) {
$this->processError();
}
// Set database that is in use (makes queries shorter to write)
if (!mysqli_select_db($this->link, $DBName)) {
$this->processError();
}
}

public function getZones()
{
// Stores the result
$Zones = array();
// Build query
$query = 'SELECT `name` ';
$query .= 'FROM `Zone` ';
// Prepare the statement
if (!$stmt = $this->link->prepare($query)) { $this->processError(); }
// Execute the query
if (!$stmt->execute()) { $this->processError(); }
// Bind variable to query values
if (!$stmt->bind_result($name)) { $this->processError(); }
// Itterate over the rows
while ($stmt->fetch()) {
// Add this Zones name to the result
$Zones[] = $name;
}
// Close the statement
$stmt->close();

// Return the result
return $Zones;
}

public function getSeats($selectedZone)
{
// Stores the result
$Seats = array();
// Build query
$query = 'SELECT `RowNumber` ';
$query .= 'FROM `Seat` ';
$query .= 'WHERE `Zone` = ? ';
// Prepare the statement
if (!$stmt = $this->link->prepare($query)) { $this->processError(); }
// Bind in form values to prevent sql injection
if (!$stmt->bind_param('s', $selectedZone)) { processError($link); } // NB: Assumed this to be a string but might be an integer, if so use i instead of s
// Execute the query
if (!$stmt->execute()) { $this->processError(); }
// Bind variable to query values
if (!$stmt->bind_result($RowNumber)) { $this->processError(); }
// Itterate over the rows
while ($stmt->fetch()) {
// Add this RowNumber to the Seats
$Seats[] = $RowNumber;
}
// Close the statement
$stmt->close();

// Return the result
return $Seats;
}

private function processError()
{
echo 'Error: Unable to connect to MySQL' . '<br>';
echo 'Debugging errno: ' . $this->link->errno . '<br>';
echo 'Debugging error: ' . $this->link->error . '<br>';
exit;
}
}

本质上,我们的 PHP 部分位于顶部,用于处理获取数据和表单提交,然后我们在下面有 html 模板,其中包括一个提交给自身的表单。

模板( View 类型的逻辑)将利用由数据库类(模型类型的逻辑)辅助的第一位( Controller 类型的逻辑)提供的数据。

所以这在本质上是 MVC 模式的一个非常简单的实现。

Javascript 的使用非常简单,可以检测选择值的变化,然后提交表单:onchange="this.form.submit()"

我尝试尽可能多地对代码进行注释,以增加您对代码的理解,但如果您有任何问题,请随时提问 :)

关于php - 如何在 php 中将一个表单元素值传递给下一个表单元素以进行 mysql 查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40892841/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com