Welcome Guest, Not a member yet? Register   Sign In
Remove data from db table if fail to insert question
#1

(This post was last modified: 08-30-2016, 11:20 PM by wolfgang1983.)

On my controller I have variable called $insert_user which sets data for my user table and model function insert_user().

If that has been inserted it will all the next $variable array which is $insert_userdata and that inserts into my table user_data.

Question If the $insert_userdata table fails to insert into my user_data table what is the best way to remove the user info from user table?

Model


PHP Code:
<?php

class Add_user_model extends CI_Model {

    public function insert_user($data) {
        $this->db->set($data);
        $this->db->insert($this->db->dbprefix 'user');
        return $this->db->insert_id();
    }

    public function insert_userdata($data) {
        $this->db->set($data);
        $this->db->insert($this->db->dbprefix 'user_data');
        if ($this->db->affected_rows() > 0) {
            return true;
        } else {
            return false;
        }
    }




Controller


PHP Code:
<?php

class Add extends MX_Controller {

    private $error = array();

    public function __construct() {
        parent::__construct();
        $this->load->library('form_validation');
        $this->load->library('upload');
        $this->load->model('admin/user/add_user_model');
    }

    public function index() {
        $this->form_validation->set_rules('username''Username''trim|required');
        $this->form_validation->set_rules('firstname''Firstname''trim');
        $this->form_validation->set_rules('lastname''Lastname''trim');
        $this->form_validation->set_rules('email''Email''trim|valid_email');
        $this->form_validation->set_rules('password''Password''trim');
        $this->form_validation->set_rules('confirm_password''Confirm Password''trim|matches[password]');

        if (($this->form_validation->run()) && $this->upload())  {

            $upload_data $this->upload->data();

            $insert_user = array(
                'status' => $this->input->post('status'),
                'date_added' => date('Y-m-d')
            );

            $set_user_id $this->add_user_model->insert_user($insert_user);

            if ($this->checkinsertedtodb($set_user_id)) {

                $insert_userdata = array(
                    'user_id' => $set_user_id,
                    'username' => ($this->input->post('username')) ? $this->input->post('username') : '',
                    'password' => '',
                    'firstname' => ($this->input->post('firstname')) ? $this->input->post('firstname') : '',
                    'lastname' => ($this->input->post('lastname')) ? $this->input->post('lastname') : '',
                    'email' => ($this->input->post('email')) ? $this->input->post('email') : '',
                    'image' => ($upload_data['file_name']) ? $upload_data['file_name'] : ''
                );

                $userdata_inserted $this->add_user_model->insert_userdata($insert_userdata);

                if ($this->checkinsertedtodb($userdata_inserted)) {
                    redirect('admin/user/users');
                

            }
            
        
}

        if (isset($this->error['warning'])) {
            $data['error'] = $this->error['warning'];
        } else {
            $data['error'] = '';
        }

        $data['header'] = Modules::run('admin/common/header/index');
        $data['footer'] = Modules::run('admin/common/footer/index');

        $this->load->view('user/add'$data);

    }

    public function upload($field 'userfile') {
        $users_dir $this->input->post('username');
        $directory FCPATH 'uploads/users/' $users_dir;

        if (isset($_FILES[$field]) && $_FILES[$field]['size'] > 0) {

            if (is_dir($directory)) {
                $this->error['warning'] = 'This ' $directory ' is all ready created!';
            }

            if (mkdir($directory)) {

                $config['upload_path'] = $directory '/';
                $config['allowed_types'] = 'gif|jpg|png';
                $config['max_size'] = 3000;
                $config['max_width'] = 0;
                $config['max_height'] = 0;
                $config['overwrite'] = true;

                $this->upload->initialize($config);

                if (!$this->upload->do_upload($field)) {    
                    $this
->error['warning'] = $this->upload->display_errors();
                }

            }

            return !$this->error;
            
    
}

    public function checkinsertedtodb($inserted) {
        if (!$inserted) {
            $this->error['warning'] = 'Opps somthing has gone wrong could not insert data!';
        }

        return !$this->error;
    }

Update I was thinking of something like

PHP Code:
public function insert_userdata($data) {
        $this->db->set($data);
        $result $this->db->insert($this->db->dbprefix 'user_data');
        
        if 
($result) {
        
            return true
;
        
        
} else {
            
            $this
->db->where('user_id'$data['user_id']);
            $this->db->delete('user');

            $this->db->where('user_id'$data['user_id']);
            $this->db->delete('user_data');
        }
 


Attached Files
.php   Add.php (Size: 3.2 KB / Downloads: 51)
.php   Add_user_model.php (Size: 435 bytes / Downloads: 53)
There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply
#2

Hi,

My suggestion would be to do it in the controller

PHP Code:
if ($this->checkinsertedtodb($userdata_inserted)) {
 
   redirect('admin/user/users');
}
else {
 
  // insert in user_data table failed 
 
  $this->user_model->delete($set_user_id);
 
  //remove uploaded file 
 
  // set error message 
  // send result to browser

A good decision is based on knowledge and not on numbers. - Plato

Reply
#3

Use Transactions
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#4

(08-31-2016, 02:53 AM)InsiteFX Wrote: Use Transactions

I have never herd of that do you have example?
There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply
#5

(08-31-2016, 03:02 AM)wolfgang1983 Wrote:
(08-31-2016, 02:53 AM)InsiteFX Wrote: Use Transactions

I have never herd of that do you have example?
https://www.codeigniter.com/user_guide/d...tions.html
Keep calm.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB