我在网站上创建了一个简单的搜索栏,当我尝试获取结果时,它告诉我没有结果,我认为我的代码有误,我已经花了很长时间了但是运气不好,下面我将发布HTML,confing.php和search.php。
HTMML搜索:
<form id="form" name="search" method="post" action="search.php">
<input type="text" name="search" id="search" placeholder="search..." />
<button class="w3-button w3-small w3-ripple" type="submit" name="submit" value=""><i class="fa fa-search"></i></button>
</form>
Eventhough连接正常,这里是config.php
<?php
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', 'BimL1st!');
define('DB_DATABASE', 'myDB');
$conn = mysqli_connect(DB_SERVER, DB_USERNAME, DBPASSWORD, DB_DATABASE);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
这是search.php
require 'config.php';
$conn = mysqli_connect(DB_SERVER, DB_USERNAME, DBPASSWORD, DB_DATABASE);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected";
$search = "";
if ($_SERVER["REQUEST_METHOD"] == "POST"){
$search = test_input($_POST['search']);
}
function test_input ($data){
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
$min_length = 0;
if (strlen($data) >= $min_length) {
$sql = "SELECT * FROM Registry WHERE Business_Name like '%$data%' OR Business_Address like '%$data%'
OR Name like '%$data%' OR phone like '%$data%' OR Email like '%$data%' OR Category like '%$data%'
OR Recomend like '%$data%' OR Services like '%$data%' OR Expertise like '%$data%'";
echo '<br>';
echo "Search completed";
echo '<br>';
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
echo "Business Name: " . $row["Business_Name"]. "Business Address: " . $row["Business_Address"].
"Person in charge: " . $row["Name"]. "Phone number: " .$row["phone"]. "Email: " .$row["Email"].
"Business Category: " .$row["Category"]. "Recomended by: " .$row["Recomend"]. "Services Rendered: " .$row["Services"].
"What sets us appart: " .$row["Expertise"]. "<br>";
}
} else {
echo "0 Results";
}
} else {
echo "invalid length";
}
答案 0 :(得分:0)
您的$sql
声明LIKE '%$data%'
也许您可以尝试吸引他们
LIKE '%".$data."%'