【问题标题】:How to pass the selected value from select box to a research如何将选定值从选择框中传递给研究
【发布时间】:2017-04-06 14:31:01
【问题描述】:

我正在寻找一个可用于多项选择的选择框,以确定我想在research 中查找的位置,并附上database 的附件。所以,在这里,这就是我发现的:

我放了一个选择框,上面有我的几个位置:

<select name="SelectLocation[]" MULTIPLE="yes">
<option value="1">Inbound</option>
<option value="2">OutBound</option>
<option value="3">Training Room</option>
<option value="4">Summerside</option>
<option value="5">Alberton</option>
</select>

我使用 foreach 给我选择的位置:

foreach ($_GET['SelectLocation'] as $selectedOption){
//echo $selectedOption."\n";

$query1="select * from location where location_id=$selectedOption";
$info=info_query($query1);
$nomLocation=$info['location'];
$Location .= $nomLocation."\n";
}
echo $Location;

现在,例如,如果我选择 Inbound 和 Summerside,回声会给我: 入境萨默赛德 这在同一行。

我使用查询来获取数据库中的名称(查询1)。 我现在想使用那些“名称”,我选择了我的示例(Inbound 和 Summerside),进行搜索,它只需要选择的位置,而不是另一个,所以我在查询中做了这个:

location.location like".$Location;
$query="select computer.computer_id,
computer.computer_name,
computer.product_key,
computer.model,
computer.serial_number,
`status`.`status`,
computer.starphone,
computer.inst_id,
computer.did,
computer.macaddress,
software.description,
vendor.vendor_name,
location.location,
department.department,
jack.jack_number
from computer
inner join computer_vendor on computer.computer_id=computer_vendor.computer_id
inner join vendor on computer_vendor.vendor_id=vendor.vendor_id
inner join `status`on computer.status_id=`status`.status_id
inner join software on computer.software_id=software.software_id
inner join jack on jack.computer_id=computer.computer_id
inner join location on location.location_id=jack.location_id
inner join department on department.dept_id=jack.dept_id
where computer.computer_name like '%".$critere."%' and location.location like".$Location;

我的问题是:我用我的示例 Inbound 和 Summerside 进行研究,它没有给我任何录音。 是因为我的位置在数据库里他可以伸手去拿吗?我尝试将% 放在$Location 之前和之后,比如$critere,但没有帮助。如果不是位置名称,我是否需要使用 value 和 location_id?

我想要的是,我的$Location 能够告诉数据库选择的位置,并且选择与数据库完美配合,我的研究显示所有必要的信息与所选位置和标准。

如果需要更多信息,请告诉我,我会提供。

select computer.computer_id, 
computer.computer_name, 
computer.product_key,
computer.model, 
computer.serial_number, 
`status`.`status`, 
computer.starphone, 
computer.inst_id, 
computer.did,
computer.macaddress, 
software.description, 
vendor.vendor_name, 
location.location, 
department.department,
jack.jack_number from computer inner join computer_vendor on computer.computer_id=computer_vendor.computer_id 
inner join vendor on computer_vendor.vendor_id=vendor.vendor_id 
inner join `status`on computer.status_id=`status`.status_id 
inner join software on computer.software_id=software.software_id 
inner join jack on jack.computer_id=computer.computer_id 
inner join location on location.location_id=jack.location_id 
inner join department on department.dept_id=jack.dept_id 
where computer.computer_name like '%52%' and location.location in ()

【问题讨论】:

  • 您的代码容易受到 SQL 注入攻击。您应该使用mysqliPDO 准备好的语句,如this post 中所述。
  • 因为你有一个位置数组,你可能想read this 让 mysql 完成这项工作?
  • mysqli已经在另一个网页调用cnx_db中完成了。部分是的,我会去阅读

标签: php mysql database search


【解决方案1】:
$Location=array();
foreach ($_GET['SelectLocation'] as $selectedOption){
//echo $selectedOption."\n";

$query1="select * from location where location_id=$selectedOption";
$info=info_query($query1);
$nomLocation=$info['location'];
$Location[] = addslashes($nomLocation);
}
$locationConsolidated = "'".join("','", $Location)."'";

并将这个 $locationConsolidated 用作 ...

where computer.computer_name like '%".$critere."%' and location.location in ($locationConsolidated)

【讨论】:

  • 这正是我需要的,我的问题是:搜索找不到任何他们认为有的记录!
  • 是的,但是现在,他没有给我数据库中的任何记录,只是告诉我:没有记录(数据什么都不提供时的消息)
  • 或者,给我一个不可能的 Unable to execute query in database and if put in ('$locationConsolidated'),不要给出任何结果,只有上面的消息
  • 我已经更新了我的答案,在准备最终字符串时缺少一个单引号,现在就试试,然后告诉我。如果查询没有给出任何结果,则回显查询并在此处提供生成的查询
  • 我在问题的最后提供了它!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-08
  • 1970-01-01
  • 2020-09-28
  • 2017-02-01
  • 2016-02-07
  • 1970-01-01
相关资源
最近更新 更多