dsxrq28228 2015-03-01 00:38
浏览 23

PHP表单输入和mySQL Null

I've got a form that submits data to a PHP script. I'm then querying a mySQL DB with the script to return the results.

Form:

<form class="search" action="customers_table.php" method="post">
    Customer ID: <input type="text" id="id" name="id" autocomplete="off"/><br/>
    Label: <input type="text" id="label" name="label" autocomplete="off"/><br/>
    Phone: <input type="text" id="phone" name="phone" autocomplete="off"/><br/>
    State: <input type="text" id="province" name="province" autocomplete="off"/><br/>
    <input type="submit" value="search"/>
</form>

PHP script:

$label = "%".$_POST['label']."%";
$id = "%".$_POST['id']."%";
$phone = "%".$_POST['phone']."%";
$province = "%".$_POST['province']."%";

$db->query("SELECT  id       AS 'Customer ID', 
                    label       AS 'Name', 
                    province    AS 'State', 
                    phone       AS 'Phone' 
            FROM customer 
            WHERE label     LIKE :label 
            AND   id        LIKE :id
            AND   phone     LIKE :phone
            AND   province  LIKE :province");

The problem is that some of the fields that I search on can contain NULL values in the DB. For example, 'phone' could be NULL.

Therefore, if phone is left blank in the above form and the form is submitted, I won't see the results where phone is NULL.

My query is looking for ...AND phone LIKE '%%' which excludes the NULL values.

Is there any way I can search on either phone LIKE '%whatever%' or if phone is empty, then nothing?

I could probably whack together something like the below, but I suspect there may be a better way? I'd be doing this for multiple columns, potentially.

    // if phone is entered, add to SQL query
    if(isset($_POST['phone'])) {
        $phone_qry = "AND phone LIKE :";
        $phone = "%".$_POST['phone']."%";
    }
    // if phone not entered, don't add to query
    else {
        $phone_qry = "";
        $phone = "";
    }


    $label = "%".$_POST['label']."%";
    $id = "%".$_POST['id']."%";
    $phone = "%".$_POST['phone']."%";
    $province = "%".$_POST['province']."%";

    $db->query("SELECT  id       AS 'Customer ID', 
                        label       AS 'Name', 
                        province    AS 'State', 
                        phone       AS 'Phone' 
                FROM customer 
                WHERE label     LIKE :label 
                AND   id        LIKE :id"
                . $phone_qry . $phone . " 
                AND   province  LIKE :province");
  • 写回答

1条回答 默认 最新

  • dongzhi8984 2015-03-01 01:01
    关注

    Thanks Ed! COALESCE: WHERE COALESCE(Label, '') LIKE :label worked perfectly. Exactly what I was after.

    评论

报告相同问题?

悬赏问题

  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解