PHP 多输入搜索 [英] PHP Multiple input search

查看:24
本文介绍了PHP 多输入搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在研究一些 PHP 并且我有 3 个文本输入.在 MySQL 数据库中搜索这些值,并应返回与输入条件相对应的任何数量的结果.

这里是搜索表单:

<字段集><legend>搜索</legend><div class='container'><label for='C_Name' >企业名称:</label><br/><input type='text' name='C_Name' id='C_Name' maxlength="50"/><br/><label for='C_County' >城市:</label><br/><input type='text' name='C_County' id='C_County' maxlength="50"/><br/><label for='Job_Type' >工作类型:</label><br/><input type='text' name='Job_Type' id='Job_Type' maxlength="50"/><br/>

<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 多输入搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
PHP最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆