MySql 将 url 从 php 传递到数据库

MySql pass url from php to database

提问人:user2751691 提问时间:2/22/2019 更新时间:2/22/2019 访问量:139

问:

在我的代码中,我有一个 sql 字符串phpselect * from tablename where url in (%s);

我正在构造输入数组,它是 url 字符串的数组。urls$urls = array("https://www.example.com", "https://www.example.com/try002");

我尝试了多种方法,但没有运气。

方法1:

$inputParams = array(implode(", ", $urls)); 

这给了我错误You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '://www.example.com,https://www.example.com/photos/try002)'

方法2: 所以我尝试添加双引号和单转义引号,因为当我尝试在 sql db 中时,可以工作。select * from tablename where url in ("https://www.example.com", "https://www.example.com/blabla")

foreach($urls as &$url)
{
   $url = "'" . urlencode($url) . "', ";
}
$inputParams = array($urls);

双引号或单引号都不起作用。它产生 \“https://example.com\” 的查询,select * from tablename where url in ("www.example.com/blabla")

我应该如何制定我的输入网址,以便我可以得到类似 please 的东西?select * from tablename where url in ("https://www.example.com", "https://www.example.com/blabla")

非常感谢。

php mysql url 转义 引号

评论

1赞 tadman 2/22/2019
警告:尽可能使用准备好的语句,以避免在查询中注入任意数据并创建 SQL 注入错误。这些在 mysqliPDO 中非常简单,其中任何用户提供的数据都指定了一个 or 指示符,稍后使用 or 填充该指示符,具体取决于您使用的指示符。?:namebind_paramexecute
2赞 tadman 2/22/2019
您没有正确引用或转义这些值。只需使用占位符值并绑定,您的问题就解决了。
0赞 Nick 2/22/2019
您应该使用准备好的语句,但在此期间更改为应该有效$inputParams = array(implode(", ", $urls));$inputParams = "'" . array(implode("', '", $urls)). "'";

答: 暂无答案