我正在使用我要存储的表单中的选择值,在提交表单时,会显示一个表格,其中包含我的sql查询结果,但使用选择值来查询数据库。我目前的html在这里:
<body>
<div id="top-menu">
<ul>
<li><a class="active" href="">Profile</a></li>
<li><a href="">Favourites</a></li>
<li><a href="">Messages</a></li>
<li id="mainheading" style="align left"><a href="Home.html"><strong>BookSmart</strong></a></li>
</div>
<div id ="search_elements">
<form method="post" action="test.php">
<img src="UniSelect.jpeg">
<select name ="university">
<option selected disabled>Select a university</option>
<option value="ucl">UCL</option>
<option value="kings">Kings College</option>
<option value="imperial">Imperial College</option>
<option value="lse">London School of Economics</option>
</select>
<img height="250px", width="600px" src="PriceSelect.jpeg">
<select name="rent">
<option selected disabled>Select a weekly rent price</option>
<option value="50">0-£50</option>
<option value="100"> £100-£150</option>
<option value="150">£150-200</option>
<option value="200"> £200+</option>
</select>
<img height="250px", width="600px" src="RoomSelect.jpeg">
<select name="roomtype">
<option selected disabled>Room Type</option>
<option value="50">University Halls</option>
<option value="100"> Studio</option>
<option value="150">Flat</option>
<option value="200"> Shared Accomodation</option>
</select>
<img height="250px", width="600px" src="DistanceSelect.jpeg">
<select name="distance">
<option selected disabled> Selecet a distance from the university</option>
<option value="1">0-1 miles</option>
<option value="3"> 1-3 miles</option>
<option value="5">3-5 miles</option>
<option value="6"> 5+ miles</option>
</select>
<input type="submit" name="submit" value="Search">
</form>
</div>
</body>
我目前用于查询数据库并在表中显示结果的代码是:
<p style ="text-align: center" id="php_style">
<?php
error_reporting(E_ERROR | E_PARSE);
session_start();
$counter_name = "counter.txt";
// Check if a text file exists. If not create one and initialize it to zero.
if (!file_exists($counter_name)) {
$f = fopen($counter_name, "w");
fwrite($f,"0");
fclose($f);
}
// Read the current value of our counter file
$f = fopen($counter_name,"r");
$counterVal = fread($f, filesize($counter_name));
fclose($f);
// Has visitor been counted in this session?
// If not, increase counter value by one
if(!isset($_SESSION['hasVisited'])){
$_SESSION['hasVisited']="yes";
$counterVal++;
$f = fopen($counter_name, "w");
fwrite($f, $counterVal);
fclose($f);
}
echo nl2br ("You are visitor number ".$counterVal. " to this site \n Today's date is ". date("d/m/Y"));
$con=mysqli_connect("localhost:8889","root","root","booksmart_properties");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
else
{
echo "we connected";
}
if(isset($_POST['submit']
{
$university = $_POST['university'];
$rent = $_POST['rent'];
$roomtype = $_POST['roomtype'];
$distance = $_POST['distance'];
//Now you can do anything with variables , you can put them inside query like this : //
// $result=mysqli_query($con,"SELECT * FROM Table WHERE Field = '$universit' "); //
// Note : this is dangrous way to do a query in PHP you should prevent something called SQL Injection , search for it //
// Perform queries
$result=mysqli_query($con,"SELECT * FROM ListedProperties
WHERE PropertyType = '.$roomtype' ");
echo "<table border='1'>
<tr>
<th>PropertyType</th>
<th>Description</th>
<th>Rent Price</th>
<th>Location</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['PropertyType'] . "</td>";
echo "<td>" . $row['Description'] . "</td>";
echo "<td>" . $row['RentPrice'] . "</td>";
echo "<td>" . $row['Location'] . "</td>";
echo "</tr>";
}
echo "</table>";
}))
mysqli_close($con);
?>
我想知道select表中的值如何存储为php变量并插入到查询中以便在按下搜索按钮时使用。
答案 0 :(得分:0)
您可以使用此链接http://www.w3schools.com/tags/att_form_method.asp
之类的表单方法并使用此链接将我的sql插入数据库 http://www.w3schools.com/php/php_mysql_insert.asp
答案 1 :(得分:0)
好的很简单,首先你必须给每个Select标签一个这样的名字: 我将为一个人做,你为其他人做这件事:
<select name="university">
<option selected disabled>Select a university</option>
<option value="ucl">UCL</option>
<option value="kings">Kings College</option>
<option value="imperial">Imperial College</option>
<option value="lse">London School of Economics</option>
</select>
在为每个SELECT
标记提供一个名称后,现在输入的名称如下:
<input type="submit" name="submit" value="Search">
现在是时候从表单接收值并将它们保存在PHP变量中。
$con=mysqli_connect("localhost:8889","root","root","booksmart_properties");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
else
{
echo "we connected";
}
if(isset($_POST['submit')
{
$university = $_POST['university'];
$rent = $_POST['rent']; // i assumed that you name the second Select tag RENT and the Third ROOMTYPE and the last DISTANCE
$roomtype = $_POST['roomtype'];
$distance = $_POST['distance'];
//Now you can do anything with variables , you can put them inside query like this : //
// $result=mysqli_query($con,"SELECT * FROM Table WHERE Field = '$universit' "); //
// Note : this is dangrous way to do a query in PHP you should prevent something called SQL Injection , search for it //
// Perform queries
$result=mysqli_query($con,"SELECT * FROM ListedProperties");
echo "<table border='1'>
<tr>
<th>PropertyType</th>
<th>Description</th>
<th>Rent Price</th>
<th>Location</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['PropertyType'] . "</td>";
echo "<td>" . $row['Description'] . "</td>";
echo "<td>" . $row['RentPrice'] . "</td>";
echo "<td>" . $row['Location'] . "</td>";
echo "</tr>";
}
echo "</table>";
}
mysqli_close($con);
?>
将来会帮助您的重要链接和主题: