简体   繁体   English

PHP在mysql中搜索多个关键字

[英]PHP search multiple keywords in mysql

Hi I've been trying to create a program like a search engine. 嗨,我一直在尝试创建一个类似搜索引擎的程序。 So here's what I've got so far: 所以这是到目前为止我得到的:

https://imgur.com/a/4Cnut https://imgur.com/a/4Cnut

I need to choose a symptoms on the select option then add it. 我需要在选择选项上选择一个症状,然后添加它。
Those options that I added will begin to search on each rows on my Mysql Database. 我添加的那些选项将开始在Mysql数据库的每一行上搜索。 I need to output each matching keywords that I added. 我需要输出添加的每个匹配关键字。 It's a real pain, so I wanna know how you will solve this. 这真的很痛苦,所以我想知道你将如何解决这个问题。

Here's my code: 这是我的代码:

$x = 0;
for($x; $x < 10;$x++) {

  $selx = "SELECT * FROM sicks WHERE tags LIKE '%{$_POST['a0']}%' AND id = $x ";
  $sel1 = mysqli_query($connect,$selx);

  $selx = "SELECT * FROM sicks WHERE tags LIKE '%{$_POST['a1']}%' AND id = $x ";
  $sel2 = mysqli_query($connect,$selx);

  $selx = "SELECT * FROM sicks WHERE tags LIKE '%{$_POST['a2']}%' AND id = $x ";
  $sel3 = mysqli_query($connect,$selx);

  $selx = "SELECT * FROM sicks WHERE tags LIKE '%{$_POST['a3']}%' AND id = $x ";
  $sel4 = mysqli_query($connect,$selx);

  $selx = "SELECT * FROM sicks WHERE tags LIKE '%{$_POST['a4']}%' AND id = $x ";
  $sel5 = mysqli_query($connect,$selx);

  $selx = "SELECT * FROM sicks WHERE tags LIKE '%{$_POST['a5']}%' AND id = $x ";
  $sel6 = mysqli_query($connect,$selx);

  $selx = "SELECT * FROM sicks WHERE tags LIKE '%{$_POST['a6']}%' AND id = $x ";
  $sel7 = mysqli_query($connect,$selx);

  $selx = "SELECT * FROM sicks WHERE tags LIKE '%{$_POST['a7']}%' AND id = $x ";
  $sel8 = mysqli_query($connect,$selx);

  $selx = "SELECT * FROM sicks WHERE tags LIKE '%{$_POST['a8']}%' AND id = $x ";
  $sel9 = mysqli_query($connect,$selx);

  $selx = "SELECT * FROM sicks WHERE tags LIKE '%{$_POST['a9']}%' AND id = $x ";
  $sel10 = mysqli_query($connect,$selx);

  $c1 = mysqli_num_rows($sel1);
  $c2 = mysqli_num_rows($sel2);
  $c3 = mysqli_num_rows($sel3);
  $c4 = mysqli_num_rows($sel4);
  $c5 = mysqli_num_rows($sel5);
  $c6 = mysqli_num_rows($sel6);
  $c7 = mysqli_num_rows($sel7);
  $c8 = mysqli_num_rows($sel8);
  $c9 = mysqli_num_rows($sel9);
  $c10 = mysqli_num_rows($sel10);

  $q2 = mysqli_query($sel2);
  $q3 = mysqli_query($sel3);
  $q4 = mysqli_query($sel4);
  $q5 = mysqli_query($sel5);
  $q6 = mysqli_query($sel6);
  $q7 = mysqli_query($sel7);
  $q8 = mysqli_query($sel8);
  $q9 = mysqli_query($sel9);
  $q10 = mysqli_query($sel10);
  $q1 = mysqli_query($sel1);

  $row = mysqli_fetch_array($q2);
  $_SESSION['news'] = $row['tags'];

  $every = $c1 + $c2 + $c3 + $c4 + $c5 + $c6 + $c7 + $c8 + $c9 ;

  echo $every;
}

The {$_POST['a0']} , {$_POST['a1']} , ... are the keywords came from the select option. {$_POST['a0']}{$_POST['a1']} ,...是关键字来自选择选项。

My JavaScript: 我的JavaScript:

$('.sendit').click(function() {
  a0 = $('.a0').val();
  a1 = $('.a1').val();
  a2 = $('.a2').val();
  a3 = $('.a3').val();
  a4 = $('.a4').val();
  a5 = $('.a5').val();
  a6 = $('.a6').val();
  a7 = $('.a7').val();
  a8 = $('.a8').val();
  a9 = $('.a9').val();
  a10 = $('.a10').val();

  $.ajax({
    url:"function.php?sendit=true",
    type:"post",
    data:{a0:a0,
    a1:a1,
    a2:a2,
    a3:a3,
    a4:a4,
    a5:a5,
    a6:a6,
    a7:a7,
    a8:a8,
    a9:a9,
    a10:a10},
    success:function(data) {
      $('.texta').html(data);
    }
  });

  return false;
});

I don't know why you're calling mysqli_query two times, maybe it's the context of your code. 我不知道为什么您要两次调用mysqli_query ,也许这是您代码的上下文。

I suggest you to always make the SQL server to these jobs. 我建议您始终将SQL Server用于这些作业。 The interfacing language (especially php) may not exactly be as efficient as the SQL server. 接口语言(尤其是php)可能不完全像SQL Server一样有效。

My advice is to use the OR operator in the WHERE clause, so you'll send only one query. 我的建议是在WHERE子句中使用OR运算符,因此您将仅发送一个查询。

$selx = "SELECT * FROM sicks WHERE
(tags LIKE '%{$_POST['a0']}%' OR 
tags LIKE '%{$_POST['a1']}%' OR 
tags LIKE '%{$_POST['a2']}%' OR 
) AND id = $x";

Note: this code is SQL injectable. 注意:此代码是SQL可注入的。 I suggest you to use str_replace on your $_POST values: 我建议您在$_POST值上使用str_replace

$_POST['a0'] = str_replace("'", "'", $_POST['a0']);

This will double all the single quotes, which are already used to delimit the strings. 这将使所有已经用于定界字符串的单引号加倍。

About the SQL injection . 关于SQL注入

You can do it by in one query by stringing together the individual LIKEs with ORs 您可以通过在一个查询中将各个LIKE与OR串在一起来完成此操作

SELECT * FROM sicks
WHERE tags LIKE '%$_POST['a0']}%'
OR tags LIKE '%$_POST['a1']}%'
OR tags LIKE '%$_POST['a2']}%'
OR tags LIKE '%$_POST['a3']}%'
and so on...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM