duanfu3390 2012-06-29 16:28
浏览 128
已采纳

HTML表单,将空白表单设置为NULL

I have a HTML form; I want to be able to set it so that if a field is empty, the field in the DB will actually be NULL and not just have the word NULL in the field. I thought that using this code would help, but it just puts the word NULL in the field. PHP Code:

<pre>
<?php
            if (isset($_POST['oc_item'])) { 
            $oc_item = mysql_escape_string($_POST['oc_item']);
            $oc_itemdesc = (!empty($_POST['oc_itemdesc'])) ? $_POST['oc_itemdesc'] : NULL;

           $sql = "INSERT INTO catalog_dev (oc_item,oc_itemdesc)
            VALUES(''$oc_item','$oc_itemdesc')";

        mysql_query($SQL);
        if (mysql_query($sql)) {
            echo '<strong><em>Your data has been submitted</em></strong><br /><br />';
                } else {
            echo '<p>Error adding submitted info: ' . mysql_error(). '</p>';
        }
        }
        ?></pre>

HTML Code:

<pre>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">

        <table>
        <tr>
            <td>Item Name</td>
            <td><input class="forms" type="text" size="50" maxlength="50" name="oc_item" /></td>
        </tr>
        <tr>
            <td>Item Description</td>
            <td><input class="forms" type="text" size="50" maxlength="50" name="oc_itemdesc" /></td>
        </tr>
</table>

        <p><input type="submit" value="Submit item" /></p>
        </form></pre>

I want the field to actually be NULL and not have the field contain the word NULL. Thanks in advance.

  • 写回答

4条回答 默认 最新

  • douzi2785 2012-06-29 16:31
    关注

    If you want to write NULL to a MySQL database, you really have to put NULL there without quotation marks.

    Like this:

    INSERT INTO table (column, column2) VALUES ('mystring', NULL);
    

    It’s always a bit effort if you want to do this manually by hand, because you would have to make if-conditions for the query.

    Remember: PHP null != MySQL NULL. They both do not know each other at all.

    But I am still wondering, what does that all have to do with the question name? (SELECT unless)

    You could write your code like this:

    $oc_item = mysql_escape_string($_POST['oc_item']);
    $oc_itemdesc = (isset($_POST['oc_itemdesc']) && trim($_POST['oc_itemdesc']) != '') ? "'" . mysql_escape_string($_POST['oc_itemdesc']) . "'" : 'NULL';
    
    $sql = "INSERT INTO catalog_dev (oc_item,oc_itemdesc)
            VALUES('" . $oc_item . "', " . $oc_itemdesc . ")";
    # sorry for adding " . all the time, but I dislike variables within strings :D
    

    But I have to admit I do not like that much either, as it moves the duty to wrap quotation marks around MySQL strings away from the SQL-query itself.

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

报告相同问题?

悬赏问题

  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站