Welcome Guest, Not a member yet? Register   Sign In
Trying to update database with textarea...
#1

I'm trying to update my database with a textarea (name=postNote). But whenever I press the save button it saves a "0" in the request_notes field in the database no matter what I type in. Here is my view...
Code:
<?php
   foreach($rows as $r){
    $data[] = $r->Submitted;
       if ($notes){
           foreach($notes as $n){
?>
           <p>Notes are for internal use/collaboration only</p>
           <table class="detail">
               <tr>
                   <td><textarea name="postNote" rows="8" cols="80"><?php echo $n->request_notes; ?></textarea><br>
               </tr>
           </table>    
           <br />
           <table class="detail">
               <tr>
                   <td><div data-ot="This will save changes made to notes" data-ot-delay="1"><?php echo anchor("main/saveNotes/$r->Submitted", 'Save changes', 'class="button icon arrowdown"'); ?></div></td>
               </tr>
           </table>
<?php
           }
       } else {
?>
           <p>Notes are for internal use/collaboration only</p>
           <table class="detail">
               <tr>
                   <td><textarea name="postNote" rows="8" cols="80"></textarea><br>
               </tr>
           </table>
           <br />
           <table class="detail">
               <tr>
                   <td><div data-ot="This will save changes made to notes" data-ot-delay="1"><?php echo anchor("main/saveNotes/$r->Submitted", 'Save changes', 'class="button icon arrowdown"'); ?></div></td>
               </tr>
           </table>
<?php
       }
   }
?>
and here is my controller ....
Code:
   public function saveNotes(){
    $this->load->model('model_data');
    if($this->model_data->isNote()){
        //the row exists so call the update function from the model
        $this->model_data->updateNotes();
      
    } else {
        //the row doesnt exist yet, call the insert functon from the model
        $this->model_data->insertNotes();
        
    }
    $this->request_details();
   }
and here is my model...
Code:
function isNote(){
       $this->db->where('submit_time', $this->uri->segment(3));
       $query = $this->db->get('apps_request_notes');
       
       if ($query->num_rows() == 1){
           return true;
       } else {
           return false;
       }
   }
   
   function updateNotes(){
       $data = array(
        'request_notes' => $this->input->post('postNote')
    );
       $this->db->where('submit_time', $this->uri->segment(3));
    $this->db->update('apps_request_notes', $data);
   }
   
   function insertNotes(){
       $data = array(
        'request_notes' => $this->input->post('postNote'),
               'submit_time' => $this->uri->segment(3)
    );
       
    $this->db->insert('apps_request_notes', $data);
   }
Not sure whats happening. Its not giving me any errors or anything. It just always saves "0" when I press save. Thanks for any help you can give.
Reply
#2

(This post was last modified: 11-19-2014, 01:55 PM by Rufnex.)

for the update stuff you need a third parameter : the id or matching key of the dataset:

PHP Code:
$this->db->update('mytable'$data"id = 4");
or as array
$this->db->update('mytable'$data, array('id' => $id)); 

Look here at the documentation: http://www.codeigniter.com/userguide3/da...-db-update

Reply
#3

I think thats what the
$this->db->where('submit_time', $this->uri->segment(3));
does right above the
$this->db->update('apps_request_notes', $data);

Do you think maybe its because I didn't name my anchor and give any form action or method for my textarea?
Reply
#4

(This post was last modified: 11-19-2014, 02:33 PM by Rufnex.)

Sorry . dont saw the where part, your right ;o)
For the Form stuff you have to sumit the form by an submit event:

Code:
<form action="/..." method="post">
<textarea name="postNote" rows="8" cols="80"><?php echo $n->request_notes; ?></textarea>
<button type="submit">Submit</button>
</form>

You can't submit a form by an html anchor.

Reply
#5

I figured it out. It was because I had no form_open, form_close, and I needed to change the anchor to a form_submit. Thanks
Reply
#6

Your welcome.

Reply




Theme © iAndrew 2016 - Forum software by © MyBB