您好我正在尝试创建一个由变量存储的字符串,其值由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;
}
;
答案 0 :(得分:8)
我会简化一下。
$whereElements = array();
foreach ($_GET as $key=>$value) {
$whereElements[] = $key . '=' . $value;
}
$where = implode(' AND ', $whereElements);
如果您不想要所有变量,则可以将白名单/提供要过滤的键数组。此外,如果您在SQL或其他环境中使用它,您应该使用准备好的查询,因为这将是SQL注入的全开放。
答案 1 :(得分: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;
答案 2 :(得分: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;
?>
答案 3 :(得分: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
只是意味着在页面上写下后面的内容,而不是将后面的内容分配给变量。
答案 4 :(得分: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;
}
等等。
答案 5 :(得分: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);