0

Iam developing 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 keep getting the following error

Unknown column 'mysubmit' in 'field list'

UPDATE offers SET phneNum = '078444', mysubmit = 'Submit Form' WHERE idNum = '12'

Filename: C:\xampp\htdocs\project\system\database\DB_driver.php

I have listed my controller , model and view below

newOffer/controller

     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("check");
 $this->load->model("offer_model");
 if ($this->input->post('mysubmit')) {
    // $this->offer_model->upddata();
  $this->offer_model->upddata($this->input->post());
   }


  }
 }
 ?>

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) {
             $data=array();

         $idNum = $data['idNum'];
    $data=$this->input->post(); //get all post value to data array

    unset($data['idNum']); // unset unnecessary values 
   $this->db->where('idNum', $idNum)->update('offers' ,$data);
   return true;
}



}


 ?>

The view // Please enter details for your new offer

        <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 -->
    ?>

3 Answers 3

2

Your issue seems to be in your model function:

 public function upddata($data) {
         $data=array();

     $idNum = $data['idNum'];
$data=$this->input->post(); //get all post value to data array

unset($data['idNum']); // unset unnecessary values 
$this->db->where('idNum', $idNum)->update('offers' ,$data);
return true;
}

the second line, $data=array(); technically wipes the data from the passed in variable and creates a new blank array. So the third statement will return null for $idNum. Instead you can use the $data variable directly to construct your where clause.

public function upddata($data) {
//do necessary data validation here or in the controller.
 $idNum = $data['idNum'];
//not needed you already have the data in the $data var//$data=$this->input->post();
unset($data['idNum']); // unset unnecessary values 
$this->db->where('idNum', $idNum);
$this->db->update('offers' ,$data);
return true;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Indeed I also forgot to mention that your table probably doesn't have the mysubmit column, you would need to unset this as well as per Bill Garrison's comment.
The model layer should not be accessing $this->input->post(). The model should perform database processes while being totally ignorant of the source of its input data. It is the responsibility of the controller to collect all required data and pass that payload to the model.
0
public function upddata($data) {

             $idNum = $data['idNum'];

             $offers_data = array("phneNum" => $data['phneNum']);

             unset($data['idNum']); // unset unnecessary values 
             $this->db->where('idNum', $idNum)->update('offers' ,$offers_data );
             return true;
}

4 Comments

thanx a lots I adjust and put extract($data); at the beginning of the function and it works alright
I don't agree with this at ALL. The method is an update method. THIS confines the method to ONLY update the phone number. If he tries to update anything else it will not work. His problem (as i mentioned in my answer) is that he had the POST data from his submit input in the array.
he wanted to update only phone number.please see the form mention in question.
For the record, CI's update() method can receive the WHERE clause data as an associative array in the method's 3rd parameter.
0

When a form is submitted the submit button is ALSO an input and gets sent along with the post. You then send it to the update method and so it is looking for the column "mysubmit" (the name of your submit input) to update to the value of your submit button.

You need to unset($data['mysubmit']) before sending it to the update method.

You also need to get rid of $data = array();. I assume that was there for debugging purposes.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.