• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
unable to update record

#1
Wink 
I am new to web development and trying to learn the codeigniter. I am following tutorial ever thing is ok. but the edit / update page is not working. May be this is a stupid topic but I am having problem to learn MVC concept. any other suggestion that I can learn the MVC.

here is controller :

Code:
     public function update_student_view() {
        $this->load->helper('form');
        $roll_no = $this->uri->segment('3');
        $query = $this->db->get_where("stud",array("roll_no"=>$roll_no));
        $data['records'] = $query->result();
        $data['old_roll_no'] = $roll_no;
        $this->load->view('Stud_edit',$data);
     }
 
     public function update_student(){
        $this->load->model('Stud_Model');
            
        $data = array(
           'roll_no' => $this->input->post('roll_no'),
           'name' => $this->input->post('name')
        );
            
        $old_roll_no = $this->input->post('old_roll_no');
        $this->Stud_Model->update($data,$old_roll_no);
            
        $query = $this->db->get("stud");
        $data['records'] = $query->result();
        $this->load->view('Stud_view',$data);
        
     }

here is model:


Code:
     public function update($data,$old_roll_no) {
        $this->db->set($data);
        $this->db->where("roll_no", $old_roll_no);
        $this->db->update("stud", $data);
     }


here is edit view

Code:
<!DOCTYPE html>
<html lang = "en">

  <head>
     <meta charset = "utf-8">
     <title>Students Example edit</title>
  </head>
    
  <body>
     <form method = "" action = "">
        
        <?php
           echo form_open('Stud_controller/update_student');
           echo form_hidden('old_roll_no',$old_roll_no);
           echo form_label('Roll No.');
           echo form_input(array('id'=>'roll_no','name'=>'roll_no','value'=>$records[0]->roll_no));
           echo "<br/>";
                
           echo form_label('Name');
           echo form_input(array('id'=>'name','name'=>'name','value'=>$records[0]->name));
           echo "<br/>";
                
           echo form_submit(array('id'=>'submit','value'=>'Edit'));
           echo form_close();
            echo $this->db->last_query();
        ?>
            
     </form>
  </body>
    
</html>

here is stud_view view


Code:
<!DOCTYPE html>
<html lang = "en">

  <head>
     <meta charset = "utf-8">
     <title>Students Example</title>
  </head>
    
  <body>
     <a href = "<?php echo base_url();?>index.php/stud/add_view">Add</a>
        
     <table border = "1">
        <?php
           $i = 1;
           echo "<tr>";
           echo "<td>Sr#</td>";
           echo "<td>Roll No.</td>";
           echo "<td>Name</td>";
           echo "<td>Edit</td>";
           echo "<td>Delete</td>";
           echo "<tr>";
                
           foreach($records as $r) {
              echo "<tr>";
              echo "<td>".$i++."</td>";
              echo "<td>".$r->roll_no."</td>";
              echo "<td>".$r->name."</td>";
              echo "<td><a href = '".base_url()."index.php/stud/edit/"
                 .$r->roll_no."'>Edit</a></td>";
              echo "<td><a href = '".base_url()."index.php/stud/delete/"
                 .$r->roll_no."'>Delete</a></td>";
              echo "<tr>";
           }
        ?>
     </table>
        
  </body>
    
</html>
Reply

#2
Well, there are a few issues, but I can't be sure I've found any of the issues you're specifically looking for without a description of what's wrong (e.g. what you expect vs. what actually happens).

To start, though, remove the form elements from your edit view. You should not open a form then use the form_open() method inside the form, and you should use either form_close() or the </form> tag at the end of the form, not both. Also, if you want to display the result of $this->db->last_query() on your view, you should pass it from the controller instead of calling it directly in the view (it might work in the view, but there are a number of reasons it might not, so it should not be done).

Next, when using $this->db->set() and $this->db->update(), there's no reason to call $this->db->set($data) if you're passing $data to $this->db->update()'s second parameter. So your update method could look like this:

PHP Code:
public function update($data$old_roll_no)
{
    
$this->db->where("roll_no"$old_roll_no);
    
$this->db->update("stud"$data);


or you could chain the db calls:

PHP Code:
$this->db->where("roll_no"$old_roll_no)->update("stud"$data); 

If you really prefer the set() syntax, just omit $data from the update() call:

PHP Code:
$this->db->set($data)->where("roll_no"$old_roll_no)->update("stud"); 

Hopefully, the tutorial will eventually get around to adding form validation and other considerations, because you still wouldn't want to start using this code in production.
Reply

#3
Wink 
(07-27-2016, 11:57 AM)thanks for reply. Wrote:
PHP Code:
     public function update($data,$old_roll_no) { 
 
        //$this->db->set($data); 
 
        //$this->db->where("roll_no", $old_roll_no); 
 
        //$this->db->update("stud", $data); 
         
         
$this->db->set($data)->where("roll_no"$old_roll_no)->update("stud"); 
 
     

changed the above, but no success.

please have a look on this tutorial may be something is missing.
http://www.tutorialspoint.com/codeignite...tabase.htm


mwhitneyWell, there are a few issues, but I can't be sure I've found any of the issues you're specifically looking for without a description of what's wrong (e.g. what you expect vs. what actually happens).

To start, though, remove the form elements from your edit view. You should not open a form then use the form_open() method inside the form, and you should use either form_close() or the </form> tag at the end of the form, not both. Also, if you want to display the result of $this->db->last_query() on your view, you should pass it from the controller instead of calling it directly in the view (it might work in the view, but there are a number of reasons it might not, so it should not be done).

Next, when using $this->db->set() and $this->db->update(), there's no reason to call $this->db->set($data) if you're passing $data to $this->db->update()'s second parameter. So your update method could look like this:

PHP Code:
public function update($data$old_roll_no)
{
 
   $this->db->where("roll_no"$old_roll_no);
 
   $this->db->update("stud"$data);


or you could chain the db calls:

PHP Code:
$this->db->where("roll_no"$old_roll_no)->update("stud"$data); 

If you really prefer the set() syntax, just omit $data from the update() call:

PHP Code:
$this->db->set($data)->where("roll_no"$old_roll_no)->update("stud"); 

Hopefully, the tutorial will eventually get around to adding form validation and other considerations, because you still wouldn't want to start using this code in production.
Reply

#4
I am unable to update the record in database table and also, the page is not changing after edit.
Reply

#5
Try this:
PHP Code:
$this->db->update('stud',$data,'roll_no=' $old_roll_no); 
stud is the name of the table.
$data is the array with field names and new values.
The 3rd parameter of the update method is the where clause.
Reply

#6
(07-27-2016, 12:20 PM)nadeem14375 Wrote: I am unable to update the record in database table and also, the page is not changing after edit.

Did you change the view as I mentioned, or just the model? I don't think the issue with the model was likely to cause it to fail to update the data in the database, but the error in the view would potentially cause the form not to submit to the controller, which would make it impossible for you to receive the data in the first place.

Quote:please have a look on this tutorial may be something is missing.

I looked at the tutorial before writing my first response for the purposes of determining whether your code was bad because of the tutorial, or you just didn't copy it properly. In this case, it was the tutorial that was at fault, but I can't go through the whole thing and rewrite it for you.
Reply

#7
Replace $roll_no = $this->uri->segment('3'); to
$roll_no = $this->uri->segment(3);

3 without single quotes
          Heart  love codeigniter Heart
          Learning  best  practices
     Rate my post if you found it helpfull
Reply

#8
(07-30-2016, 10:36 AM)hi Jaynarayan,I think I confused every thing, can you guide me to some tutorial or can you share some simple code for update /edit records.jaynarayan Wrote: Replace   $roll_no = $this->uri->segment('3');  to
$roll_no = $this->uri->segment(3);

3 without single quotes
Reply

#9
There should be no quotes its an integer
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#10
(07-30-2016, 11:37 AM) pid=\335273' Wrote:hi InsiteFX,

mwhitney also mentioned that there is some problem with <form> please check the following and suggest.


PHP Code:
<!DOCTYPE html
<
html lang "en">
 
 
  <head
 
     <meta charset "utf-8"
 
     <title>Students Example edit</title
 
  </head
    
 
  <body
 
     <form method "" action "">
        
 
        <?php 
            echo form_open
('Stud_controller/update_student'); 
 
           echo form_hidden('old_roll_no',$old_roll_no); 
 
           echo form_label('Roll No.'); 
 
           echo form_input(array('id'=>'roll_no','name'=>'roll_no','value'=>$records[0]->roll_no));
 
           echo "<br/>"
                
 
           echo form_label('Name'); 
 
           echo form_input(array('id'=>'name','name'=>'name','value'=>$records[0]->name));
 
           echo "<br/>"
                
 
           echo form_submit(array('id'=>'submit','value'=>'Edit')); 
 
           echo form_close();
            echo 
$this->db->last_query();
 
        ?> 
            
      </form> 
   </body>
    
</html> 

There should be no quotes its an integer
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.