【问题标题】:is there a way to get the value [product_code]1INSAZE from querystring and use it to run sql query有没有办法从查询字符串中获取值 [product_code]1INSAZE 并使用它来运行 sql 查询
【发布时间】:2019-11-19 11:12:42
【问题描述】:

我正在尝试从如下所示的查询字符串中获取数据:

http://127.0.0.1:8000/product/controller/php?ids[]=[product_code]1INSAZE

如果我这样做:

$ids = $request->query->get('ids', null);

and dump it I get something like this :

array:1 [▼ 0 => "[product_code]1INSAZE" ] ]

如果我dump($ids[0]) 我得到:

"[product_code]1INSAZE"

我需要该字符串才能执行此操作:

$ids = $ids[0];

$result = $this->createQueryBuilder('k')

->andWhere("k.product_code = $ids")

->getQuery()

->getResult();

return $result;

我收到此错误:

[Syntax Error] line 0, col 72: Error: Expected Literal, got '['

它与那个 [product_code] 有关吗?如果是,我该如何摆脱它并执行查询?

【问题讨论】:

  • 你需要[product_code]1INSAZE整体吗?
  • yes 需要在查询字符串中,但是运行代码时出现语法错误。
  • 我解决了 str_replace() php 方法的问题,我删除了这个词。
  • 那是因为你在值周围缺少引号,例如->andWhere("k.product_code = '$ids'"),但更好的办法是绑定@UrsolSolutions 回答的那些参数。
  • Rikudou_Sennin 谢谢这就是我的问题的解决方案

标签: php mysql sql symfony symfony4


【解决方案1】:

不要只将变量放在引号中,这是不安全的,也不是使用 QueryBuilder 的首选方式。使用参数。

andWhere("k.product_code = :ids");
->setParameter('ids', $ids);

如果这不起作用,请尝试使用 '\[' 转义 '['

编辑:使用了错误的框架语法

【讨论】:

  • 预期文字,得到 '\'
  • 你添加参数了吗?在您尝试之前忽略 \。
  • 你的意思是查询字符串?是的,我确实犯了那个错误
  • 我解决了 str_replace() php 方法的问题,我删除了这个词。
  • 这似乎不是一个解决方案。您能在此处发布解决问题的代码吗?
猜你喜欢
  • 2019-12-25
  • 1970-01-01
  • 2018-03-02
  • 1970-01-01
  • 2014-02-21
  • 2011-03-12
  • 2015-01-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多