Connecting Tech Pros Worldwide Help | Site Map

Text link does accidental dbl insert

daJunkCollector's Avatar
Member
 
Join Date: Jun 2007
Posts: 75
#1: Jul 1 '09
Hey, I have a simple web app that requires the user to click a text link to add a section to the form. As you will see from the following code (PHP/mySQL), the link inserts a row into the database table. The problem is that, during testing, sometimes when I click the link, two rows are added instead of only one. I only want one row to be added. Could you please help me figure out my bug?

Expand|Select|Wrap|Line Numbers
  1. // Insert and delete actions for Education, Work Experiences, and References
  2.         if($_GET['action']=="insertEdu" && $_GET['checkId'] == $_SESSION['usrId'] && !isset($_SESSION['keyEdu']))
  3.         {
  4.  
  5.             $sqlInsertStatementEdu ="INSERT INTO edu5000 (eduId, eduUsrId, eduName, eduCity, eduState, eduStatus, eduStartDte, eduEndDte, eduDegree, eduMajor, eduMinor, eduExtra, eduLastChgUsr, eduLastChgDte) VALUES (NULL , '".$_SESSION['usrId']."', NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL , '".$_SESSION['usrEmail']."', now())";            //PROBLEM! INSERTING MULTIPLE ROWS SOMETIMES, NEXT ADJUST edu fields, NEXT adjust work exp, NEXT references, NEXT non-textbox controls
  6.             $resultEdu0   = mysql_query($sqlInsertStatementEdu); // Insert row into edu
  7.  
  8.             if (!$resultEdu0) {
  9.                 print('Sorry, database error.  Please try again later. If problem persists, contact administrator (reference error 2784)');
  10.             } else {
  11.                 $_SESSION['keyEdu'] = mysql_insert_id(); //sets new edu entry session key to prevent multiple new rows
  12.             }
  13.         }
and the body work...

Expand|Select|Wrap|Line Numbers
  1. <div><a href="resume.php?action=insertEdu&checkId='.$_SESSION['usrId'].'">(+ add education entry)</a></div>unset($_SESSION["keyEdu"]);//END EDUCATION
daJunkCollector's Avatar
Member
 
Join Date: Jun 2007
Posts: 75
#2: Jul 1 '09

re: Text link does accidental dbl insert


http://support.mozilla.com/tiki-view...8945&forumId=1
dlite922's Avatar
Expert
 
Join Date: Dec 2007
Location: Moon, Dark Side
Posts: 1,094
#3: Jul 1 '09

re: Text link does accidental dbl insert


I think you need better design because every time the page executes (such as using the Back button) the page executes again (sending the value to DB once more)

If this is suppose to add dynamic fields, I would do it with Ajax.

If this is more resume Edition, I would do this:

With JavaScript I'd insert a set of fields (row) to my table (let's say the Education Section) and when the user is done editing the resume, clicking "Save" would write it to the DB.

Let me know what the use case is, I'm not sure at this point why you're doing what you're doing.




Dan
Reply