2013-04-05 110 views
-2

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

感谢

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; 
      } 
     ; 
+1

为什么你会投下来,但没有发表评论? – user2096890 2013-04-05 04:41:15

+0

这是一个语法错误问题。有很多喜欢它,这一个是没有什么不同。语法错误总是过于本地化,因为它们在http://stackoverflow.com/search?q=%5Bphp%5D+unexpected+T_IF+is%3Aquestion之前已经被记录。注意大多数这些问题都有负面投票。 StackOverflow不是您的个人皮棉检查器。 – 2013-04-05 13:25:35

回答

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; 
?> 
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的=到什么已经存在结束后的价值大头钉变量。

在你的问题中,你使用的是错误的echoecho只是表示在页面上写下下面的内容,而不是将后面的内容分配给变量。

+0

但是这只设置$到当前正在测试的变量的哪里。我需要它积累。 – user2096890 2013-04-05 03:41:51

+0

'。='导致它累积(技术上“连接”)。 – 2013-04-05 03:42:27

+1

其中积累实际上意味着连接... – ElefantPhace 2013-04-05 03:43:15

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; 
} 

等。

8

我会简化这一点。

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

如果你不想要所有的变量,你总是可以白名单/提供一个过滤键的数组。另外,如果您在SQL或其他环境中使用它,则应该使用准备好的查询,因为这将对SQL注入开放。

+1

这也避免了以'AND'开头的字符串,这可能是不可取的。 – icktoofay 2013-04-05 03:42:34

+1

+1这里是最好的答案。 – 2013-04-05 03:53:23

+0

+1好技巧! – ElefantPhace 2013-04-05 04:08:49

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; 
+0

它工作完美谢谢你! – user2096890 2013-04-05 03:43:48

+0

不客气。 – 2013-04-05 03:45:46

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);