页面设置为运行简单的数据库查询。表单和提交按钮呈现,但是当我键入搜索项时,它会返回错误。当我在下拉菜单中使用它来调用查询时,数据库连接脚本运行良好,所以我不确定为什么它在这里不起作用。
我编辑了我的原始帖子以删除MySQL并将其替换为如下:我现在遇到的问题是错误是说我在第28行有一个未定义的变量if if($ result-> num_rows&gt ; 0){
<?php
$servername = "localhost";
$username = "xxx";
$password = "xxx";
$dbname = "oldga740_SeniorProject";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$output= '';
//collect
if(isset($_POST['search'])) {
$searchq = $_POST['search'];
$searchq = preg_replace("#[^0-9a-z]#i","",$searchq);
$query = "SELECT * FROM Customer WHERE Client LIKE '%$searchq%'";
$result = $conn->query($query);
}
//Display results
if ($result->num_rows > 0) {
echo $result;
}
else {
echo "0 results";
}
?>
<html>
<head>
</head>
<body>
<form action="Index.php" method="post">
<input type="text" name="search" placeholder="Search...." />
<input type="submit" value=">>" />
</form>
</body>
</html>
答案 0 :(得分:-1)
请替换您的查询
$query = mysql_query("SELECT * FROM Customer WHERE Client LIKE '%$searchq%'") or die("could not search");
通过
$query = mysql_query("SELECT * FROM Customer WHERE Client LIKE '%".$searchq."%'") or die("could not search");