美好的一天!我只是php编程的新手,所以我需要有人来帮助我。我仍然无法找到这段代码的错误。我创建了一个带有下拉菜单的搜索表单来过滤结果。但是当我进行搜索时没有任何反应。它只是刷新页面或显示错误标准消息。任何答案将不胜感激! :) 提前致谢!这是代码:
<html>
<head>
<basefont face="Arial">
</head>
<body>
<?php
error_reporting(E_ALL);
if (!isset($_POST['Submit'])) {
// form not submitted
?>
<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
search
<input type="text" name="search">
<select size="1" name="dropdown">
<option value="" selected>search By...</option>
<option value="first">Company</option>
<option value="last">Address</option>
</select>
<input type="Submit" value="Submit" name="Submit">
</form>
<?php
}
else {
// Server Variables
$host = "localhost";
$user = "mdti";
$pass = "tnet";
$db = "ojt";
$search = empty($_POST['search'])? die ("ERROR: Enter search Criteria") : mysql_escape_string($_POST['search']);
$dropdown = empty($_POST['dropdown'])? die ("ERROR: Select from dropdown") : mysql_escape_string($_POST['dropdown']);
// Open Connection
$connect = mysql_connect($host, $user, $pass) or die ("Unable to connect to host");
//Select Database
mysql_select_db($db) or die ("Unable to connect to database");
//Create query
$query = "SELECT arCompanyname, arAddress FROM ar WHERE $dropdown like'$search'" or die (mysql_error());
$result = mysql_query($query) or die (mysql_error());
$num=mysql_numrows($result);
mysql_close($connect);
echo "<b><center>Database Output</center></b><br><br>";
$i=0;
while ($i < $num) {
$company=mysql_result($result,$i,"arCompanyname");
$address=mysql_result($result,$i,"arAddress");
echo "<br>Company: $company<br><br>Address: $address<hr><br>";
$i++;
}
}
?>
</body>
</html>
答案 0 :(得分:0)
查看代码,最初的想法是:
change like'$search' with like '%$search%'
change mysql_numrows to mysql_num_rows
check the dropdwon option values 'arCompanyname' and 'arAddress' matching your field names.
change <?=$_SERVER['PHP_SELF']?> to <?php echo $_SERVER['PHP_SELF']; ?>
答案 1 :(得分:0)
您在不打开mysql_real_escape_string
的情况下致电mysql_connect
..您需要调用mysql_real_escape_string
代码
// Open Connection
$connect = mysql_connect($host, $user, $pass) or die ("Unable to connect to host");
$search = empty($_POST['search'])? die ("ERROR: Enter search Criteria") : mysql_real_escape_string($_POST['search']);
$dropdown = empty($_POST['dropdown'])? die ("ERROR: Select from dropdown") : mysql_real_escape_string($_POST['dropdown']);
您还需要更改选择选项值
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
search
<input type="text" name="search">
<select size="1" name="dropdown">
<option value="" selected>search By...</option>
<option value="arCompanyname">Company</option>
<option value="arAddress">Address</option>
</select>
<input type="Submit" value="Submit" name="Submit">
</form>
并使用
更改您的查询SELECT arCompanyname, arAddress FROM ar WHERE $dropdown LIKE '%$search%'
答案 2 :(得分:0)
检查更新的代码:
更新了搜索查询,mysql_numrows - &gt; mysql_num_rows,添加mysql_fetch_array进行循环。
尝试使用mysqli_*函数编写脚本。因为mysql_ *函数将被弃用。
<html>
<head>
<basefont face="Arial">
</head>
<body>
<?php
error_reporting(E_ALL);
if (!isset($_POST['Submit'])) {
// form not submitted
?>
<form action="" method="post">
search
<input type="text" name="search">
<select size="1" name="dropdown">
<option value="" selected>search By...</option>
<option value="first">Company</option>
<option value="last">Address</option>
</select>
<input type="Submit" value="Submit" name="Submit">
</form>
<?php
}
else {
// Server Variables
$host = "localhost";
$user = "mdti";
$pass = "tnet";
$db = "ojt";
// Open Connection
$connect = mysql_connect($host, $user, $pass) or die ("Unable to connect to host");
$search = empty($_POST['search'])? die ("ERROR: Enter search Criteria") : mysql_escape_string($_POST['search']);
$dropdown = empty($_POST['dropdown'])? die ("ERROR: Select from dropdown") : mysql_escape_string($_POST['dropdown']);
//Select Database
mysql_select_db($db) or die ("Unable to connect to database");
//Create query
$query = "SELECT arCompanyname, arAddress FROM ar WHERE $dropdown like '%$search%'";
$result = mysql_query($query) or die (mysql_error());
$num=mysql_num_rows($result);
if($num > 0) {
echo "<b><center>Database Output</center></b><br><br>";
while ($row = mysql_fetch_array($result)) {
$company = $row['arCompanyname'];
$address = $row['arAddress'];
echo "<br>Company: $company<br><br>Address: $address<hr><br>";
}
} else {
echo "No rows found";
}
mysql_close($connect);
}
?>
</body>
</html>
答案 3 :(得分:0)
使用“mysql_real_escape_string”
$search = empty($_POST['search'])? die ("ERROR: Enter search Criteria") : mysql_real_escape_string($_POST['search']);
$dropdown = empty($_POST['dropdown'])? die ("ERROR: Select from dropdown") : mysql_real_escape_string($_POST['dropdown']);
将'$ search'更改为'%$ search%'
$query = "SELECT arCompanyname, arAddress FROM ar WHERE $dropdown like'%$search%'" or die (mysql_error());
将mysql_numrows更改为mysql_num_rows