Skip to content
Advertisement

Why I cannot update the data?

original data echo data upated data does not updated into databse Controller Update I got the id from the form and pass the value to show the data on the next page. It will show certain data based on the user click which row button.

<?php

class ControllerUpdate extends CI_Controller{

    public function __construct(){

        parent:: __construct();

        $this->load->model('common/ModelUpdate');

        $this->load->database();

        $this->load->library('form_validation');

        $this->load->helper(array('form', 'url'));

    }

    public function index(){
         
        //show data by id
        $id = $this->input->get('id');

        $data['showData'] = $this->ModelUpdate->showData($id);

        $this->load->view('template/update', $data);

        if($this->input->post('updates')){
            $id = $this->input->post('id');
            $site_name = $this->input->post('names');
            $address = $this->input->post('addresses');
            $description = $this->input->post('descriptions');
            $image = $this->input->post('images');

            $this->ModelUpdate->updateData($id, $site_name, $address, $description, $image);    
            echo "<script>alert('Data Updated Successfully');</script>";

        }

    }
    
}
?>

Model Update I have passed the id when updating the data, and It will not update the data but echo that the data was updated successfully.

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

class ModelUpdate extends CI_Model {

    public function showData($id){

        $sql = "SELECT DISTINCT * FROM `web_listing` WHERE `id` = '".$id."'";
        $result = $this->db->query($sql);
        return $result;
    }

    public function updateData($id, $site_name, $address, $description, $images){

        //, `image_file` = '$image_file'
        
        echo "<pre>";
        print_r($id ."<br>". $site_name ."<br>". $address ."<br>". $description ."<br>". $images);
        echo "</pre>";
        
        $sql = "UPDATE `web_listing` SET `website_name` = '$site_name', `website_address` = '$address', `description` = '$description', `image_name` = '$images' WHERE `id` = '".$id."'";

        $query = $this->db->query($sql);
        return $query;
        
    }
}

?>

Update View I also have passed the id in the action and also the value in the submit button. But the update function still cannot work.

<?php
        if($showData->num_rows() > 0)
           {
             foreach($showData->result_array() as $row){
            $ids = $row['id'];
?>

  <form class="form-horizontal" id="form" method="post" action="<?php base_url('updateData/$ids')?>" enctype="multipart/form-data">

    <div class="form-group">
      <label class="control-label col-sm-3">Id:<span class="required">&nbsp;*</span></label>
      <div class="col-sm-9">
        <input type="text" disabled="disabled " class="form-control" id="id" name="id" value="<?php echo $row['id'] ; ?>" required>
      </div>
    </div>

    <div class="form-group">
      <label class="control-label col-sm-3">Website Name:<span class="required">&nbsp;*</span></label>
      <div class="col-sm-9">
        <input type="text" class="form-control" id="names" name="names" value="<?php echo $row['website_name'] ; ?>" required>
      </div>
    </div>
    
    <div class="form-group">
      <label class="control-label col-sm-3">Website Address:<span class="required">&nbsp;*</span></label>
      <div class="col-sm-9">
        <input type="text" class="form-control" id="addresses" name="addresses" value="<?php echo $row['website_address']  ; ?>" required>
      </div>
    </div>
    
    <div class="form-group">
      <label class="control-label col-sm-3">Description:<span class="required">&nbsp;*</span></label>
      <div class="col-sm-9">          
        <input type="text" class="form-control" id="descriptions" name="descriptions" value="<?php echo $row['description']  ; ?>" required>
      </div>
    </div>

    <div class="form-group">
      <label class="control-label col-sm-3">Image:<span class="required">&nbsp;*</span></label>
      <div class="col-sm-9">          
        <input type="file" class="form-control" id="images" name="images" value="<?php echo $row['image_file']  ; ?>" >
      </div>
    </div>

    <div class="form-group">
        <div class="col-sm-offset-3 col-sm-9">
        <button type="submit" value="<?php echo $ids ; ?>" id="updates" name="updates" class="btn btn-default" >Update</button>
        <a href="<?php base_url()?>editData"><button type="button" value="back" id="back" name="back" class="btn btn-default" onclick="back()">Back</button></a>
      </div>
    </div>

  </form>
</div>
</div>
        <?php
       }
                } 
           
            ?>

Advertisement

Answer

Disabled form elements are not sent to PHP so $this->input->post('id') will be blank which is why your echo_data image/screenshot does not show the ID. Change the form element to readonly instead of disabled or use $this->input->get('id'); which appears to be present anyway as you use it to get the data initially.

Also it doesn’t appear you are handling the uploaded file (images) correctly, please look into that.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement