我设置了以下 php 页面来查询我的数据库并将结果保存到会话变量数组中:
<?php
include 'newCustomer.php';
connect('final');
$query = $_POST['searchDB'];
$query = htmlspecialchars($query); // stop HTML characters
$query = mysql_real_escape_string($query); //stop SQL injection
$data = mysql_query("SELECT * FROM customer WHERE First_Name LIKE '$query'") ;//query the DB with search field in colleumn selected//
if($data === FALSE) {
$error = 'Query error:'.mysql_error();
echo $error;
}
else
{
$test = array();
$colNames = array();
while($results = mysql_fetch_assoc($data)){// puts data from database into array, loops until no more
$test[] = $results;
}
$anymatches=mysql_num_rows($data); //checks if the querys returned any results
if ($anymatches != 0) {
$_SESSION['names']=$test;
$colNames = array_keys(reset($test));
}
if ($anymatches == 0)
{
echo "Sorry, but we can not find an entry to match your query<br><br>";
}
}
?>
...以及在不同页面上使用会话变量中的结果创建组合框的以下内容:
<?php
if (isset($_SESSION['names'])){
echo "Customers";
$array1 = $_SESSION['names'];
echo'<select name="customers>';
foreach($array1 as $name){
echo'<option value="'.$name.'">'.$name.'</option>';
}
echo'</select>';
}
?>
在主页上按下按钮时会调用查询页面。它创建了组合框。然而,它只是一遍又一遍地填满了“数组”这个词。为什么它不显示我的数据库中 First_Name 的内容?
谢谢,