1

我正在发送一个具有多个值的变量,如下所示:

查询:

 $(".indexMain").load('indexMain.php?color=' + colors.join("+"), function()

--> indexMain.php?colors=blue+red+brown

我想 _GET 这些值,然后在 while 循环中使用它们将它们放入像这样的 SQL 查询中:

$items = $con -> prepare("SELECT * FROM item_descr WHERE color_base1 = :colorbase1");
$items -> bindValue(":colorbase1", $color);

谢谢!

编辑:这是我当前的代码,但它不起作用,它只显示与第一种颜色对应的项目。

foreach (explode(' ', $_GET['color']) as $color) 
    {
        $items = $con -> prepare("SELECT * FROM item_descr WHERE color_base1 = :colorbase1");
        $items -> bindValue(":colorbase1", $color);

    }
4

3 回答 3

3
indexMain.php?color=blue+red+brown

应该

indexMain.php?colors%5B%5D=blue&colors%5B%5D=red&colors%5B%5D=brown

这相当于

indexMain.php?colors[]=blue&colors[]=red&colors[]=brown

这将创建一个可通过$_GET['colors']. 之后使用这个 PHP:

foreach ($_GET['colors'] as $color) {
    $query = $con->prepare("SELECT * FROM item_descr WHERE color_base1 = :colorbase1");
    if ($query->execute(":colorbase1", $color)) {
        // use results from query
    } else {
        // handle failed query
    }
}

如果您不想更改查询字符串,也可以这样做:

foreach (explode(' ', $_GET['colors']) as $color) {
    $query = $con->prepare("SELECT * FROM item_descr WHERE color_base1 = :colorbase1");
    if ($query->execute(":colorbase1", $color)) {
        // use results from query
    } else {
        // handle failed query
    }
}

请注意,唯一的变化是在第一行,我们用空格分割字符串(因为“+”被转换为空格字符)。

Also note that both examples assume that $_GET['colors'] is defined. You can use isset() to check if it is defined.

于 2012-08-17T03:11:32.793 回答
0

我认为这应该有效:

$colors = explode(' ', $_GET['color']);
foreach($colors as $color) {
    // your code here...
}
于 2012-08-17T03:04:10.973 回答
0

试试这个

$colors = explode("+", $_GET['color']);
foreach($colors as $color){
$items = $con -> prepare("SELECT * FROM item_descr WHERE color_base1 = :colorbase1");
$items -> bindValue(":colorbase1", $color);
}

这个怎么样

$colors = explode(" ", $_GET['color']);
foreach($colors as $color){
$items = $con -> prepare("SELECT * FROM item_descr WHERE color_base1 = :colorbase1");
$items -> bindValue(":colorbase1", $color);
}
于 2012-08-17T03:06:18.833 回答