【问题标题】:PHP get from html form and echo a variable as mysql order by [closed]PHP从html表单中获取并通过[关闭]将变量作为mysql顺序回显
【发布时间】:2016-07-20 09:21:32
【问题描述】:

我不知道为什么,但是下面的代码不起作用,我需要获取并显示一个 _GET 变量,但它不起作用

    $order_by = mysqli_real_escape_string($database,$_GET['order_method']);
    $query = mysqli_query($database,"SELECT * FROM `products` 
order by `<?php if(empty($order_by)){echo "id";}else{echo "$order_by"; ?>` ASC");

【问题讨论】:

  • 你说代码不工作是什么意思。到底发生了什么(或没有发生)?

标签: php html mysql css forms


【解决方案1】:

当您在字符串中使用变量来生成格式化文本时,您必须使用 " 符号而不是 ' 符号。当您使用 ' 符号时,PHP 会将单词视为文本,并且不会提取变量的值。

错误:

echo 'value=$test';

echo "value=$test";

样本

$test = isset( $_GET["testing"] )
    ? $_GET["testing"]
    : "notset";
echo "the value is $test";

试试这个

$order_by = mysqli_real_escape_string($database,$_GET['order_method']);
$query = mysqli_query($database,"SELECT * FROM `products` order by `$order_by` ASC");

【讨论】:

  • 编辑了问题以便更好地理解
  • 根据新问题编辑了答案
  • mysql查询中order_by为空怎么办?
  • 表示URL中没有'order_method'。使用 $_GET 后,您的 url 必须包含 order_method 参数。像这样的东西:localhost/allusers.php?order_method=username
猜你喜欢
  • 2013-09-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-14
  • 1970-01-01
  • 2016-12-06
相关资源
最近更新 更多