[英]PHP, SQL populate info from database based on drop down selection menu
我設法將 SQL 數據庫中的數據拉入下拉選擇菜單並添加了一個提交按鈕。 但是,當用戶單擊提交時,我正在努力從其他(甚至相同)表中填充來自 DB(例如,ID)的更多數據。
我希望用戶能夠從下拉菜單中選擇 customerName 並提交。 單擊“提交”按鈕時,應從數據庫中填充並顯示鏈接到特定 customerName(例如-Rob)的其他(例如年齡、ID)詳細信息。 請幫忙。 我是 php 的新手,花了幾個小時在這個愚蠢的事情上。 我只被允許使用 php、sql 和 html。
非常感謝。
dropdown.php - 原始版本(無效)
<?php
include "db.php"; // Database connection
$sql="SELECT customerName FROM customers";
echo "<select name=customers value=''>Customer Name</option>";
foreach ($conn->query($sql) as $row){
echo "<option value=$row[customerName]>$row[customerName]</option>";
}
echo "</select>";
?>
<form id= method="post" action = "iNeedHelpWithThisFile.php">
<input type="submit" value="SUBMIT"/>
</form>
</body>
dropdown.php - 更新版本
<?php
?>
<form id= method="post" action = "iNeedHelpWithThisFile.php">
<select name="customerName">Customer Name</option>
<?php
include "db.php";
$sql="SELECT `customerName` FROM `customers`";
foreach ($conn->query($sql) as $row){
echo "<option value=\"".$row['customerName']."\">".$row['customerName']."</option>";
}
?>
</select>
<input type="submit" value="SUBMIT"/>
</form>
數據庫文件
<?php
// to connect database
define('DB_NAME', 'clients');
define('DB_USER', 'root');
define('DB_PASSWORD', '');
define('DB_HOST', 'localhost');
$conn = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) or
die ("Cannot connect");
echo "Connected to Clients";
?>
iNeedHelpWithThisFile.php
echo 語句-年齡、名稱、id 沒有被執行
<?php
echo "<pre>";
print_r($_REQUEST);
echo "</pre>";
include('db.php');
echo "<br />";
$customerName = $_POST['submit'];
if(array_key_exists('customerName', $_REQUEST ))
{
// not sure if there's an issue with intval..?
$customerName = intval($_REQUEST['customerName']);
echo("Customer: $customerName <br />");
if ($customerName)
{
$sql = "SELECT * FROM customers where customerName= ?"; //
// $conn -defined in db.php
if($stmt = mysqli_prepare($conn, $sql))
{
mysqli_stmt_bind_param($stmt, "i", $customerName);
mysqli_stmt_execute($stmt);
$queryresult = mysqli_stmt_get_result($stmt);
}
else {
echo "Could not fetch results!";
}
if ($queryresult)
{
if ($currentrow = mysqli_fetch_assoc($queryresult)) {
$name = $currentrow['customerName'];
$ID = $currentrow['customerID'];
$age = $currentrow['age'];
// these statements are not being executed
echo "Customer Name: $name <br />";
echo "Customer ID: $ID <br />";
echo "Age: $age <br />";
}
}
else {
echo "<p>Customer not in the database</p>";
}
}
}
?>
總而言之 :
<form id= method="post" action = "iNeedHelpWithThisFile.php">
<select name="customers">Customer Name</option>
<?php
include "db.php";
$sql="SELECT `customerName` FROM `customers`";
foreach ($conn->query($sql) as $row){
echo "<option value=\"".$row['customerName']."\">".$row['customerName']."</option>";
}
?>
</select>
<input type="submit" value="SUBMIT"/>
</form>
或者,如果您喜歡簡短的 php 標簽:
<php
include "db.php";
$sql="SELECT `customerName` FROM `customers`";
?>
<form id="main" method="post" action = "iNeedHelpWithThisFile.php">
<select name="customers">Customer Name</option>
<?php foreach ($conn->query($sql) as $row){ ?>
<option value="<?=$row['customerName'];?>"><?=$row['customerName'];?></option>
<?php } ?>
</select>
<input type="submit" value="SUBMIT"/>
</form>
如果您還想同時提交年齡和ID以及客戶姓名,您可以像這樣:
<php
include "db.php";
$sql="SELECT `age`, `ID`, `customerName` FROM `customers`";
?>
<form id= method="post" action = "iNeedHelpWithThisFile.php">
<select name="customers">Customer Name</option>
<?php foreach ($conn->query($sql) as $row){
$conc=base64_encode($row['age']."|".$row['ID']."|".$row['customerName']); ?>
<option value="<?=$conc;?>"><?=$row['customerName'];?></option>
<?php } ?>
</select>
<input type="submit" value="SUBMIT"/>
</form>
在iNeedHelpWithThisFile.php 中,你可以這樣
$c = strip_tags(htmlspecialchars($_POST['customers']);
$c = base64_decode($c);
$name = explode("|",$c)[2];
$ID = explode("|",$c)[1];
$age = explode("|",$c)[0];
// these statements are not being executed
echo "Customer Name: $name <br />";
echo "Customer ID: $ID <br />";
echo "Age: $age <br />";
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.