Welcome Guest, Not a member yet? Register   Sign In
Logic with multiple queries
#1

[eluser]xtremer360[/eluser]
I'm curious to know if someone could explain some logic on how to write a specific function. In my register controller I have a function called create_user. When the function gets called it runs a function in my model called insert user that inserts set data such as username, email address, name, etc. to the users table as a new row. It returns te user_id (auto increment id) back to the create user function then if the user_id is more than one the runs the insert_registration_details model function that inserts user data into the registrations table like the ip address, browser, etc.. If it fails to insert into that table I want it to go back and delete the user in the users table. How can I achieve this if the database user doesn't have delete privileges.
#2

[eluser]xtremer360[/eluser]
I was looking at transactions and wanted to know if that's what I needed and if so then is this correct?

Code:
public function submit()
    {
        $this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean|min_length[6]|max_length[12]|alpha_numeric|strtolower|callback__is_username_available');
        $this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|min_length[6]|max_length[12]|alpha_numeric');
        $this->form_validation->set_rules('confirm_password', 'Confirm Password', 'trim|required|xss_clean|min_length[6]|max_length[12]|alpha_numeric|matches[password]');
        $this->form_validation->set_rules('first_name', 'First Name', 'trim|required|xss_clean|alpha');
        $this->form_validation->set_rules('last_name', 'Last Name', 'trim|required|xss_clean|alpha');
        $this->form_validation->set_rules('email_address', 'Email Address', 'trim|required|xss_clean|valid_email|callback__is_email_address_available');
        
        // Tests to see if the form was a valid submission
        if ($this->form_validation->run())
        {
            $post_username = $this->input->post('username');
            $post_password = $this->input->post('password');
            $post_first_name = $this->input->post('first_name');
            $post_last_name = $this->input->post('last_name');
            $post_email_address = $this->input->post('email_address');
            $registration_ip_address = $this->input->ip_address();
            $registration_key = md5(rand().microtime());
            $registration_date = gmdate('Y-m-d H:i:s');
            
            // Defines the user_data variable but creating a new user.
            $generated_password_hash = $this->functions_model->generate_password_hash($post_password);
            
            $this->db->trans_start();
            $user_id = $this->users_model->create_user($post_username, $generated_password_hash[0], $generated_password_hash[1], $post_first_name, $post_last_name, $post_email_address);
            if ($user_id > 0)
            {
                $this->users_model->insert_user_registration_details($user_id, $registration_ip_address, $registration_key, $registration_date);
                $this->users_model->create_user_profile($user_id);                
            }
            $this->db->trans_complete();
            
            if ($this->db->trans_status())
            {
                $this->email->from('[email protected]', 'KOW Management Team');
                $this->email->to($user_data->email_address);
                $this->email->subject('KOW Manager Registration Details');
                $this->email->message('Hello ' . $post_first_name . ' ' . $post_last_name . ',<br /><br /> We would like to thank you for joining the KOW fan site. We emailed you to provide you the details you gave upon registering.<br /><br /> Username: ' . $post_username . ' <br />Password: ' . $post_password . '<br /><br />Please click the following to activate your account as you will not be able to login without doing so.<br /><br /><a href="http://www.kansasoutlawwrestling.com/kowmanager/activation/' . $user_data['user_id'] . '/' . $user_data['registration_key'] . '">Click here to activate</a><br /><br /> Thanks again for joining! <br /><br />KOW Management Team');
                $this->email->send();
                $output_array = array( 'error' => FALSE, 'message' => 'Successful registration! We will be emailing you a confirmation email shortly!' );
            }
            else
            {
                $output_array = array( 'error' => TRUE, 'message' => 'There was a problem creating the user! Please try again!' );  
            }
            
            $user_data = $this->mylib->create_user( $post_username, $post_password, $post_first_name, $post_last_name, $post_email_address );    
        }
        else
        {
            $output_array = array( 'error' => TRUE, 'message' => validation_errors() );
        }
        echo json_encode( $output_array );
    }

#3

[eluser]DarkManX[/eluser]
you have the user_id, just delete the row where the id is the same...
transactions should be used with more complicated inserts.
#4

[eluser]xtremer360[/eluser]
Can you explain to me why that is?
#5

[eluser]DarkManX[/eluser]
why you should use transactions in the your given case?
#6

[eluser]xtremer360[/eluser]
Yes that way I can better understand it from my code point of view.
#7

[eluser]DarkManX[/eluser]
hope you know what transactions are...
by starting a transaction session all changes, inserts etc you make inside of this session will be stored temporary in the database, so you can use the modifications inside your transaction. if everything gone well, you can store them parmanently in your db. in case something went "wrong" and you need to redo all actions you have executed you just rollback the transaction. and the db will be in the state before starting transaction.

why would you need to do that stuff if you just want to delete a simple row from the database? sure, you can do it that way but it isnt as performant as it could be.
#8

[eluser]xtremer360[/eluser]
Well from what I gather from the docs and from you that's what I need because the user gets inserted into the users table and then other inserts are done and predicated on the fact that the user got inserted so if not I need the user deleted.
#9

[eluser]DarkManX[/eluser]
as i told you, you can do whatever you want but it isnt that permormant.
you also can backup your whole db before starting the script and if something doesnt wrong just reapply the backedup database - just giving you an exaggerated example. because thats kind of what you are doing to get rid of just 1 data-row.
#10

[eluser]xtremer360[/eluser]
I thank you for replying so much. I'm just trying to learn why some routes might be taken easier over others is all.




Theme © iAndrew 2016 - Forum software by © MyBB