This is the code of search.php - this part display the search textbox and search button. will be get data form database.
<?php
//Just declaration here for null
$search_term = '';
search_results = false;
//Check if search data was submitted
if (isset($_GET['s'])) {
// Include the search class
require_once( dirname(__FILE__) . '/class-search.php' );
// Instantiate a new instance of the search class
$search = new search();
// Store search term into a variable
$search_term = $_GET['s'];
// Send the search term to our search class and store the result
$search_results = $search->search($search_term);
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Search</title>
</head>
<body>
<h1>Search</h1>
<div class="search-form">
<form action="" method="get">
<div class="form-field">
<label for="search-field">Search</label>
<b> <input type="search" name="s" placeholder="Enter your search term..." results="5" value="<?php echo $search_term; ?>"> </b>
<input type="submit" value="Search">
</div>
</form>
</div>
<?php
if ($search_results) :
?>
<div class="results-count">
<p>
<?php
echo $search_results['count'];
?> results found
</p>
</div>
<div class="results-table">
<?php
foreach ($search_results['results'] as $search_result) :
?>
<div class="result">
<p>
<?php
echo $search_result->cust_ic;
?>
</p>
</div>
<?php
endforeach;
?>
</div>
<?php
endif;
?>
</body>
</html>
This is the code of class-search.php
class search {
private $mysqli;
public function __construct() {
$this->connect();
}
private function connect() {
$this->mysqli = new mysqli( 'localhost', 'root', 'root', 'abc' );
}
public function search($search_term) {
$sanitized = $this->mysqli->real_escape_string($search_term);
// Run the query
$query = $this->mysqli->query("
SELECT *
FROM customer_info
WHERE cust_name LIKE '%{$sanitized}%'
OR cust_ic LIKE '%{$sanitized}%'
");
// Check results
if ( ! $query->num_rows ) {
return false;
}
// Loop and fetch objects
while( $row = $query->fetch_object() ) {
$rows[] = $row;
}
// Build our return result
$search_results = array(
'count' => $query->num_rows,
'results' => $rows,
);
return $search_results;
}
}
I get this error code:
: Undefined variable: search_term in on line :第行的未定义变量:search_term
Notice: Undefined variable: search_results in C:\\xampp\\htdocs\\search\\search.php on line 34
When I run search.php i get 2 errors. I try click search,after search no more error appear. any ideas for this solution? Thank you
Juste declare your variables :
<?php
//Declaration
$search_term = '';
search_results = false;
//Check if search data was submitted
if (isset($_GET['s'])) {
// Include the search class
require_once( dirname(__FILE__) . '/class-search.php' );
// Instantiate a new instance of the search class
$search = new search();
// Store search term into a variable
$search_term = $_GET['s'];
// Send the search term to our search class and store the result
$search_results = $search->search($search_term);
}
?>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.