简体   繁体   English

PHP-两个多重选择下拉菜单,将用户选择传递到MySQL查询中

[英]PHP - two multiple select dropdowns, passing user selections into a MySQL query

EDIT: 编辑:

The drop down menus have the following listed in them: 下拉菜单中列出了以下内容:

Typing Course Daily Marketing Course 打字课程日常营销课程

When using the code below to add selected text form the dropdown into the MySQL statement, only the first word appears ie. 当使用下面的代码从下拉列表中添加所选文本到MySQL语句时,仅出现第一个单词,即。 'Typing' and 'Daily', the code looks like this: “键入”和“每日”,代码如下所示:

SELECT * FROM `acme` WHERE `course` IN('Typing', 'Daily')AND `date` IN('2010-08-27', '2010-08-31')

it should be this: 应该是这样的:

SELECT * FROM `acme` WHERE `course` IN('Typing Course', 'Daily Marketing Course')AND `date` IN('2010-08-27', '2010-08-31')

Original question below: 以下是原始问题:

Hi all, 大家好,

Ok, I'll do my best to explain what I would like to do. 好的,我会尽力解释我想做什么。

I have two dropdown menus set to multiple, the first is Course and the second is Date, here is the code that populates each dropdown: 我有两个下拉菜单设置为多个,第一个是Course,第二个是Date,这是填充每个下拉菜单的代码:

Course 课程

echo "<select name='course' value='' multiple='multiple'>";
            // printing the list box select command
            echo "<option value=''>All</option>";
            while($ntc=mysqli_fetch_array($queryc)){//Array or records stored in $nt
            echo "<option value=$ntc[course]>$ntc[course]</option>";
            /* Option values are added by looping through the array */
            }
            echo "</select>";// Closing of list box 

Date 日期

echo "<select name='date' value='' multiple='multiple'>";
        // printing the list box select command
        echo "<option value=''>All</option>";
        while($nt=mysqli_fetch_array($queryr)){//Array or records stored in $nt
        echo "<option value=$nt[dates]>$nt[dates]</option>";
        /* Option values are added by looping through the array */
        }
        echo "</select>";// Closing of list box 

The main problem I have is passing the results of each dropdown to a MySQL query. 我的主要问题是将每个下拉列表的结果传递给MySQL查询。 For example, if a user select from the Course dropdown 'Typing' AND 'Marketing' - I need the MySQL query to be: 例如,如果用户从“课程”下拉菜单中选择“打字”和“营销”-我需要MySQL查询为:

SELECT * FROM acme WHERE course = 'Typing' OR course = 'Marketing'

In addition, I also need to add the second dropdown into the equation, so working on the assumption the user has selected 'Typing' AND 'Marketing', they then select 21-06-2010 from the Date dropdown, so the query then needs to be: 另外,我还需要将第二个下拉列表添加到公式中,因此假设用户选择了“ Typing”和“ Marketing”,然后他们从Date下拉列表中选择了21-06-2010,因此查询需要成为:

SELECT * FROM acme WHERE course = 'Typing' OR course = 'Marketing' AND date = '21-06-2010' OR date = '18-05-2010'

Clearly, I also need to build in if they select more than one date form the dropdown. 显然,如果他们从下拉列表中选择了多个日期,则还需要进行构建。

I hope I have explained clearly enough what I'm looking to achieve..any and all help gratefully received. 我希望我已经清楚地解释了我想要实现的目标。 Really struggling to get my head around this one. 真的很难让我明白这一点。

Thanks in advance, 提前致谢,

Homer. 荷马。

Use WHERE value IN ('a', 'b') : 使用WHERE value IN ('a', 'b')

SELECT * FROM acme WHERE course IN ('Typing','Marketing') AND date IN ('21-06-2010', '17-09-2010');

In HTML (or the PHP that outputs HTML), add [] to fieldnames: 在HTML(或输出HTML的PHP​​)中,在字段名中添加[]

<select name='course[]' value='' multiple='multiple'>

in PHP: 在PHP中:

$courses=$_POST['course'];
$courses=array_map('mysql_real_escape_string', $courses);


$dates=$_POST['date'];
$dates=array_map('mysql_real_escape_string', $dates);

$query = 'SELECT * FROM `acme` WHERE ';
$query.='`course` IN(\''. join("', '", $courses). '\')';
$query.='AND `date` IN(\''. join("', '", $dates). '\')';

OK, first of all, your SQL is a bit off. 好的,首先,您的SQL有点过时了。 Rather than WHERE course = 'Typing' OR 'Marketing' , you want WHERE course = 'Typing' OR course = 'Marketing' . 而不是WHERE course = 'Typing' OR 'Marketing' ,而是希望WHERE course = 'Typing' OR course = 'Marketing' Alternatively, you could use WHERE course IN ('Typing', 'Marketing') . 或者,您可以使用WHERE course IN ('Typing', 'Marketing') You can then create this using the array_map function to add the quotes and the join function to link them together: 然后,您可以使用array_map函数添加引号,并使用join函数将其链接在一起来创建它:

<?php

...

function escape_and_add_quotes($string) {
    return '\'' . mysql_real_escape_string($string) . '\'';
}

...

$sql = 'SELECT * FROM acme WHERE course IN (' . join(',', array_map('escape_and_add_quotes', $courses)) . ')';

?>

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

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