Welcome Guest, Not a member yet? Register   Sign In
Database update submitting zero's when loading a view as well
#1

[eluser]Unknown[/eluser]
When updating a single record in a MySQL table, the non-datetime fields end up with 0's rather than the strings being submitted from the form. This only happens when loading a view in the same function. When I don't load a view, the update works correctly.

I tried moving the update code into a library file but had the same effect.

It is as if the controller function is being called twice, which works fine for the initial form submit but then on the second call, of course no longer has access to the post variables.

If anyone can offer an explanation of this behaviour, I would be really grateful.

This is the code for the function within the controller:
Code:
function usersave() {
        echo "usersave()<br />";
        // Load the custom config file        
        $this->config->load('customconfig');
        
        // Load the libraries
        $this->load->library('CoLoAccessControl');
        
        $oDbContentZ = $this->load->database('ecommerce',TRUE);
        $query = $oDbContentZ->get_where('users', array('user_email'=>$this->input->post('txtEmail')));
        if ($query->num_rows() > 0) {
   // Found existing user - update their details
            echo "Found user<br />";
            $this->coloaccesscontrol->updateuser(
                $this->input->post('txtEmail'),
                $this->input->post('txtNameFirst'),
                $this->input->post('txtNameLast'),
                $this->input->post('ddlUserLevel'),
                $this->input->post('txtPassword'),
                $this->input->post('txtPasswordC'),
                $this->input->post('hdnUser_ID'));
            
            // Set the page display
            $dataHeader['vw_Title'] = "CMS User Maintenance";
            $dataHeader['vw_Options'] = 'Home';
            $dataContent['vw_Content'] = '<p>The user details have been updated. '.
                'Click here'.
                ' to return the user or here to return to CMS home</p>';
            
            // Load the views
            $this->load->view('structure/header', $dataHeader);
            $this->load->view('generic', $dataContent);
            $this->load->view('structure/footer');
        }
        else {
            // No existing user - continue
            echo "No user found<br />";
            $this->coloaccesscontrol->create($this->input->post('txtEmail'), $this->input->post('txtPassword'), $this->input->post('txtNameFirst'), $this->input->post('txtNameLast'), $this->input->post('ddlUserLevel'));
        }
        echo "Function end";
    }

Code for the function being called in the library:
Code:
function updateuser($txtEmail = '', $txtNameFirst = '', $txtNameLast = '', $ddlUserLevel = '', $txtPassword = '', $txtPasswordC = '', $user_id = '') {
        $this->CI =& get_instance();
        
        // Load the custom config file        
        //$this->CI->config->load('customconfig');
        
        // Load the libraries
        $this->CI->load->library('encrypt');
        
        // Load the database
        $oDbContentZ = $this->CI->load->database('ecommerce',TRUE);
        
        $query = $oDbContentZ->get_where('users', array('user_email'=>$txtEmail));
        if ($query->num_rows() > 0) {
   // Found existing user - update their details
            
            if(($txtPassword == $txtPasswordC) && ($txtPassword != "")) {
                // Generate update data with password
                $data = array(
                    'user_email' => $txtEmail,
                    'user_namefirst' => $txtNameFirst,
                    'user_namelast' => $txtNameLast,
                    'user_accesslevel' => $ddlUserLevel,
                    'user_pass' => $this->CI->encrypt->sha1($txtPassword),
                    'user_date_modified' => date("Y-m-d H:i:s", time())
                );
            }
            else {
                // Generate update data without password
                $data = array(
                    'user_email' => $txtEmail,
                    'user_namefirst' => $txtNameFirst,
                    'user_namelast' => $txtNameLast,
                    'user_accesslevel' => $ddlUserLevel,
                    'user_date_modified' => date("Y-m-d H:i:s", time())
                );
            }
            
            //print_r($data);
            
            // Update the database
            $oDbContentZ->update('users', $data,  array('md5(user_id)' => $user_id));
            echo $oDbContentZ->last_query();
        }
        else {
            echo "No user for " . $txtEmail . " found";
        }
    }




Theme © iAndrew 2016 - Forum software by © MyBB