我正在尝试创建一个下拉菜单来查询 mysql 数据库中的信息。我已经完成了这项工作,现在在@jeroen 的帮助下,我想我现在可以将选定的对象传递给namesearch16.php。该网页位于此处: http ://swapabook.hostei.com/search16.php
没有错误,但我希望它输出与查询匹配的数据库中的行。
我返回字符串:array(1) { ["select1"]=> &string(4) "book" }
我有一种感觉,问题是我的 while 声明我返回了错误的东西。顺便说一句,我的表有 5 个字段,我想返回其中的 4 个字段,例如选择书籍、作者、流派、书籍中的电子邮件
初始文件的代码如下:
<form action="namesearch16.php" method="post">
Name of Book
<?php
mysql_connect("mysql1x.000webhost.com","a4425533_swapabo","xxxxx") or die("Connection Failed");
mysql_select_db('a4425533_swapabo')or die("Connection Failed");
$query = "SELECT * FROM book";
$result = mysql_query($query);
?>
<select name="select1">
<?php
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
?>
<option value="<?php echo $line['book'];?>"> <?php echo $line['book'];?> </option>
<?php
}
?>
</select>
<input type="submit">
</form>
根据@jeroen 的输入,将第二个文件更改为以下文件:
<?php
$con=mysqli_connect('mysql1x.000webhost.com','a4425533_swapabo','xxxx','a4425533_swapabo');
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$namequery="SELECT * FROM book WHERE book = ?";
$namestmt = mysqli_prepare($con, $namequery);
mysqli_stmt_bind_param($namestmt, "s", $_POST['select1']);
var_dump($_POST);
mysqli_stmt_execute($namestmt);
if ($result = mysqli_query($con, $namequery)) {
while($row = mysqli_fetch_array($result))
{
echo $row['book'];
echo "<br>";
echo "got as far as the while loop";
}
}
?>