OGeek|极客世界-中国程序员成长平台

标题: ios - 无法将 'is not null' 查询传递到服务器端 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 07:06
标题: ios - 无法将 'is not null' 查询传递到服务器端

情况

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

我的逻辑

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

客户端:

 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/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4