問題描述
我目前正在研究一些 PHP 并且我有 3 個文本輸入.在 MySQL 數據庫中搜索這些值,并應返回與輸入條件相對應的任何數量的結果.
這里是搜索表單:
我目前正在研究一些 PHP 并且我有 3 個文本輸入.在 MySQL 數據庫中搜索這些值,并應返回與輸入條件相對應的任何數量的結果.
這里是搜索表單:
<div class='container'><input type='submit' name='Submit' value='Search'/>
</fieldset></表單>
這是它在動作中鏈接的 PHP 腳本:
0){//附加條件$query .= " WHERE " .內爆 (' AND ', $conditions);//您可以更改為或",但我建議應用累積過濾器}$result = mysqli_query($mysqli_link, $query) 或 die(mysql_error());mysqli_close($mysqli_link);if(isset($_POST['submit'])) {while($row = mysqli_fetch_assoc($result)) {$C_Name = $row['C_Name'];$C_StreetNumber = $row['C_StreetNumber'];$C_StreetName = $row['C_StreetName'];$C_Postcode = $row['C_Postcode'];$C_County = $row['C_County'];$C_Tele = $row['C_Tele'];$C_Website = $row['C_Website'];$Contact_Forename = $row['Contact_Forename'];$Contact_Surname = $row['Contact_Surname'];$Contact_Email = $row['Contact_Email'];$Job_Type = $row['Job_Type'];$Job_Price = $row['Job_Price'];echo "<b>名稱:$C_Name</b><br>街道號碼:$C_StreetNumber<br>街道名稱:$C_StreetName<br>郵政編碼:$C_Postcode<br>縣:$C_County<電話>$C_Tele<br>網站:$C_Website<br>聯系人姓名:$Contact_Forename $Contact_Surname<br>電子郵件:$Contact_Email<br>工作類型:$Job_Type<br>工作價格<>>工作價格:$Job>;}}}?>
由于某種原因它返回有
<塊引用>意外的文件結尾
" 但是我已經檢查了代碼并且當我在最后添加另一個 '}' 時,所有代碼都被正確關閉(從我所看到的)腳本根本不返回任何內容.任何人都知道為什么這會發生嗎?
來源:使用表單中的多個字段搜索 MySQL 數據庫
因為忘記關閉
if(isset($_POST['submit'])) {//你沒有關閉條件
文件末尾
只需在文件末尾添加 }
I'm currently working on a bit of PHP and I've 3 text inputs. The values are searched in the MySQL database and should return whatever amount of results correspond with the entered criteria.
here is the search form:
<form id='SearchPersonal' method='post' action='businessUsersSearch.php' accept-charset='UTF-8'>
<fieldset >
<legend>Search</legend>
<div class='container'>
<label for='C_Name' >Business Name: </label><br/>
<input type='text' name='C_Name' id='C_Name' maxlength="50" /><br/>
<label for='C_County' >City: </label><br/>
<input type='text' name='C_County' id='C_County' maxlength="50" /><br/>
<label for='Job_Type' >Job Type: </label><br/>
<input type='text' name='Job_Type' id='Job_Type' maxlength="50" /><br/>
</div>
<div class='container'>
<input type='submit' name='Submit' value='Search' />
</div>
</fieldset>
</form>
Here is the PHP script it links too in the action:
<?php
$mysqli_link = mysqli_connect("server", "database", "pass", "user");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
if(isset($_POST['submit'])) {
// define the list of fields
$fields = array('C_Name', 'C_County', 'Job_Type');
$conditions = array();
// loop through the defined fields
foreach($fields as $field){
// if the field is set and not empty
if(isset($_POST[$field]) && $_POST[$field] != '') {
// create a new condition while escaping the value inputed by the user (SQL Injection)
$conditions[] = "'$field' LIKE '%" . mysqli_real_escape_string($mysqli_link, $_POST[$field]) . "%'";
}
}
// builds the query
$query = "SELECT C_Name, C_StreetNumber, C_StreetName, C_Postcode, C_County, C_Tele, C_Website, Contact_Forename, Contact_Surname, Contact_Email, Jobs.Job_Type, Jobs.Job_Price FROM Company INNER JOIN Jobs ON Company.Company_ID = Jobs.Company_ID";
// if there are conditions defined
if(count($conditions) > 0) {
// append the conditions
$query .= " WHERE " . implode (' AND ', $conditions); // you can change to 'OR', but I suggest to apply the filters cumulative
}
$result = mysqli_query($mysqli_link, $query) or die(mysql_error());
mysqli_close($mysqli_link);
if(isset($_POST['submit'])) {
while($row = mysqli_fetch_assoc($result)) {
$C_Name = $row['C_Name'];
$C_StreetNumber = $row['C_StreetNumber'];
$C_StreetName = $row['C_StreetName'];
$C_Postcode = $row['C_Postcode'];
$C_County = $row['C_County'];
$C_Tele = $row['C_Tele'];
$C_Website = $row['C_Website'];
$Contact_Forename = $row['Contact_Forename'];
$Contact_Surname = $row['Contact_Surname'];
$Contact_Email = $row['Contact_Email'];
$Job_Type = $row['Job_Type'];
$Job_Price = $row['Job_Price'];
echo "<b>Name: $C_Name</b><br>Street Number: $C_StreetNumber<br>Street Name: $C_StreetName<br>Postcode: $C_Postcode<br>County: $C_County<br>Telephone: $C_Tele<br>Website: $C_Website<br>Contact Name: $Contact_Forename $Contact_Surname<br>Email: $Contact_Email<br>Job Type: $Job_Type<br>Job Price: $Job_Price<hr><br>";
}
}
}
?>
For some reason it is returning that there is "
unexpected end of file
" however I've checked the code and all the codes is closed off correctly (from what I can see) when I add another '}' in at the end the script doesn't return anything at all. Anyone know why this would be happening?
Source: Search MySQL Database with Multiple Fields in a Form
Because you forget to close
if(isset($_POST['submit'])) {// you not close the condition
At the end of your file
Just add }
at end of your file
這篇關于PHP 多輸入搜索的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!