【问题标题】:Can not get the url parameter in PHP无法在 PHP 中获取 url 参数
【发布时间】:2015-05-27 08:22:24
【问题描述】:

我试图在 SQL 中获取 URL 参数,但没有任何反应。

这是我的网址:

http://localhost/webshop/imagegallery.php?categori=necklace

这是我的 SQL 查询:

$sql = 'SELECT count(productid) FROM products where productcategori=".$_GET["categori"]"';

我做错了什么?

也看看这个查询:

  $sql = 'select * from products join ids on products.productid=ids.productid join photos on photos.photosid=ids.photoid where products.productcategori='".$_GET["kategori"]."' && ids.photonumber=1 ORDER BY products.productid  DESC $limit';

【问题讨论】:

  • **productcategori= 有什么用处?你确定他们属于那里吗?
  • 我想让它加粗。 * 实际上不会在那里。
  • 我删除了它们。我们不希望其他人感到困惑;)

标签: url-parameters


【解决方案1】:

首先,您的引号似乎是问题所在。尝试将您的查询行更改为:

$sql = "SELECT count(productid) FROM products where productcategori='".$_GET["categori"]."'";

此外,您应该从不将变量插入到这样的 SQL 查询中。绝不。 原因是这样,您的系统容易受到 SQL 注入的攻击。

改为考虑使用 PDO。 This SO question 有一个关于如何正确执行的很好的答案。

使用该答案,这是有关您问题最后一部分的一些示例代码。请注意,我用 PDO 占位符替换了查询字符串中的所有变量。

<?php
$pdo = new PDO('mysql:dbname=mydatabase;host=127.0.0.1;charset=utf8', 'username', 'password');
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$sql = "SELECT * FROM products JOIN ids ON products.productid=ids.productid JOIN photos ON photos.photosid=ids.photoid WHERE products.productcategori=:categori && ids.photonumber=1 ORDER BY products.productid DESC LIMIT :limit_min , :limit_max";
$stmt = $pdo->prepare($sql);
$stmt->bindParam(':categori', $_GET['categori']);
$stmt->bindParam(':limit_min', ($pagenum - 1) * $page_rows, PDO::PARAM_INT);
$stmt->bindParam(':limit_max', $page_rows, PDO::PARAM_INT);
$stmt->execute();

foreach($stmt as $row) {
    // do something with $row
}
?>

【讨论】:

  • 我还有一个 quey。我做了同样的方法,但它不起作用。请检查一下好吗?
  • @Danny 我为此添加了一个示例。
  • 谢谢,但这里的 $limit 是; " $limit = 'LIMIT ' .($pagenum - 1) * $page_rows .',' .$page_rows; " 你确定我们指的是同一个限制吗?
  • 好的,请稍等。我会尽快更新我的答案。
  • 你去。我将LIMIT 部分拆分为两个参数。
猜你喜欢
  • 2014-06-13
  • 2021-12-10
  • 2021-05-09
  • 1970-01-01
  • 1970-01-01
  • 2014-06-13
  • 1970-01-01
  • 1970-01-01
  • 2013-12-22
相关资源
最近更新 更多