doufan6886 2012-04-26 15:46
浏览 39
已采纳

使用来自html表单的NULL填充SQL查询

I have an html page with a form that contains a couple of selectors.

The form is processed by GET via a js function that uses jquery ajax to send the data to a php page. The php builds the TSQL query to select the information.

If you don’t choose any option in the selectors I need a NULL in my query. Both in the js and php page I’ve tried with:

if($variableFromSelector1 ==’-’){ $variableFromSelector1 =’NULL’;}
if($variableFromSelector2 ==’-’){ $variableFromSelector2 =’NULL’;}

(the – means nothing chosen in the selector)

But this fills the query with ‘NULL’ which is not the same as NULL

Here's how the query goes:

   function showData($variableFromSelector1,$variableFromSelector2)
    {  

            $strSqlSelect = "EXECUTE mybd.dbo.myprocedure 
            @var1='".$variableFromSelector1."',
            @var2='".$variableFromSelector2."'";
    }

How can I get the query with NULL in case no option is chosen in the selector?

Thanks a lot

  • 写回答

2条回答 默认 最新

  • dongqiao1888 2012-04-26 16:02
    关注
    if($variableFromSelector1 ==’-’){ $variableFromSelector1 = NULL;}
    if($variableFromSelector2 ==’-’){ $variableFromSelector2 = NULL;}
    
    function showData($variableFromSelector1,$variableFromSelector2)
    {  
      $var1 = is_null($variableFromSelector1) ? 'NULL' : "'{$variableFromSelector1}'";
      $var2 = is_null($variableFromSelector2) ? 'NULL' : "'{$variableFromSelector2}'";
    
      $strSqlSelect = "EXECUTE mybd.dbo.myprocedure 
      @var1=".$var1.",
      @var2=".$var2;
    }
    

    Also remember to sanitize all inputs to protect against SQL-injection.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向