【问题标题】:how to pass String Datatype value from query string in php如何从php中的查询字符串传递字符串数据类型值
【发布时间】:2015-06-03 13:18:03
【问题描述】:

我在 PHP 中使用查询字符串传递字符串值时遇到问题。我正在使用 PhpMyAdmin 数据库。

下面是我的查询字符串:

http://192.168.0.106/PHP/webservice/comments.php?city_town='Vadodara'

我的php代码如下:

if(isset($_GET["city"])){
    @$city = $_GET["city"];

//echo $city;
$query = "select * from shop_detail where city_town ='". $city ."' ";

【问题讨论】:

  • 看起来问题是你得到的是“city”而不是“city_town”
  • 1. PHP 名称与元素不同。 2. 您在传入的值中有引号。 3. 您对使用此代码的 SQL 注入持开放态度,将用户输入与查询分开。 4. 不要使用@。不要压制错误。

标签: php mysql query-string


【解决方案1】:

使用city_town 而不是city 并使用mysqli_real_escape_string 来防止sql注入以及table and column name in backtick

if(isset($_GET["city_town"])){
    $city = mysqli_real_escape_string($conn,$_GET["city_town"]);

//echo $city;
$query = "select * from `shop_detail` where `city_town` ='". $city ."' ";

【讨论】:

  • mysqli_real_escape_string 需要连接,不是吗?我也不清楚 OP 正在使用 mysqli。我只是将 OP 指向该线程以防止 SQL 注入,stackoverflow.com/questions/60174/…
猜你喜欢
  • 2021-11-22
  • 1970-01-01
  • 1970-01-01
  • 2016-05-22
  • 1970-01-01
  • 1970-01-01
  • 2022-11-04
  • 1970-01-01
  • 2012-07-03
相关资源
最近更新 更多