duanbipu7601 2014-08-22 13:47
浏览 49
已采纳

通过php表单输入的mysql INT和DATE的空值

I'm working on a form like this :

     <tr align="center"><!-- list A doc -->
            <td><b>Documents Collected:</b></td>
            <td>  
                <select name="listA_id">
                <option value="">List A documents</option>
                <?php
                include("connect-db.php");
                $list = mysql_query("SELECT doc_no, doc_name FROM hr_doc_master WHERE doc_id =1 ");

            while($row_list=mysql_fetch_array($list))
            {
                echo  "<option value=\"{$row_list[0]}\">{$row_list[1]}</option>";
            }
            mysql_close($connection);
    ?>
    </select></td></tr>
   <tr align="center"><td><b>Document No.</b></td>
    <td><input type="text" name="listA_doc_no"></td></tr>
            <tr align="center"><td>Expiry Date</td><td><input type="date" name="listA_doc_expiry"></td></tr>

           <tr align="center"><td>List B</td></tr>   
           <!----- list B ------->
            <tr align="center">
            <td><b>Documents Collected:</b></td>
            <td>  
                <select name="listB_id">
                <option value="">List B documents</option>  
                <?php 
                 include("connect-db.php");
                $list = mysql_query("SELECT doc_no, doc_name FROM hr_doc_master WHERE doc_id = 2");

            while($row_list=mysql_fetch_array($list))
            {
                echo  "<option value=\"{$row_list[0]}\">{$row_list[1]}</option>";
            }
            mysql_close($connection);
    ?>

                    </select>
                    </td>
            </tr>
            <tr><td>Document No.</td><td><input type="text" name="listB_doc_no" size =30 ></td></tr>
            <tr align="center"><td><b>Expiry Date</b></td><td><input type="date" name="listB_doc_expiry" size =30 ></td></tr>
           <tr><td colspan=2><hr/></td></tr>
           <tr align="center"><td>List C</td></tr>     
           <tr align="center">
            <td>
                    <b>Documents Collected:</b>
                </td>
                    <td>  
                    <select name="listC_id">
                        <option value="">List C documents</option>
                        <?php
        include("connect-db.php");
                            $list = mysql_query("SELECT doc_no, doc_name FROM hr_doc_master WHERE doc_id =3 ");

            while($row_list=mysql_fetch_array($list))
            {
                echo  "<option value=\"{$row_list[0]}\">{$row_list[1]}</option>";
            }
            mysql_close($connection); `?>`


                    </select>
                </td>
        </tr>
        <tr align="center"><td>Document No.</td><td><input type="text" name="listC_doc_no" size =30 ></td></tr>
        <tr align="center"><td>Expiry Date</td><td><input type="date" name="listC_doc_expiry" size =30 ></td></tr>

The user inputs will be POSTed on another page. Now its not necessary that user will select from all three lists. User may select from one list or two lists or from all three. This will give six cases list A only, B only, C only, AB, BC, AC, ABC. Now is there a way of adding null values to INT as well as DATE in mysql. I tried adding if else like this :

if($_POST['listA_doc_expiry'] ==""){$listA_doc_expiry = 'NULL' ; 
 $listA_id = 'NULL'; }
else{$listA_doc_expiry = $_POST['listA_doc_expiry'];
echo $listA_doc_expiry; }    

And The QUERY :

$result = mysql_query("INSERT INTO hr_information(consultant_id, listA_doc_no, listB_doc_no, listC_doc_no, listA_doc_expiry, listB_doc_expiry,
 listC_doc_expiry, listA_id, listB_id, listC_id) VALUES('$consultant_id','$listA_doc_no','$listB_doc_no','$listC_doc_no','$listA_doc_expiry','$listB_doc_expiry','$listC_doc_expiry','$listA_id','$listB_id','$listC_id');") or die(mysql_error());

It gives the following error Incorrect date value: 'NULL' for column 'listB_doc_expiry' at row 1 how should I enter a NULL date value and INT in DataBase. It works when I manually enter NULL(without '') directly in the query. P.S. I already have declared both as NULL while creating the database.

  • 写回答

2条回答 默认 最新

  • dsjz1119 2014-08-22 13:50
    关注

    You're trying to insert the string 'NULL', rather than NULL. Your quotation marks in your VALUES part are causing the problem for NULL values.

    Even more important is that your code is horribly vulnerable to SQL injection attacks. Rather than constructing a string to represent the statement, and putting the values into the string yourself, you should use a prepared statement, and then bind the values to the fields you introduce. There are lots of examples on SO of how to do that.

    A nice bonus is that this will also get rid of your 'NULL' problem!

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

报告相同问题?

悬赏问题

  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式