• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 无法将 'is not null' 查询传递到服务器端

[复制链接]
菜鸟教程小白 发表于 2022-12-13 07:06:44 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

情况

我想从数据库中搜索数据。

我的逻辑

如果我的文本字段长度为零,则将“不为空”值传递给服务器端。否则将文本字段数据传递到服务器端。

客户端:

 UILabel *first=[[UILabel alloc]init];
 UILabel *sec=[[UILabel alloc]init];

 if (_zip.text.length==0) {
    first.text=@"is not null";
 }
 else
 if (_zip.text.length>0) {
 first.text=_zip.text;
 }
 if (_par.text.length==0) {
 sec.text=@"is not null";
 }
 else if (_par.text.length>0){
 sec.text=_par.text;
 }


 NSString *selquery=[NSString   stringWithFormat"http://localhost/filtering1.php?zipcode=%@&parking=%@",first.text,sec.text];


 NSData *data=[NSData dataWithContentsOfURL:[NSURL URLWithString:selquery]];
 NSString *str=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
 NSLog(@"%@",str);

服务器端

    <?php

    $host="localhost";
    $username="root";
    $password="";
    $db_name='BuyAndSell';
    mysql_connect("$host", "$username", "$password")or die("cannot connect");
    mysql_select_db("$db_name")or die("cannot select DB");


    $zipcode=$_GET['zipcode'];
    $parking=$_GET['parking'];

    $sql="SELECT * FROM bas WHERE zipcode='$zipcode' and parking='$parking'";

   if ($result = mysql_query($sql)){

   $resultArray = array();
   $tempArray = array();


   while($row = mysql_fetch_object($result)){
    $tempArray = $row;
    array_push($resultArray, $tempArray);
    }


    echo json_encode($resultArray);

    }
    else{

    echo "failed query";}
    ?>



Best Answer-推荐答案


为了做到正确,您需要在服务器端进行。当您的数据到达服务器时,关于 _zip.text.length 为零的关键信息已经消失。服务器只知道 $zipcode 变量设置为字符串 is not null,所以它运行搜索

SELECT * FROM bas WHERE zipcode='is not null' and parking='is not null'

什么都找不到。

将长度检查的代码移至服务器端,并从客户端传递“原始”字符串。然后修改您的 PHP 代码以根据输入构造查询字符串。从基本字符串 "SELECT * FROM bas WHERE" 开始,然后在 $zipcode 为空时附加 zipcode is not null 或比较到 $zipcode 如果它不为空。之后附加 "AND ",并对 $parking 变量执行相同操作。

注意 1:您的服务器端实现有助于进行简单的 SQL 注入(inject)攻击。引用 this Q&A有关如何通过参数化查询来解决此问题的信息。

注意 2: 在生产代码中使用 SELECT * 不是一个好习惯。见 this Q&A了解它的问题所在。

关于ios - 无法将 'is not null' 查询传递到服务器端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30214184/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap