简体   繁体   中英

Update record in database using codeigniter

Hi there Iam using codeIgniter and I have managed to simply post a id number and phone number into the a table named "offers" both fields are INT, however when i try update a phone number corresponding to a specific id I see no changes in the database. I have listed my controller , model and view below

newOffer controller

   <?php 
    if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  //insert data into db using offer_model model
  // other option update submit 
  class newOffer extends CI_Controller {
       function addOffer() {
       //if the form is submitted           
       $this->load->view("check");
       $this->load->model("offer_model");
         if ($this->input->post('mysubmit')) {
             $this->offer_model->entry_insert();
          }
       }

        function updateOffer (){
           $this->load->view("check2");
           $this->load->model("offer_model");
           if ($this->input->post('mysubmit')) {
                $this->offer_model->upddata();
            }
         }
    }
  ?>

offer_model

class offer_model extends CI_Model{

 public function entry_insert(){
      $data = array(
           'idNum' => $this->input->post('idNum'),
            'phneNum' => $this->input->post('phneNum'),

        );
      $this->db->insert('offers',$data);
  }

   public function upddata($data) {
    $this->db->where('idNum', $idNum);
    $this->db->update('data' ,$data);
    //extract($data); 
  //$data['OfferName']
  //$this->db->where('OfferName' ,  $data['OfferName']); 
    //$this->db->update($Offers, array('OfferName' => $OfferName)); 
   return true;
    }
}
 ?>

The view to update the values

<?form _open(base_url()."index.php/newOffer/updateOffer")?>
<div class="form">
 // <?php echo form_open('newOffer/addOffer'); ?>
<legend>Please enter details for your new offer</legend>
<label for="ID Number">ID Number:  <span class="required">*</span></label>
<input type="text" name="idNum" id="idNum" placeholder="Please enter ID Number/>
<label for="phone Number">Phone Number:</label>
<input type="text" name="phneNum" id="phneNum " placeholder="Please enter phone Number"/>

 <fieldset class="submit_field">
     <?php echo form_submit('mysubmit', 'Submit Form'); ?>
 </fieldset>
 </div><!-- end of form div -->
   ?>

You aren't passing any data to your model here

$this->offer_model->upddata();

You need to add something like

$this->offer_model->upddata($this->input->post());

Also in your Model code $idNum is undefined you will need to provide that too.

eg:

public function upddata($data) {
  $idNum = $data['idNum'];
  unset($data['idNum']);
  $this->db->where('idNum', $idNum);
  $this->db->update('offers' ,$data);
  return true;
}
//etc

Make sure you are feting from the same table in which you are inserting or updating data.

public function upload($id_akun, $data)
    {
        $table = "foto";

        $this->db->where("id_akun", $data['id_akun']);
        $count = $this->db->count_all_results($table);


        if ($count < 1) {
            $this->db->insert($table, $data);
        } else {
            $this->db->where('id_akun', $data['id_akun']);
            $this->db->update($table, $data);
        }
    }

Good luck :)

Use construct for load your model in controller And always pass the post data to the model function then only you will find the form post data into model function.

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

  function __construct()
  {
        parent::__construct();
        $this->load->model("offer_model");
  }

  class newOffer extends CI_Controller {
       function addOffer() {
       //if the form is submitted           
       $this->load->view("check");
       $this->load->model("offer_model");
         if ($this->input->post('mysubmit')) {
             $CI_Post = $$this->input->post();
             $this->offer_model->entry_insert($CI_Post);
          }
       }

        function updateOffer (){
           $this->load->view("check2");

           if ($this->input->post('mysubmit')) {
              $CI_Post = $$this->input->post();
              $this->offer_model->upddata($CI_Post);
           }
         }
    }
?>

Here is model which getting form post data in array.

<?php
class offer_model extends CI_Model{

     public function entry_insert($param=""){
          $data = array(
               'idNum' => $param['idNum'],
                'phneNum' => $param['phneNum'],

            );
          $this->db->insert('offers',$data);
          return $this->db->insert_id();
      }

     public function upddata($param="") {
      $data = array(
             'idNum' => $param['idNum'],
              'phneNum' => $param['phneNum'],

      );
      $this->db->where('idNum', $idNum);
      $this->db->update('data' ,$data);
      return $this->db->affected_rows();
     }
}
?>

in your model do something like

public function upddata() {
    $data=array();
    $data=$this->input->post(); //get all post value to data array
    $idNum=$data['idNum'];
    unset($data['idNum']); // unset unnecessary values 
    $this->db->where('idNum', $idNum)->update('offers' ,$data);
    return true;
} 

this is the procedure.If you have 100 input fields then its not possible to add every value to an array. Please let me know if you face any problem.

update

change

<?php echo form_open('newOffer/addOffer'); ?>

to

<?php echo form_open('newOffer/updateOffer'); ?>

in The view to update the values

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM