• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Refreshing re-enters the data in the db again and again. Help?

#6
[eluser]boltsabre[/eluser]
I agree with Otemu's last statement, for pretty much all my model stuff, I actually check if something has been inserted (or updated).

Code:
//model, in pseudo code for a new user insert
//here run your DB insert and check if it actually worked
if($inserted){
   return true //or return $user_id
}else{
   // there was a db failure somewhere and our new user was NOT inserted
   // Here I do some custom error logging for my own benefit and...
   return false
}

And in the controller
Code:
$run = $this->user_model->insert_new_user($post_data);
   if ($run){
      //user successfully inserted into DB, do whatever you need to do now and then...
      redirect('successfully_registered');
   }else{
      //user NOT inserted into the DB for some reason, do whatever you want:
      //redirect back to your homepage, or set some flashdata letting the user
      // know there was a problem and to try again and then reload the view for them, etc
   }

It makes for a lot more coding, but it will save on problems later on. What would happen if there was a DB problem and the user was NOT inserted to the DB, but you didn't check for it and just set a "logged_in" session variable and gave them full access to your website even though they don't exist and there is no "user_id" for them.
Is it likely to happen? Let's hope not, but with shared DB servers you never know... what if they have a problem, or go down for maintenance, or one of the other shared websites experiences a DoS attack shutting down the servers, etc.
With stuff like "inserting a forum post/reply", it isn't that important to do these checks as the page just wont display the post if it doesn't exist in the DB, but for stuff like inserting a new user, yeah, it's pretty important


Messages In This Thread
Refreshing re-enters the data in the db again and again. Help? - by El Forum - 05-30-2012, 03:55 AM

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.