我创建了一个搜索栏,但是我的php代码无法获取输入,以下是我的代码,我从表单中的输入标签中获取了输入,但是 "item "这个输入没有被php获取,并且显示一个错误 Undefined index: item and Undefined variable: item。
<?php
if( $_POST['item'] !== ''){
$con = mysqli_connect('localhost','root','');
$db = mysqli_select_db($con,'search');
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<link rel="stylesheet" href="StyleSheet1.css" />
<link rel="stylesheet" href="https://pro.fontawesome.com/releases/v5.10.0/css/all.css" integrity="sha384-AYmEC3Yw5cVb3ZcuHtOA93w35dYTsvhLPVnYs9eStHfGJvOvKxVfELGroGkvsg+p" crossorigin="anonymous"/>
<script src="https://use.fontawesome.com/a49d136066.js"></script>
<title>search bar</title>
</head>
<body>
<form action="search.php" meathod="POST" id="searchForm">
<div class="search-box">
<input class="search-txt" type="text" name="item" id="searchBox" placeholder="Type to Search" />
<a class="search-btn" href="#">
<i style="font-size:20px;" class="fab fa-searchengin">
</i></a>
<?php
$query = mysqli_query($con,"SELECT * FROM products where title LIKE '%$item%' OR description LIKE '%$item%' ");
$num_rows = mysqli_num_rows($query);
while($row = mysqli_fetch_array($query)){
$id = $row['id'];
$title = $row['title'];
$dec = $row['description'];
echo '<h3>' . $title . '</h3><p>' . $dec . '</p></br>';
}
?>
</div>
</form>
</body>
</html>
<?php } ?>
试着像这样声明你的变量。由于你已经检查了$_POST['item']是否为空,你可以这样操作来验证变量是否被设置并存在。然而, search.php
文件到底在做什么?你能把它附加到你的文章吗?
if(isset($_POST['item'])){
$con = mysqli_connect('localhost','root','');
$db = mysqli_select_db($con,'search');
$item = $_POST['item'];
}else{
$item = null;
}
?>