【问题标题】:PHP search form with multiple inputs具有多个输入的 PHP 搜索表单
【发布时间】:2015-09-25 01:32:19
【问题描述】:

我有一个 PHP 脚本,我正在尝试使用它从数据库生成搜索结果,其中包含多个搜索文本输入和一个提交。三个输入是术语、关键字和位置。我已经完成了搜索,但我发现验证逻辑有点困难。我将查询分为三个部分,以便当其中一个输入为空时,它可以免除将查询的那部分添加到完整查询中。但是由于 OR 运算符,当第一个输入为空时,其余的都失败了。

第二只眼睛在这里会有所帮助。请不要将此问题标记为过于笼统,只需查看查询,您就会发现问题所在。

$term = mysqli_real_escape_string ($con , $_GET['term'] );
$location = mysqli_real_escape_string ($con , $_GET['location'] );
$keyword =  mysqli_real_escape_string ($con , $_GET['keyword'] );

if(empty($term)) {
$term1 = "";
}else{$term1 = "job_title LIKE '%".$term."%'";}

if(empty($location)) {
$loc1 = "";
}else{$loc1 = "location LIKE '%".$location."%'";}

if(empty($keyword)) {
$key1 = "";
}else{$key1 = "OR description LIKE '%".$keyword."%'";}


$sql = "SELECT * FROM jobs WHERE ".$term1." ".$loc1." ".$key1." ";
$r_query = mysqli_query($con,$sql);


while ($joblist = mysqli_fetch_array($r_query)){
$now = date('Y-m-d',time());

【问题讨论】:

  • 也许这个帖子对你有用,如果它有效,OP 永远不会回复,但我认为它应该(如果有效,你可以回复吗?)。 stackoverflow.com/questions/32685881/…
  • @chris85 谢谢克里斯,遗憾的是他的逻辑与我的有点不同,因为他没有使用阻碍我的逻辑的 OR 和 LIKE 运算符:(

标签: php mysql


【解决方案1】:

您在第一个 like 语句之后缺少 OR,这里 -

if(empty($location)) {
$loc1 = "";
}else{$loc1 = "location LIKE '%".$location."%'";}

您可能想在location like 之前放置一个OR,所以-

"OR location LIKE '%".$location."%'"

【讨论】:

  • 谢谢@lincolndidon1 我以前有手术室,在发帖前把它拿出来了。它不起作用,因为:SELECT * FROM jobs WHERE job_title LIKE '%".$term."%' OR location LIKE '%".$location."%' OR description LIKE '%".$keyword."%' 这是因为一旦第一个条件 job_title LIKE '%".$term."%' 为空,则查询变为SELECT * FROM jobs WHERE OR location LIKE '%".$location."%' OR description LIKE '%".$keyword."%' 由于WHERE OR 序列而不起作用
【解决方案2】:

这是修改后的方法。试一试,如果它有效或无效,请回帖。

if (!empty($_GET['term'])) {
    $where[] = " job_title like ? ";
    $params[] = '%' . $_GET['term'] . '%';
}
if (!empty($_GET['location'])) {
    $where[] = " location like ? ";
    $params[] = '%' . $_GET['location'] . '%';
}
if (!empty($_GET['keyword'])) {
    $where[] = " description like ? ";
    $params[] = '%' . $_GET['keyword'] . '%';
}
$sql_where = !empty($where) ? ' where ' . implode(' or ', $where) : '';
$query = "SELECT * FROM jobs $sql_where";
if (!($tot = mysqli_prepare($con, $query))) {
   echo "Prepare failed: (" . mysqli_errno($con) . ") " . mysqli_error($con);
} else {
    if(!empty($params)) {
    $params = array_merge(array($tot),
        array(str_repeat('s', count($params))), 
        $params);
    call_user_func_array('mysqli_stmt_bind_param', $params);
    // adapated from http://stackoverflow.com/questions/793471/use-one-bind-param-with-variable-number-of-input-vars and http://www.pontikis.net/blog/dynamically-bind_param-array-mysqli may need to be altered
    }
    mysqli_execute($tot);
}

【讨论】:

  • 我尝试了查询,但似乎参数和位置对我来说效果不佳,所以我发现并根据您的进行了一些更改,这是有效的结果。
  • @GeraldGray 你遇到了什么错误;或者它是怎么不起作用的?
  • 你能输出$query吗?
  • $query 的输出给了我SELECT * FROM jobs where job_title like ?
  • 它将查询与警告一起输出:警告:mysqli_stmt_bind_param() 的参数 3 应为参考,值在
【解决方案3】:

这似乎根据@chris85 的建议起作用。我不太确定 $params[] 是如何发挥作用的。任何解释都会很好。

if (!empty($_GET['term'])) {
$where[] = " job_title LIKE '%".$_GET['term']."%' ";
$params[] = '%' . $_GET['term'] . '%';
}
if (!empty($_GET['location'])) {
$where[] = " location LIKE '%".$_GET['location']."%' ";
$params[] = '%' . $_GET['location'] . '%';
}
if (!empty($_GET['keyword'])) {
$where[] = " description LIKE '%".$_GET['keyword']."%' ";
$params[] = '%' . $_GET['keyword'] . '%';
}
$sql_where = !empty($where) ? ' where ' . implode(' or ', $where) : '';
$query = mysqli_query ($con, "SELECT * FROM jobs $sql_where");

【讨论】:

  • 我的方法实现了防止 SQL 注入的参数化查询。这会将用户输入直接放入您的查询中,从而使您可以进行 SQL 注入。我的回答怎么了? 1.en.wikipedia.org/wiki/SQL_injection 2.php.net/manual/en/mysqli.quickstart.prepared-statements.php
  • params 从查询中分离出用户数据。您当前的代码中不需要它们,因此您可以删除它们。您不应该使用此代码,尽管如之前的评论中所述..
  • @chris85 运行您给我的方法时,我得到以下信息。 Warning: Parameter 3 to mysqli_stmt_bind_param() expected to be a reference, value givenWarning: mysqli_execute() expects parameter 1 to be mysqli_stmt, boolean given
猜你喜欢
  • 2017-09-24
  • 2014-12-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-30
  • 2019-11-11
相关资源
最近更新 更多