一个输入中包含多个列的搜索输入


Search input with multiple columns just in 1 input

我在我的网站(汽车网站)上有一个搜索输入,我用这个搜索输入只搜索他们品牌的汽车。。。比方说奥迪、宝马等……我想知道是否有一种我可以打字的方式:

奥迪R8,而不是什么都不展示,它将展示那辆车,就像从汽车中选择品牌,型号其中品牌=第一空间,型号=第二空间。。。

类似的东西,而不是创建两个输入。。。所以我只能在里面输入全名奥迪R8 Spyder或奥迪R8,它将只显示R8车型。。。

谢谢,

使用此:

 <?php        
       $input = 'Audi R8';
       $arr = explode(' ', $input); //to separate the input
       $sql = "SELECT * FROM automoviles WHERE marca like '%$arr[0]%' or modelo like '%$arr[1]%'";        
 ?>

使用转义编辑1

<?php 
        $input = 'Audi R8';
        $arr = explode(' ', $input);
        $input1 = mysqli_real_escape_string($conn, $arr[0]);
        $input2 = mysqli_real_escape_string($conn, $arr[1]);
        $sql = "SELECT * FROM automoviles WHERE marca like '%$input1%' or modelo like '%$input2%'";  
?>

Hi尝试使用php的爆炸函数,这样您将获得两个变量。

<?php
$dcs_str = "audi m8";
$dcs_arr = explode(" ",$dcs_str); ?>

您可以将输入一分为二。例如:

var input = "Audi R8";
var space = input.indexOf(" ");
var firstspace = input.substring(0, space); // Audi
var secondspace = input.substring(space); // R8

所以你的sql应该是这样的:

SELECT brand, model FROM cars WHERE brand = firstspace, model=secondspace