tags:

views:

146

answers:

6

If needed I will place the code I just wanted to know what code will I need and where do I place it to stop a form that has empty fields from being entered into the database.

A code snippet would be appreciated. This question may be too vague let me know.

I'm using PHP and mysql.

+3  A: 
if(isset($_POST['formfield'])) {
    //add to db
}
else {
    //don't add to db
}

http://www.php.net/isset

Mr. Smith
comment slashes backwards bro.
Evernoob
What you on about? :P
Mr. Smith
+1  A: 

Check if the submitted form is valid, if it is not, don't insert it into the database.

tliff
+3  A: 

You have to check the form content before inserting into the database.

It is also important to check if the content is empty. The variable might exist but the content maybe empty. That is something you don't want, unless the field is optional.

Something like this:

<?php
    if (isset ($_POST['field']) && strlen($_POST['field'] >0))
    {
        insert_to_database();
    }
?>
rogeriopvl
+1  A: 

you should make some fields required. for example the form has a required field : name of course you need to know the name of the people that contact you.. so name should be a required field. in the action file you need to put an if condition that will fix that problem :

if(isset($_POST['name'])){
//script that adds the variables to the database
}else{ echo 'the required field : name. was not filled, please try again. Thank you'; }
// it can work without else but you could output something if the required field is not filled in

also you could make e-mail a required field and find a php script that validates the e-mail so that it`s not a fake one :-/ anyway hope this might help you

DanTdr
+1  A: 

Besides checking if fields are empty as others have suggested, you might want to entirely avoid queries being run on a page refresh. To do this I use "intermediate" scripts that do the processing and when they end they redirect the user to the final page which displays whatever is needed: in this way refreshing the last page does nothing on the database because all the DB processing code is in another script that you won't reach unless you manually resubmit the form.

kemp
What you're describing is the Post/Redirect/Get pattern, it is indeed a good idea to follow that, see http://en.wikipedia.org/wiki/Post/Redirect/Get
Wim
A: 

For preventing the page refresh, a cookie will be usefull; To check the empty fields, isset()

<?php
//in the top of the page, BEFORE every output!
if(isset($_COOKIE['givemeaname'])){
    setcookie('givemeaname', 'some foo&bar data, maybe a timestamp?', time()+3600);
    //now check the mandatory fields and
    //do whatever you want with your db
}else{
    //do nothing, the page is been refreshed!
}
//do whatever you have to do now!
?>

If you dont like to move your db-about code in the top of the page, in the cookie check you can valorize an boolean var, and then just chet it... choose your path.

Instead of the cookie, you can use a session, if is more convenience for you.. but the behavior should be clear ;)

This solution will save you all the times, becose even if you are hitting the page with a POST form, some browser ask to resend the information again when prompted to refresh the page (and the lazy users usually click 'yes' without reading or understand what it means ;)

DaNieL