gpt4 book ai didi

php - 使用 PHP 从 SQL 数据库传递选择值

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

我正在尝试使用一个选择表单,该表单包含从 SQL 数据库填充的字段。数据库中有一个商店表,我使用这种方法将那些不同的字段放入选择工具中。

echo '<select name="StoreName" />'."\n";
while ($row = mysqli_fetch_assoc($distinctResult)){

echo '<option value="'.$row["StoreID"].'">';
echo $row["StoreName"];
echo '</option>'."\n";

};

echo '</select>'."\n";

正确的商店名称显示在选择工具上,但是当我提交表单数据时,表单的值没有正确传递。在初始页面中:

<?php


require_once 'login.php';

$connection = mysqli_connect(
$db_hostname, $db_username,
$db_password, $db_database);
if(mysqli_connect_error()){
die("Database Connection Failed: " .
mysqli_connect_error() .
" (" . mysqli_connect_errno() . ")"
);
};

$query = "SELECT * from PURCHASE";
//echo $query;
$result = mysqli_query($connection,$query);
if(!$result) {
die("Database Query Failed!");
};

$distinct = "SELECT DISTINCT StoreName FROM PURCHASE";


$distinctResult = mysqli_query($connection,$distinct);
if(!$result) {
die("Database Query Failed!");
};

echo '<title>Output 1</title>';
echo '</head>';
echo '<body>';
echo '<h1>Required Output 1</h1>';
echo '<h2>Transaction Search</h2>';
echo '<form action="output1out.php" method="get">';
echo 'Search Store:<br/>';
echo '<br/>';


echo '<select name="StoreName" />'."\n";
while ($row = mysqli_fetch_assoc($distinctResult)){

echo '<option value="'.$row["StoreID"].'">';
echo $row["StoreName"];
echo '</option>'."\n";

};

echo '</select>'."\n";

echo '<input name="Add Merchant" type="submit" value="Search">';
echo '</form>';

mysqli_free_result($result);

mysqli_close($connection);


?>

这是输出页面:

<?php

$transaction = $_REQUEST["StoreName"];

require_once 'login.php';

$connection = mysqli_connect(
$db_hostname, $db_username,
$db_password, $db_database);



$sql = "SELECT * FROM PURCHASE WHERE StoreName LIKE '%".$transaction."%'";
$result = $connection->query($sql);

?>

Purchases Made From <?php echo $transaction ?>
<table border="2" style="width:100%">
<tr>
<th width="15%">Item Name</th>
<th width="15%">Item Price</th>
<th width="15%">Purchase Time</th>
<th width="15%">Purchase Date</th>
<th width="15%">Category</th>
<th width="15%">Rating</th>
</tr>
</table>
<?php
if($result->num_rows > 0){
// output data of each row
while($rows = $result->fetch_assoc()){ ?>
<table border="2" style="width:100%">
<tr>
<td width="15%"><?php echo $rows['ItemName']; ?></td>
<td width="15%"><?php echo $rows['ItemPrice']; ?></td>
<td width="15%"><?php echo $rows['PurchaseTime']; ?></td>
<td width="15%"><?php echo $rows['PurchaseDate']; ?></td>
<td width="15%"><?php echo $rows['Category']; ?></td>
<td width="15%"><?php echo $rows['Rating']; ?></td>
</tr>
<?php
}
}
?>

我在输出页面中调用 select name StoreName 但它没有传递下拉列表中的值。我需要在第二页上使用该术语运行查询,以便为它提取相关数据。如果有人能看到我做错了什么,请提出建议。谢谢。

最佳答案

评论回答:

您正在执行 SELECT DISTINCT StoreName FROM PURCHASE$row["StoreID"] 不在您的选择集中。

  • 将其添加到您的 SELECT。

关于php - 使用 PHP 从 SQL 数据库传递选择值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37057778/

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