Jump to content

prevent duplicate record insertion while refresh the php page - Very urgent


theteche

Recommended Posts

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.

Link to comment
Share on other sites

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,

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.