我有这个在另一个网站上工作,但修改它给我带来了问题,我看不出它认为哪里有额外的参数。
我稍后会添加 Javascript,但我需要能够从下拉菜单中进行选择并从数据库中打印相应的信息。我的PHP代码如下:
include('definitions.php');
$con=mysqli_connect(DB_HOST,DB_USER,DB_PASS,DB_NAME);
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
echo "These are the matches for the search";
echo "<br>";
$query="SELECT name,email,town,location,gmapslink FROM users WHERE `town` = '?'";
$stmt = mysqli_prepare($con, $query);
mysqli_stmt_bind_param($stmt, "s", $_POST['select1']);
var_dump($_POST);
mysqli_stmt_execute($stmt);
/* bind variables to prepared statement */
mysqli_stmt_bind_result($stmt, $col2, $col3, $col5, $col8, $col9);
while(mysqli_stmt_fetch($stmt))
{
printf("Name:%s\t Email:%s\t Town:%s\t Location:%s\t Google Maps Link:%s\t", $col2, $col3, $col5, $col8, $col9);
echo "<br>";
}
/* close statement */
mysqli_stmt_close($stmt);
/* close connection */
mysqli_close($con);
?>
我从网站上得到的回复如下。这些是搜索的匹配项:
PHP 错误信息:
警告:mysqli_stmt_bind_param() [function.mysqli-stmt-bind-param]:变量数与第 18 行 /home/a5839517/public_html/search.php 中准备好的语句中的参数数不匹配
免费虚拟主机:
array(3) { ["county"]=> string(15) "爱尔兰以外" ["select1"]=> string(12) "Ballincollig" ["select"]=> NULL }
town
在数据库上运行 SQL 代码可以正常工作,但在这种情况下,要使其正常工作,我必须在and周围添加引号?
。
任何想法我的问题是参数似乎都是正确的。
从调用上述 php 的原始文件中添加了编辑:
<form action="search.php" method="post">
County <select name="county">
<option> Outside Ireland </option>
<option> Antrim </option>
<option> Armagh </option>
</select>
<br>
Name of Book
<?php
mysql_connect(DB_HOST,DB_USER,DB_PASS) or die("Connection Failed");
mysql_select_db(DB_NAME)or die("Connection Failed");
$query = "SELECT DISTINCT town FROM users";
$result = mysql_query($query);
?>
<select name="select1">
<?php
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
?>
<option value="<?php echo $line['town'];?>"> <?php echo $line['town'];?> </option>
<?php
}
?>
</select>
<input type="submit">
</form>
上面的代码已更正,以在反引号中包含 select1 语句和列名。