theteche Posted June 12, 2008 Share Posted June 12, 2008 hi, I am new to php. ??? i have a small form with staff name ,staff id ,date etc fields.i am using mysql database.Inserting the record in to database is working fine for me.but if you refresh the page after clicking submit button,it adds empty records to the database. note:after submitting the page should display the form.. How to avoid that empty records in the database? please provide the solution...Thanks in advance. Quote Link to comment Share on other sites More sharing options...
bluejay002 Posted June 12, 2008 Share Posted June 12, 2008 you need to check whether $_POST has been set using isset(). given that it has a value, trim the values using trim() then check if its empty using empty(), only fields with value should be saved. you may use a hidden variable to check if it has a valid value, say 1 would allow it to save or 0 if not. this value is initially 0... once the form is submitted, change to 1. after all the operations, change back to 0. this way, it will not set... of course, this would mean you need to check that variable is 1 before you want to make any operation. another way to do it is to reload the page. another way is to unset the $_POST vars. and there are a lot of ways! Jay, Quote Link to comment Share on other sites More sharing options...
xtopolis Posted June 12, 2008 Share Posted June 12, 2008 2 things: MYSQL: First search for something unique that is in each record. This could possibly be Staff ID, depending on what type of records you're entering... Have the mysql first search for a record with a matching unique id (use a COUNT(*) or mysql_num_rows() or w/e), if none are found then allow the record to be inserted, otherwise you can error, or 'pretend to insert it'. PHP: If you have blank records being inserted, use your isset($_POST['elementid']) to check for null/empty values. If they are found, display the form again. If they are set, perform whatever validation, then proceed to the mysql section where it checks for a duplicate value, and if none are found, it adds the record. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.