我一直在苦苦挣扎,试图弄清楚如何将以下查询与HTML搜索表单连接起来。
查询:
<?php
include_once("mysqli_connection.php");
// startpage
$page = 0;
//items pr page
$per_page= 8;
//smileyscore
$smiley_score='';
// perform database query
$query = "SELECT * ";
$query .="FROM smile AS a, smiley_detail AS b ";
$query .= "WHERE a.smiley_id = b.smiley_id AND b.smiley >= 1 ";
$query .= "ORDER BY b.date ";
$query .= "LIMIT ". $page.', '. $per_page . ";";
$result = mysqli_query($connnetion, $query);
$row = mysqli_fetch_row($result);
$rows = $row[0];
if (!$result){
die("database query failed.");
}
?>
形式:
<div class="row">
<div class="col-xs-12">
<form class="menu" action="<?=$PHP_SELF?>" id="searchfor" method="post">
<select type="value" name="smiley_score" placeholder="s">
<option value="0">Alle stikprøver (0)</option>
<option value="1">Ingen anmærkninger (1)</option>
<option value="2">Indskærpelse (2)</option>
<option value="3">Påbud eller forbud (3)</option>
<option value="4">Bødeforlæg, politianmeldelse, autorisation eller registrering frataget (4)</option>
</select>
<select type="text" name="city">
<option value="">By</option>
<option value="København">København</option>
<option value="Århus">Århus</option>
<option value="Odense">Odense</option>
<option value="Alle de andre">Alle de andre</option>
</select>
<select type="value" name="year">
<option value="">År</option>
<option value="2001">2001</option>
<option value="2002">2002</option>
<option value="2003">2003</option>
<option value="2004">2004</option>
<option value="2005">2005</option>
<option value="2006">2006</option>
<option value="2007">2007</option>
<option value="2008">2008</option>
<option value="2009">2009</option>
<option value="2010">2010</option>
<option value="2011">2011</option>
<option value="2012">2012</option>
<option value="2013">2013</option>
<option value="2014">2014</option>
<option value="2015">2015</option>
</select>
<input type="text" name="searchcriteria" class="form-control" placeholder="Indtast søgekriterier"> </input>
<button class="btn" id="submit_button" type="submit" name="submit" value="Submit"> Søg</button>
</form>
</div>
</div>
回声:
<div class="container">
<div class="row">
<?php
while($row = mysqli_fetch_assoc($result)){
// output data from each row
echo '<div class="col-md-12">';
echo '<div class="overskrift">';
echo '<h3>';
echo $row["organisation"] . "<br/>";
echo '</h3>';
echo 'Branche: '.$row["type"] . " - ";
echo 'Dato: '. $row["date"] . " <br /> ";
echo '</div >';
echo '<p class="adress">';
echo $row["adresse1"] . "<br /> ";
echo $row["zip"] . " ";
echo $row["postby"] . " ";
echo '<img class="score" src="img/'.$row["smiley"] .'.gif">';
echo '</p> ';
echo '<div class="textfield" >';
echo $row["text"] . "<br />";
echo '</div>';
echo '</div>';
}
// release data
mysqli_free_result($result);
?>
</div>
答案 0 :(得分:0)
有几种方法可以做到这一点。在包含表单的页面上,如下所示的伪结构可能就是答案。
<html>
<head><title>Searches and results....</title><head>
<body>
<?php
if( $_SERVER['REQUEST_METHOD']=='GET' ){
/* Display the form here */
} elseif( $_SERVER['REQUEST_METHOD']=='POST' ) {
/* Construct and run the sql query */
/* Display the results here */
} else {
/* Wrong method - warn user or redirect or whatever */
}
?>
</body>
</html>
答案 1 :(得分:0)
您需要处理数据,您可以通过向$ PHP_SELF添加使用$ _POST关联数组的逻辑来获取提交表单上的数据,表单元素的名称是数组中使用的。
例如,如果你有一个名为data的元素,那么访问它的PHP就是$ _POST ['data']。
答案 2 :(得分:0)
以数组形式创建条件。将方法更改为get方法。 像这样自定义您的查询
$sql = mysql_query("SELECT * FROM smile WHERE smiley_id LIKE '%$_GET[term]%' LIMIT $page,$_GET[results]");