我有一个搜索表单来搜索名字和姓氏。知道我不知道我怎么做才能得到两者的确切结果..
<form action="search1.php" method="GET">
<label for="firstname">Firstname</label>
<input type="name1" name="query" />
<label for="lastname">lastname</label>
<input type="lastname" name="query1" />
<input type="submit" value="Search" />
现在在数据库来宾中,我的名字为firstname,lastname = lastname。 因此,当有人搜索时,我希望它只显示两者是否匹配。
我的search1.php:
<?php
$query = $_GET['query'];
$query = $_GET['query1'];
// 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 guests WHERE name1 LIKE "$query" AND lastname LIKE "$query1";") 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)){
echo "<p><h3>".$results['title']."</h3>".$results['text']."</p>";
// posts results gotten from database(title and text) you can also show id ($results['id'])
}
}
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;
}
?>
&#13;
如果有人可以帮助我会很酷..我知道如何搜索1输入而不是2或3输入需要匹配?
答案 0 :(得分:0)
如果你想要完全匹配数据库的两个字段,那么你可以这样做
$raw_results = mysql_query("SELECT * FROM guests WHERE name1='".$query."' AND lastname='".$query1."';") or die(mysql_error());
答案 1 :(得分:0)
<form action="search1.php" method="GET">
<label for="firstname">Firstname</label>
<input type="name1" name="query" />
<label for="lastname">lastname</label>
<input type="lastname" name="query1" />
<input type="submit" value="Search" />
我的search1.php:
<?php
// gets value sent over search form
$query = $_GET['query'];
$query1 = $_GET['query1'];
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 guests WHERE name1 LIKE '".$query."' AND lastname LIKE '".$query1."' ") 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)){
echo "<p><h3>".$results['title']."</h3>".$results['text']."</p>";
// posts results gotten from database(title and text) you can also show id ($results['id'])
}
}
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;
}
?>