-
nadeem14375 Junior Member
 
-
Posts: 37
Threads: 12
Joined: Jul 2016
Reputation:
0
07-27-2016, 11:16 AM
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>
-
mwhitney Posting Freak
    
-
Posts: 1,101
Threads: 4
Joined: Nov 2014
Reputation:
95
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.
-
nadeem14375 Junior Member
 
-
Posts: 37
Threads: 12
Joined: Jul 2016
Reputation:
0
07-27-2016, 12:18 PM
(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.
-
Wouter60 Posting Freak
    
-
Posts: 851
Threads: 38
Joined: Feb 2015
Reputation:
77
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.
-
nadeem14375 Junior Member
 
-
Posts: 37
Threads: 12
Joined: Jul 2016
Reputation:
0
(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
-
nadeem14375 Junior Member
 
-
Posts: 37
Threads: 12
Joined: Jul 2016
Reputation:
0
(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
|