這個網路應用程式的功能是:從下拉串列中選擇一個客戶(下拉串列的值是從資料庫中自動彈出的),它會在結果頁面上列印選擇的客戶名稱和它的郵政編碼。
當我從下拉串列中選擇客戶名稱并單擊提交按鈕時,結果頁面僅列印 $customerv 值(第一個回顯),但未列印 $result 值(第二個回顯)。客戶名稱在資料庫中是唯一的。
索引.php:
<?php
require_once('config.php');
?>
<!DOCTYPE HTML>
<html>
<form action="result.php" method="post">
Customer:<br>
<select Customer id="customer" name="Customer">
<option value="">--- Select Customer ---</option>
<?php
$sql = "SELECT b.BPName from BP b where b.BPCode like 'C%' Order by b.BPName";
$customer = mysqli_query($conn, $sql);
while ($cat = mysqli_fetch_array(
$customer,
MYSQLI_ASSOC
)) :;
?>
<option value="<?php echo $cat['BPName']; ?>">
<?php echo $cat['BPName']; ?>
</option>
<?php
endwhile;
?>
</select>
<input type="submit" value="Submit">
</form>
</html>
配置.php:
<?php
$servername = "localhost";
$username = "xxx";
$password = "xxx";
$databse = "xxx";
$conn = new mysqli($servername, $username, $password, $databse);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
結果.php:
<table>
<?php
require_once('config.php');
$customerv = $_POST['Customer'];
echo $customerv;
$sql = "SELECT shiptozipcode FROM BP WHERE BPName ='$customerv'";
$result = $conn->query($sql);
echo $result;
?>
</table>
uj5u.com熱心網友回復:
查詢結果本身不是頁面“可列印”的東西。它不僅僅是一個單一的值,它是一個復雜的物件。您需要從結果中獲取記錄。例如:
$result = $conn->query($sql);
while ($row = $result->fetch_assoc()) {
echo $row["shiptozipcode"];
}
如果您確定只有一行(無論如何添加一些錯誤檢查仍然是個好主意),那么您不需要回圈:
$result = $conn->query($sql);
$row = $result->fetch_assoc();
echo $row["shiptozipcode"];
但無論哪種方式,您都需要從結果集中提取資料。(如果您更喜歡物件語法而不是陣列語法,也可以使用fetch_object()而不是。)fetch_assoc()
順便說一句,請注意您的查詢對SQL 注入是開放的。現在是學習如何糾正它的好時機。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/492739.html
上一篇:致命錯誤:未捕獲的型別錯誤:不支持的運算元型別:字串*字串
下一篇:Laravel產量為空
