Jump to content

Database Not Updating - CodeIgniter


Go to solution Solved by Moorcam,

Recommended Posts

Hi guys,

Been fighting with this and can't get it to work.

Basically, on submit, session is being updated but data is not being sent to the database.

Here is my view_profile.php

<?php
if(!$this->session->userdata('id')) {
	redirect(base_url().'admin');
}
?>
<section class="content-header">
	<div class="content-header-left">
		<h1>Edit Profile</h1>
	</div>
</section>

<section class="content" style="min-height:auto;margin-bottom: -30px;">
	<div class="row">
		<div class="col-md-12">
			<?php
			if($this->session->flashdata('error')) {
				?>
				<div class="callout callout-danger">
					<p><?php echo $this->session->flashdata('error'); ?></p>
				</div>
				<?php
			}
			if($this->session->flashdata('success')) {
				?>
				<div class="callout callout-success">
					<p><?php echo $this->session->flashdata('success'); ?></p>
				</div>
				<?php
			}
			?>
		</div>
	</div>
</section>


<section class="content">
	<div class="row">
		<div class="col-md-12">
				
			<div class="nav-tabs-custom">
				<ul class="nav nav-tabs">
					<li class="active"><a href="#tab_1" data-toggle="tab">Update Information</a></li>
					<li><a href="#tab_2" data-toggle="tab">Update Photo</a></li>
					<li><a href="#tab_3" data-toggle="tab">Update Password</a></li>
				</ul>
				<div class="tab-content">

      				<div class="tab-pane active" id="tab_1">						
						<?php echo form_open(base_url().'admin/profile/update',array('class' => 'form-horizontal')); ?>
						<div class="box box-info">
							<div class="box-body">

								<div class="form-group">
									<label for="" class="col-sm-2 control-label">Display Name <span>*</span></label>
									<div class="col-sm-4">
										<input type="text" class="form-control" name="name" value="<?php echo $this->session->userdata('name'); ?>">
									</div>			
								</div>
								<div class="form-group">
									<label for="" class="col-sm-2 control-label">Email Address <span>*</span></label>
									<div class="col-sm-4">
										<input type="text" class="form-control" name="email" value="<?php echo $this->session->userdata('email'); ?>">
									</div>			
								</div>
								<div class="form-group">
									<label for="" class="col-sm-2 control-label">Phone <span>*</span></label>
									<div class="col-sm-4">
										<input type="text" class="form-control" name="phone" value="<?php echo $this->session->userdata('phone'); ?>">
									</div>			
								</div>
								<div class="form-group">
									<label for="" class="col-sm-2 control-label"></label>
									<div class="col-sm-6">
										<button type="submit" class="btn btn-success pull-left" name="form1">Update Information</button>
									</div>
								</div>
							</div>
						</div>
						<?php echo form_close(); ?>
      				</div>


      				<div class="tab-pane" id="tab_2">
						<?php echo form_open_multipart(base_url().'admin/profile/update',array('class' => 'form-horizontal')); ?>
						<div class="box box-info">
							<div class="box-body">
								<div class="form-group">
						            <label for="" class="col-sm-2 control-label">Existing Photo</label>
						            <div class="col-sm-6" style="padding-top:6px;">
						            	<?php if($this->session->userdata('photo') ==''): ?>
						            		<img src="<?php echo base_url(); ?>public/img/no-photo.jpg" class="existing-photo" alt="profile photo" width="140">
						            	<?php else: ?>
						            		 <img src="<?php echo base_url(); ?>public/uploads/<?php echo $this->session->userdata('photo'); ?>" class="existing-photo" width="140">
						            	<?php endif; ?>							                
						            </div>
						        </div>
								<div class="form-group">
						            <label for="" class="col-sm-2 control-label">New Photo</label>
						            <div class="col-sm-6" style="padding-top:6px;">
						                <input type="file" name="photo">
						            </div>
						        </div>
						        <div class="form-group">
									<label for="" class="col-sm-2 control-label"></label>
									<div class="col-sm-6">
										<button type="submit" class="btn btn-success pull-left" name="form2">Update Photo</button>
									</div>
								</div>
							</div>
						</div>
						<?php echo form_close(); ?>
      				</div>


      				<div class="tab-pane" id="tab_3">
						<?php echo form_open(base_url().'admin/profile/update',array('class' => 'form-horizontal')); ?>
						<div class="box box-info">
							<div class="box-body">
								<div class="form-group">
									<label for="" class="col-sm-2 control-label">Password </label>
									<div class="col-sm-4">
										<input type="password" class="form-control" name="password">
									</div>
								</div>
								<div class="form-group">
									<label for="" class="col-sm-2 control-label">Retype Password </label>
									<div class="col-sm-4">
										<input type="password" class="form-control" name="re_password">
									</div>
								</div>
						        <div class="form-group">
									<label for="" class="col-sm-2 control-label"></label>
									<div class="col-sm-6">
										<button type="submit" class="btn btn-success pull-left" name="form3">Update Password</button>
									</div>
								</div>
							</div>
						</div>
						<?php echo form_close(); ?>
      				</div>
      			</div>
			</div>
		</div>
	</div>
</section>

Here is my controller - Profile.php

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Profile extends CI_Controller 
{
	function __construct() 
	{
        parent::__construct();
        $this->load->model('admin/Model_common');
        $this->load->model('admin/Model_profile');
    }
	public function index()
	{
		$data['setting'] = $this->Model_common->get_setting_data();

		$this->load->view('admin/view_header',$data);
		$this->load->view('admin/view_profile',$data);
		$this->load->view('admin/view_footer');
		
	}
	public function update()
	{
		$error = '';
		$success = '';

		$data['setting'] = $this->Model_common->get_setting_data();

		if(isset($_POST['form1'])) {

			$valid = 1;

			$this->form_validation->set_rules('email', 'Email Address', 'trim|required|valid_email');

			if($this->form_validation->run() == FALSE) {
				$valid = 0;
                $error = validation_errors();
            }

            if($valid == 1) {
	            $form_data = array(
					'email'     => $_POST['email'],
					'name'      => $_POST['name'],
					'phone'     => $_POST['phone']
	            );
	        	$this->Model_profile->update($form_data);
	        	$success = 'Profile Information is updated successfully!';
	        	
	        	$this->session->set_userdata($form_data);

	        	$this->session->set_flashdata('success',$success);
	        	redirect(base_url().'admin/profile');
            }
            else {
            	$this->session->set_flashdata('error',$error);
	        	redirect(base_url().'admin/profile');
            }
		}

		if(isset($_POST['form2'])) {
			$valid = 1;
			$path = $_FILES['photo']['name'];
		    $path_tmp = $_FILES['photo']['tmp_name'];
		    if($path!='') {
		        $ext = pathinfo( $path, PATHINFO_EXTENSION );
		        $file_name = basename( $path, '.' . $ext );
		        $ext_check = $this->Model_common->extension_check_photo($ext);
		        if($ext_check == FALSE) {
		            $valid = 0;
		            $data['error'] = 'You must have to upload jpg, jpeg, gif or png file<br>';
		        }
		    } else {
		    	$valid = 0;
		        $data['error'] = 'You must have to select a photo<br>';
		    }
		    if($valid == 1) {
		    	// removing the existing photo
		    	unlink('./public/uploads/'.$this->session->userdata('photo'));

		    	// updating the data
		    	$final_name = 'user-'.'.'.$ext;
		        move_uploaded_file( $path_tmp, './public/uploads/'.$final_name );
		    			        
				$form_data = array(
					'photo' => $final_name
	            );
	        	$this->Model_profile->update($id,$form_data);
	        	$success = 'Photo is updated successfully!';

	        	$this->session->set_userdata($id,$form_data);
	        	$this->session->set_flashdata('success',$success);
	        	redirect(base_url().'admin/profile');
		    }
		    else {
		    	$this->session->set_flashdata('error',$error);
	        	redirect(base_url().'admin/profile');
		    }
		}

		if(isset($_POST['form3'])) {
			$valid = 1;

		    $this->form_validation->set_rules('password', 'Password', 'trim|required');
		    $this->form_validation->set_rules('re_password', 'Retype Password', 'trim|required|matches[password]');

			if($this->form_validation->run() == FALSE) {
				$valid = 0;
                $error = validation_errors();
            }

		    if($valid == 1) {

		    	$form_data = array(
					'password' => md5($_POST['password'])
	            );
	        	$this->Model_profile->update($id,$form_data);
	        	$success = 'Password is updated successfully!';
	        	
	        	$this->session->set_userdata($id,$form_data);
	        	$this->session->set_flashdata('success',$success);
	        	redirect(base_url().'admin/profile');
		    }
		    else {
		    	$this->session->set_flashdata('error',$error);
	        	redirect(base_url().'admin/profile');
		    }
		}

		$data['setting'] = $this->Model_common->get_setting_data();

		$this->load->view('admin/view_header',$data);
		$this->load->view('admin/view_profile',$id,$data);
		$this->load->view('admin/view_footer');
	}
	
}

And finally, my Model_profile.php (only need to update)

No errors etc.

Any help would be appreciated.

Cheers,

Dan

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Model_profile extends CI_Model 
{

    function update($id) {
        $this->db->where('id',$id);
        $this->db->update('tbl_team_member',$data);
    }
   
}

 

Edited by Moorcam
Link to comment
https://forums.phpfreaks.com/topic/315658-database-not-updating-codeigniter/
Share on other sites

  • Solution

Fixed.

Was not getting id from database.

So, changed

    function update($id) {
        $this->db->where('id',$id);
        $this->db->update('tbl_team_member',$data);
    }

to this

    function update($data) {
        $this->db->where('id',$this->session->userdata('id'));
        $this->db->update('tbl_team_member',$data);
    }

Just in case anyone else runs into same issue.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.