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 Matlab在app上输入带有矩阵形式的初始条件发生错误
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址
  • ¥15 elmos524.33 eeprom的读写问题
  • ¥15 用ADS设计一款的射频功率放大器