这是我的问题。我有一个只想按名称搜索的数据库。如果有相似的名称,我希望它弹出并提供选择哪个名称的选项。完成并单击搜索后,它会进入 search.php 文件...我想显示结果,但 GET 方法似乎不起作用。这是我的表格。
<form action="search.php" method="GET">
<input type="text" name="query" />
<input type="submit" value="Search" />
这是我的 PHP 搜索
<?php
mysql_connect("localhost", "dbname", "password") or die("Error connecting to database: ".mysql_error());
/*
localhost - it's location of the mysql server, usually localhost
root - your username
third is your password
if connection fails it will stop loading the page and display an error
*/
mysql_select_db("ambassador") or die(mysql_error());
/* tutorial_search is the name of database we've created */
$query = $_GET['query'];
// gets value sent over search form
$min_length = 3;
// you can set minimum length of the query if you want
if(strlen($query) >= $min_length){ // if query length is more or equal minimum length then
$query = htmlspecialchars($query);
// changes characters used in html to their equivalents, for example: < to >
$query = mysql_real_escape_string($query);
// makes sure nobody uses SQL injection
$raw_results = mysql_query("SELECT * FROM member
WHERE (`Name` LIKE '%".$query."%')") or die(mysql_error());
if(mysql_num_rows($raw_results) > 0){ // if one or more rows are returned do following
while($results = mysql_fetch_array($raw_results)){
}
}
else{ // if there is no matching rows do following
echo "No results";
}
}
else{ // if query length is less than minimum
echo "Minimum length is ".$min_length;
}
?>
然后我尝试使用这种方法从数据库中获取结果。
<?php echo $_GET['Name']; ?>
感谢你的帮助!