Welcome Guest, Not a member yet? Register   Sign In
Help with Update in CRUD.
#1

[eluser]HSKrustofsky[/eluser]
Well, I've pretty much got everything working in my CRUD. The only thing is with the update function. I know in editnews.php I need to have a value within the form, but everything I try to input never seems to bring up anything. Any help would be awesome.

Here is the code:

admin_model.php
Code:
<?php

class Admin_model extends Model {
    
    function Admin() {
        parent::Model();
    }
    
    function create($data) {

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

    }

    function retrieve() {

        $query = $this->db->get('data');
        return $query->result();

    }

    function update($data) {

        $this->db->where('id', $this->uri->segment(3));
        $this->db->update('data', $data);

    }

    function delete() {

        $this->db->where('id', $this->uri->segment(3));
        $this->db->delete('data');

    }
    
}

admin.php
Code:
<?php

class Admin extends Controller {
    
    function Admin() {
        parent::Controller();
    }
    
    function index() {
    
        $data = array();
    
        if($query = $this->admin_model->retrieve()) {
            $data['records'] = $query;
        }

        $data['title'] = "Admin";
        $this->load->view('panelview', $data);

    }

    function create() {

        $data = array(
            'title' => $this->input->post('title'),
            'contents' => $this->input->post('contents')
        );
        
        $this->load->view('addnews');
        
        if($_POST) {
            $this->admin_model->create($data);
        }
        
        if($_POST != NULL) {
            redirect('../admin/');
        }

    }

    function delete() {
        
        $this->admin_model->delete();
        $this->index();

    }

    function update() {

        $data = array(
            'title' => $this->input->post('title'),
            'contents' => $this->input->post('contents')
        );$this->load->view('editnews', $data);
        
        if($_POST) {
            $this->admin_model->update($data);
        }
        
        if($_POST != NULL) {
            redirect('../admin/');
        }

    }

}

editnews.php
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

&lt;html&gt;
    &lt;head&gt;
        &lt;title&gt;&lt;?php echo $title; ?&gt;&lt;/title&gt;
        &lt;meta http-equiv="Content-Type" content="text/html; charset-UTF-8"&gt;
        
    &lt;/head&gt;    
    &lt;body&gt;
        <h1 align="center">Edit News</h1>
        <table cellpadding="5px" cellspacing="0" border="1" bordercolor="#000" align="center">
            <tr>
                <td>
                    &lt;?= form_open('../admin/update/') ?&gt;
                        <ul>
                            <li>
                                <label>Title:</label>
                            </li>
                            <li>
                                &lt;?= form_input('title') value ?&gt;
                            </li>
                            <li>
                                <label>Content:</label>
                            </li>
                            <li>
                                &lt;?= form_textarea('contents') ?&gt;
                            </li>
                            <li>
                                &lt;?= form_submit('', 'Submit') ?&gt;
                            </li>
                        </ul>
                    &lt;?= form_close() ?&gt;
                </td>
            </tr>
        </table>
    &lt;/body&gt;
&lt;/html&gt;
#2

[eluser]xeroblast[/eluser]
try this:

Code:
function update($data,$id) {

        $this->db->where('id', $id);
        $this->db->update('data', $data);

    }

Code:
if($_POST) {
            $this->admin_model->update($data,$this->uri->segment(3));
        }
#3

[eluser]HSKrustofsky[/eluser]
Thanks for your help, but I did you what you said, and nothing. What could I have done wrong?
#4

[eluser]xeroblast[/eluser]
try to change this: &lt;?= form_open('../admin/update/') ?&gt;

to: &lt;?= form_open('admin/update') ?&gt;
#5

[eluser]HSKrustofsky[/eluser]
When I first did the form I did that, and it didn't work. I had to add the ../ in order for the page to read the cotnroller.

I was looking online and I think I have to set a value to the form, but can't seem to get anything to show up when I do. I also can't seem to get any information to change when I input new information in the fields.

Do you think this may be the issue?
#6

[eluser]wakey[/eluser]
&lt;?= form_input('title') value ?&gt;

Should the value be there?
#7

[eluser]HSKrustofsky[/eluser]
I had added a value, but when nothing work I started deleting them. I guess I didn't delete all of them. I'm pretty sure a value needs to be there. That's one of things I'm stuck on. All I need it to do is bring up that id's info so I can update whatever information comes up.

Thanks in advance for everyone's help.
#8

[eluser]wakey[/eluser]
If you need to retrieve data to be updated you will need to create a function to retrieve the data from the database and then echo this into the form in the editnews view - is this what you mean?
#9

[eluser]HSKrustofsky[/eluser]
Yes. That's exactly what I need to do.
#10

[eluser]xeroblast[/eluser]
i think this is the line where you got it wrong:

your controller:
Code:
function update() {

        $data = array(
            'title' => $this->input->post('title'),
            'contents' => $this->input->post('contents')
        );$this->load->view('editnews', $data);
        
        if($_POST) {
            $this->admin_model->update($data);
        }
        
        if($_POST != NULL) {
            redirect('../admin/');
        }

    }

your view:
Code:
<li>
     &lt;?= form_input('title') value ?&gt;
</li>
<li>
     <label>Content:</label>
</li>
<li>
    &lt;?= form_textarea('contents') ?&gt;
</li>
<li>
    &lt;?= form_submit('', 'Submit') ?&gt;
</li>

you should do it like this:

controller:
Code:
function update($id) {
  // this is where if the submit button is push
  if ($_POST) {
    $save = array(
      'title'  => $this->input->post('title'),
      'contents'  => $this->input->post('contents'),
    );
    $this->admin_model->update($save,$id); // use the model i created in my previous post
  }
  //this is where you load the data to view
  $data['blah'] = $this->admin_model->get($id);
  $this->load->view('editnews',$data);
}

your view:
Code:
&lt;?= form_open($this->uri->uri_string()) ?&gt;
<li>
     &lt;?= form_input(array('name' => 'title', 'value' => $blah->title)) ?&gt;
</li>
<li>
     <label>Content:</label>
</li>
<li>
    &lt;?= form_textarea(array('name' => 'contents', 'value' => $blah->contents)) ?&gt;
</li>
<li>
    &lt;?= form_submit('', 'Submit') ?&gt;
</li>
&lt;?= form_close() ?&gt;

your model should have this: to get a single row data...
Code:
function get($id) {
  return $this->db->get_where('table',array('id' => $id))->row();
}




Theme © iAndrew 2016 - Forum software by © MyBB