Pregunta

I am trying to update records in DB, but for some reason it is not updating. Maybe the way I pass the ID problematic?

the model:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class site_model extends CI_Model {




    function __construct()
    {
        // Call the Model constructor
        parent::__construct();

    }


     function get_records()
    {
        $query = $this->db->get('posts');

        return $query->result();

    }


    function get_records_by_id()

    {
        $this->db->where('postID', $this->uri->segment(3));
        $query = $this->db->get('posts');

        return $query->result();

    }


    function add_records($data) {

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

        return;


    }

    function updata_records($data) {

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



    }

    function delede_roe() {

        $this->db->where('postID', $this->uri->segment(3));
        $this->db->delete('posts');



    }

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */

controoler: 

    <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class site extends CI_Controller {

    /**
     * Index Page for this controller.
     *
     * Maps to the following URL
     *      http://example.com/index.php/welcome
     *  - or -  
     *      http://example.com/index.php/welcome/index
     *  - or -
     * Since this controller is set as the default controller in 
     * config/routes.php, it's displayed at http://example.com/
     *
     * So any other public methods not prefixed with an underscore will
     * map to /index.php/welcome/<method_name>
     * @see http://codeigniter.com/user_guide/general/urls.html
     */
    public function index()
    {
        $this->load->view('home');


    }

    function add_post() {

        $this->load->view('add_post');


    }

    function creata() {


        $data = array (
                'title' => $this->input->post('title'),
                'post' => $this->input->post('content')

        );

        $this->load->model('site_model');
        $this->site_model->add_records($data);

        redirect('site/all_posts/');

    }


    function edit_post () {

        if ($query = $this->site_model->get_records_by_id()) {


            $data['record'] = $query;

        }

        $this->load->view('edit_post', $data);

        }


        function updata() {



            $data = array (
                'title' => $this->input->post('title'),
                'post' => $this->input->post('content')

        );

            $this->load->model('site_model');
        $this->site_model->updata_records($data);

        redirect('site/all_posts/');

        }



    function all_posts() {


    if ($query = $this->site_model->get_records()) {


            $data['records'] = $query;

        }

        $this->load->view('all_posts', $data);

    }


    function delete() {


    $this->site_model->delede_roe();
     redirect('site/all_posts/');

}

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */

form: 


            <?php  if (isset($record)) {



                foreach ($record as $row) { 

                 echo form_open('index.php/site/updata'); ?>

                <label for="title"> title </label>

                <input type="text" name="title" id="title" value="<?php echo $row->title; ?>">


                    <label for="content"> content </label>

                    <input type="text" name="content" value="<?php echo $row->post; ?>" id="content" style="width: 630px;">



                    <input type="submit" value="׳©׳�׳—">

                    <?php  echo form_close(); }} ?>
¿Fue útil?

Solución

updata_records($data) , I think the problem is there, model function should have uri data with $data array.

$data = array (
'postID' => $this->uri->segment(3),
'post_content' => $post_content 
);

$this->site_model->updata_records($data);
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top