草庐IT

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

coder 2024-01-24 原文

情况

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

我的逻辑

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

客户端:

 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";}
    ?>

最佳答案

为了正确执行,您需要在服务器端执行。当您的数据到达服务器时,有关 _zip.text.length 为零的关键信息已经消失。服务器只知道 $zipcode 变量设置为字符串 不为空,因此它运行搜索

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/

有关ios - 无法将 'is not null' 查询传递到服务器端的更多相关文章

  1. ruby - 使用 ruby​​ 和 savon 的 SOAP 服务 - 2

    我正在尝试使用ruby​​和Savon来使用网络服务。测试服务为http://www.webservicex.net/WS/WSDetails.aspx?WSID=9&CATID=2require'rubygems'require'savon'client=Savon::Client.new"http://www.webservicex.net/stockquote.asmx?WSDL"client.get_quotedo|soap|soap.body={:symbol=>"AAPL"}end返回SOAP异常。检查soap信封,在我看来soap请求没有正确的命名空间。任何人都可以建议我

  2. ruby-on-rails - rails : "missing partial" when calling 'render' in RSpec test - 2

    我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou

  3. ruby-on-rails - 由于 "wkhtmltopdf",PDFKIT 显然无法正常工作 - 2

    我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-

  4. ruby-on-rails - 'compass watch' 是如何工作的/它是如何与 rails 一起使用的 - 2

    我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t

  5. ruby - 具有身份验证的私有(private) Ruby Gem 服务器 - 2

    我想安装一个带有一些身份验证的私有(private)Rubygem服务器。我希望能够使用公共(public)Ubuntu服务器托管内部gem。我读到了http://docs.rubygems.org/read/chapter/18.但是那个没有身份验证-如我所见。然后我读到了https://github.com/cwninja/geminabox.但是当我使用基本身份验证(他们在他们的Wiki中有)时,它会提示从我的服务器获取源。所以。如何制作带有身份验证的私有(private)Rubygem服务器?这是不可能的吗?谢谢。编辑:Geminabox问题。我尝试“捆绑”以安装新的gem..

  6. ruby - ECONNRESET (Whois::ConnectionError) - 尝试在 Ruby 中查询 Whois 时出错 - 2

    我正在用Ruby编写一个简单的程序来检查域列表是否被占用。基本上它循环遍历列表,并使用以下函数进行检查。require'rubygems'require'whois'defcheck_domain(domain)c=Whois::Client.newc.query("google.com").available?end程序不断出错(即使我在google.com中进行硬编码),并打印以下消息。鉴于该程序非常简单,我已经没有什么想法了-有什么建议吗?/Library/Ruby/Gems/1.8/gems/whois-2.0.2/lib/whois/server/adapters/base.

  7. ruby-on-rails - 无法使用 Rails 3.2 创建插件? - 2

    我对最新版本的Rails有疑问。我创建了一个新应用程序(railsnewMyProject),但我没有脚本/生成,只有脚本/rails,当我输入ruby./script/railsgeneratepluginmy_plugin"Couldnotfindgeneratorplugin.".你知道如何生成插件模板吗?没有这个命令可以创建插件吗?PS:我正在使用Rails3.2.1和ruby​​1.8.7[universal-darwin11.0] 最佳答案 随着Rails3.2.0的发布,插件生成器已经被移除。查看变更日志here.现在

  8. ruby-on-rails - Rails 3.2.1 中 ActionMailer 中的未定义方法 'default_content_type=' - 2

    我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer

  9. ruby - 无法运行 Rails 2.x 应用程序 - 2

    我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby​​:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r

  10. ruby - 在 jRuby 中使用 'fork' 生成进程的替代方案? - 2

    在MRIRuby中我可以这样做:deftransferinternal_server=self.init_serverpid=forkdointernal_server.runend#Maketheserverprocessrunindependently.Process.detach(pid)internal_client=self.init_client#Dootherstuffwithconnectingtointernal_server...internal_client.post('somedata')ensure#KillserverProcess.kill('KILL',

随机推荐