[英]How to use array values in SQL query in PHP_MySQL
I am taking multiple values from user from a drop down list. 我正在从下拉列表中从用户获取多个值。 I stored those values in an array in php. 我将这些值存储在php中的数组中。 Everything is fine. 一切顺利。 Now I want to use these array values to query my database to give the desired results. 现在,我想使用这些数组值来查询数据库以提供所需的结果。 My html code is 我的html代码是
<select multiple name="equipment[]">
<option value="all">ALL</option>
<option value="helmet">HEMLMET</option>
<option value="boots">BOOTS</option>
<option value="jacket">JACKET</option>
<option value="flask">FLASK</option>
</select>
$equipment=$_POST['equipment'];
I used this piece to store values. 我用这部分来存储值。
Now based on one or more values in the array; 现在基于数组中的一个或多个值; I want filter my query. 我想过滤查询。
Select w,x,y,z from table_a inner join table_b where table_a.w="the values in array";
I tried using foreach to loop through, but its taking only the last value. 我尝试使用foreach进行遍历,但是它仅采用了最后一个值。 thank you for your help. 谢谢您的帮助。
Keep in mind that HTML, PHP, and MySQL are three different processing environments, so none of them share the same memory (variables) or code (eg loops). 请记住,HTML,PHP和MySQL是三个不同的处理环境,因此它们都不共享相同的内存(变量)或代码(例如循环)。
Roughly in order: 大致顺序为:
So, to get the variable selections to MySQL from PHP, you need to compose a "query string" that looks for all of the answers. 因此,要从PHP获取MySQL的变量选择,您需要编写一个“查询字符串”以查找所有答案。
It will look something like this: 它看起来像这样:
SELECT w,x,y,z
FROM table_a
INNER JOIN table_b
WHERE table_a.w IN ('helmet', 'boots', 'jacket');
See MySQL Expression syntax for more help on this. 有关更多帮助,请参见MySQL Expression语法 。
To build it, you will need to be VERY CAREFUL to read the "MySQL string injection" (where a person using your web page can change the calls your database makes) question linked above, since you are taking the values from the "user"/"client" (through a URL or form post). 要构建它,您需要非常仔细阅读上面链接的“ MySQL字符串注入”(使用网页的人可以更改数据库进行的调用)问题,因为您是从“用户”获取值的/“客户”(通过URL或表单发布)。
The answers to Can I bind an array to an IN() condition? 我可以将数组绑定到IN()条件的答案吗? have some good suggestions for how to build a query string with array values while preventing injection attacks. 对于如何在防止注入攻击的同时使用数组值构建查询字符串有一些好的建议。
It's the IN clause you are looking for: 您正在寻找的IN子句:
select * from tableA inner join on tableB on ... where tableA.w in ('value1', 'value2')
You can use a loop or implode() function in php to generate the in clause. 您可以在php中使用loop或implode()函数来生成in子句。 Just be mindful that if you select only 1 item in the control, then $_POST['equipment'] will be a string, not an array. 请注意,如果您在控件中仅选择一项,则$ _POST ['equipment']将是字符串,而不是数组。 So, use is_array() to determine if multiple values were selected. 因此,使用is_array()确定是否选择了多个值。
<form action='somepage.php' method='POST'>
.
.
<select multiple name="equipment[]">
<option value="all">ALL</option>
<option value="helmet">HEMLMET</option>
<option value="boots">BOOTS</option>
<option value="jacket">JACKET</option>
<option value="flask">FLASK</option>
</select>
.
.
</form>
somepage.php somepage.php
<?
$equipment=$_POST['equipment'];
$TotalEquipment=sizeof($equipment); //Count total number of equipements selected
$values=""; //initialize values as empty.
for($i=0;$i<$TotalEquipment;$i++)
{
$valu=$equipment[$i];
if($i==0)
{
$values="'".$valu."',";
}
else
{
$values=$values."'".$valu."',";
}
}
$values=rtrim($values, ","); //For removing last comma (,) from the string
$Query="SELECT w,x,y,z
FROM table_a
INNER JOIN table_b
WHERE table_a.w IN ($values)";
//Here, execute your $Query with appropriate mysql functions
?>
Enjoy Coding. 享受编码。 Cheers. 干杯。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.