-2

您好我正在尝试创建一个由变量存储的字符串,其值由 URL 中的值确定。但是,我不确定如何去做。我不断收到错误“意外的 T_IF”。我可能做错了什么?

谢谢

PHP:

        $where=  

            if (isset($_GET['name'])) {
                echo "name=" . $name;
            }
            if (isset($_GET['number'])) {
                echo " AND number=" . $number;
            }
            if (isset($_GET['address'])) {
                echo " AND address=" . $address;
            }
            if (isset($_GET['city'])) {
                echo " AND city=" . $city;
            }
            if (isset($_GET['state'])) {
                echo " AND state=" . $state;
            }
            if (isset($_GET['zip'])) {
                echo " AND zip=" . $zip;
            }
            if (isset($_GET['color'])) {
                echo " AND color=" . $color;
            }
        ;
4

6 回答 6

8

我会简化一下。

$whereElements = array();
foreach ($_GET as $key=>$value) {
    $whereElements[] = $key . '=' . $value;
}
$where = implode(' AND ', $whereElements);

如果您不想要所有变量,则可以始终将其列入白名单/提供要过滤的键数组。此外,如果您在 SQL 或其他上下文中使用它,您应该真正使用准备好的查询,因为这将对 SQL 注入开放。

于 2013-04-05T03:40:44.733 回答
2

尝试

        $where=""; 

            if (isset($_GET['name'])) {
                $where .= "name=" . $name;
            }
            if (isset($_GET['number'])) {
                $where .= " AND number=" . $number;
            }
            if (isset($_GET['address'])) {
                where .= " AND address=" . $address;
            }
            if (isset($_GET['city'])) {
                where .= " AND city=" . $city;
            }
            if (isset($_GET['state'])) {
                $where .= " AND state=" . $state;
            }
            if (isset($_GET['zip'])) {
                $where .= " AND zip=" . $zip;
            }
            if (isset($_GET['color'])) {
                $where .= " AND color=" . $color;
            }
echo $where;
于 2013-04-05T03:41:22.753 回答
1

你不能像这样声明 PHP 变量。你必须连接变量。

<?php
$where = '';
if (isset($_GET['name'])) {
    $where .= " name=" . $name;
}
if (isset($_GET['number'])) {
    $where .= " AND number=" . $number;
}
if (isset($_GET['address'])) {
    $where .= " AND address=" . $address;
}
if (isset($_GET['city'])) {
    $where .= " AND city=" . $city;
}
if (isset($_GET['state'])) {
    $where .= " AND state=" . $state;
}
if (isset($_GET['zip'])) {
    $where .= " AND zip=" . $zip;
}
if (isset($_GET['color'])) {
    $where .= " AND color=" . $color;
}
echo $where;
?>
于 2013-04-05T03:41:12.527 回答
0

用这个:

if (isset($_GET['name'])) {
    $where .= "name=" . $name;
}
if (isset($_GET['number'])) {
    $where .= " AND number=" . $number;
}
if (isset($_GET['address'])) {
    $where .= " AND address=" . $address;
}
if (isset($_GET['city'])) {
    $where .= " AND city=" . $city;
}
if (isset($_GET['state'])) {
    $where .= " AND state=" . $state;
}
if (isset($_GET['zip'])) {
    $where .= " AND zip=" . $zip;
}
if (isset($_GET['color'])) {
    $where .= " AND color=" . $color;
}

基本上,.=告诉 PHP 将值附加到=变量中已经存在的值的末尾。

在您的问题中,您使用echo错误。echo只是意味着在页面上写下后面的内容,而不是将后面的内容分配给变量。

于 2013-04-05T03:39:24.740 回答
0

尝试

$where="";

进而

if (isset($_GET['name'])) {
    $where .= "name=" . $name;
}
if (isset($_GET['number'])) {
    $where .= " AND number=" . $number;
}
if (isset($_GET['address'])) {
    $where .= " AND address=" . $address;
}

等等。

于 2013-04-05T03:40:28.027 回答
0

要么你启用了 register_globals(这是邪恶的!),要么你的代码永远不会工作。

$where=  

在这里,您开始了一项任务,...

if (isset($_GET['name'])) {
    echo "name=" . $name;
}

if...并用一个语句打断它。这就是 PHP 抱怨的原因。

下一个问题是您检查一个变量 ( $_GET['name']) 的存在,然后使用另一个变量 ( $name)。

此外,您显然是在尝试构建(部分)SQL 查询而不保护您的数据库不被黑客入侵。

因此,为了简化您的代码并使其更安全,请将其更改为

// $mysqli = new mysqli("localhost", "my_user", "my_password", "my_database");

$properties = array('number', 'address', 'city', 'state', 'zip', 'color');
$conditions = array();
foreach ($properties as $property) {
    if (!empty($_GET[$property])) {
        $conditions[] = sprintf("`%s`='%s'", $property, $mysqli->real_escape_string($_GET[$property]));
    }
}
$where = implode(' AND ', $conditions);
于 2013-04-05T04:36:15.973 回答