SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict Mainer82's Avatar
    Join Date
    Aug 2003
    Location
    Maine, USA
    Posts
    249
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Submission form, set blank forms to 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:
    <?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>';
            }
            }
            
    ?>
    HTML Code:
    <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>
    Any suggestions? Thank you,
    Get to know the man behind the screen

  2. #2
    SitePoint Member centrex's Avatar
    Join Date
    Feb 2011
    Location
    San Diego
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The ternary operator is assigning a string value of NULL to $oc_itemdesc if the field is blank. Removing the the quotes around it should yield the expected result.

    Code PHP:
    $oc_itemdesc = (!empty($_POST['oc_itemdesc'])) ? escape($_POST['oc_itemdesc']) : NULL;

  3. #3
    SitePoint Addict Mainer82's Avatar
    Join Date
    Aug 2003
    Location
    Maine, USA
    Posts
    249
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I tried that, now the field is empty in the database, however it's not actually marked as NULL when being viewed in PHPmyadmin. Is there any way to set it to NULL?
    Get to know the man behind the screen


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •