Prevent Empty database insertions

Discussion in 'MySQL' started by SiteExpress, Nov 10, 2006.

  1. #1
    I have created a php/html based form that inserts data into a MySQL database. However, I need to add something that will prevent an empty row from being created. In other words, if I leave the form blank and hit submit, I dont want it to insert just an empty row.

    Suggestions?
     
    SiteExpress, Nov 10, 2006 IP
  2. jeremyweber

    jeremyweber Peon

    Messages:
    112
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Sure, validate that your required fields arent empty.
     
    jeremyweber, Nov 10, 2006 IP
  3. hiredgunz

    hiredgunz Peon

    Messages:
    203
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Right .. probably the easiest way is to use a javascript form/field validation that checks if the fields length = 0 or not before it is submitted .. there are tons of free javascript examples on the net to do this :)

    best,

    Jan
     
    hiredgunz, Nov 14, 2006 IP
  4. mikestrand

    mikestrand Active Member

    Messages:
    237
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    58
    #4
    I'm not sure on your setup but you can validate using php.

    If you are posting the form using POST then just check each field isn't empty using ;
    
    if ($_POST['fieldname'] == "" || $_POST['fieldname2'] == "" ){
    
    echo "Error Message";
    
    }
    PHP:
     
    mikestrand, Nov 16, 2006 IP
  5. SiteExpress

    SiteExpress Well-Known Member

    Messages:
    1,355
    Likes Received:
    153
    Best Answers:
    0
    Trophy Points:
    155
    #5
    Thanks Mike. That is what I ended up doing.
     
    SiteExpress, Nov 16, 2006 IP