dsdsds12222 2012-11-17 06:40
浏览 116
已采纳

PHP数组只返回字符串的第一个字符

I'm using check-boxes to alter an SQL query.

The HTML:

        <input type="checkbox" name="chapter" value="7"  /><label>ch. 7</label><br />
        <input type="checkbox" name="chapter" value="13"  /><label>ch. 13</label>

The PHP:

   //adjust query for chapter
   $chap = $_POST['chapter'];
   $chapter = "";
   if(!empty($chap)){
        $N = count($chap);
        if($N == 1){
         $chapter .= " AND chapter = '".$chap[0]."'";
        }else{
         $chapter .= " AND (chapter = '".$chap[0]."' OR chapter = '".$chap[1]."')";
        }
   }    

       $zquery = "SELECT * FROM records WHERE ".$zipcodes.$chapter;

When the check boxes are left unchecked it works fine. When the chapter 7 box is checked it works fine. The trouble is with that 13 box. When I check that, $chap[] only returns a value of "1" instead of "13". For example, when I var_dump $zquery I get string(3384) "SELECT * FROM records WHERE (party_zip='34683' OR party_zip='34682' ) AND chapter = '1'".

Also, $N is always returning 1, even if I check both boxes. Is $_POST['chapter'] not an array? Why is it only returning the first character in the string?

Namaste

  • 写回答

5条回答 默认 最新

  • duanmu2941 2012-11-17 06:53
    关注

    Both Dr. Molle and Douglas are correct, but I'd like to expand a little on their answers.

    $_POST is an array. It can contain a variety of values, either in a key => value structure or in a series of values indexed numerically. Each value can be any other type of data. In this case, your variable:

    $_POST['chapter']

    is a string, most likely - PHP interprets the array index ([0], etc) as a character index when used against a string (or against a variable that can be trivially cast to string, like an integer or float.) This is why you're getting the first letter out instead of the first value: there's only the one value to get!

    Of course, you might run into situations where the value of an array is itself an array - in which case you could access the value by typing, say, $array[0]['candles'] or something, but that's not going to happen with the $_POST array (which is always an array of strings.)

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

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器