Welcome Guest, Not a member yet? Register   Sign In
Mysql not updating
#1

(This post was last modified: 02-11-2015, 11:47 PM by elaka.)

hello guys,

I need you help please

I have this in controllers:

================

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

class Website extends Admin_Controller {
    public $_table_names = 'websites';
    public $_subView = 'admin/websites/';
    public $_redirect = 'admin/website';
    public function __construct(){
        parent::__construct();
        $this->data['active'] = 'website';
    }
    

    //  Landing page of admin section.
    function index(){
        $this->data['title'] = 'Websites | '.$this->data['site_name']->value;
        $this->data['name'] = 'Websites';
        $this->data['login'] = $this->session->all_userdata();
        $this->db->order_by('id','desc');    
        $count= count($this->comman_model->get($this->_table_names,false));
        $perpage = 50;
        if ($count > $perpage) {
            //echo 'yes';die;
            $this->load->library('pagination');
            $config['base_url'] = site_url($this->_redirect.'/');
            $config['total_rows'] = $count;
            $config['per_page'] = $perpage;
            $config['uri_segment'] = 3;
            //stylish pagination
            $config['full_tag_open'] = "<ul class='pagination pull-right'>";
            $config['full_tag_close'] ="</ul>";
            $config['num_tag_open'] = '<li>';
            $config['num_tag_close'] = '</li>';
            $config['cur_tag_open'] = "<li><span>";
            $config['cur_tag_close'] = "</span></li>";
            $config['next_tag_open'] = "<li>";
            $config['next_tag_close'] = "</li>";
            $config['prev_tag_open'] = "<li>";
            $config['prev_tag_close'] = "</li>";
            $config['first_tag_open'] = "<li>";
            $config['first_tag_close'] = "</li>";
            $config['last_tag_open'] = "<li>";
            $config['last_tag_close'] = "</li>";

            $this->pagination->initialize($config);
            $this->data['pagination'] = $this->pagination->create_links();
            $offset = $this->uri->segment(3);
        }
        else {
            $this->data['pagination'] = '';
            $offset = 0;
        }
        $this->db->limit($perpage, $offset);
        $this->db->order_by('id','desc');    
        $this->data['all_data'] = $this->comman_model->get($this->_table_names,false);
/*        $count = count($this->data['all_data']);
        $r_from = $page <= 1 ? 1 : ($page -1) * $config["per_page"]+1; //RANGE STARTS FROM
        $r_to   = $r_from + $count - 1;    //RANGE ENDS TO
        $this->data['range'] = $count? "Showing ".$r_from." to ".$r_to." of ".$config["total_rows"]:'';*/
        //echo $this->db->last_query();die;
        //die;
        $this->data['subview'] = $this->_subView.'index';    
        $this->load->view('admin/_layout_main',$this->data);

    }

    function add(){    
        $image = NULL;
        $this->data['name'] = 'Create';
        $this->data['title'] = 'Create | '.$this->data['site_name']->value;

        $rules = $this->admin_model->website_rules;
        $this->form_validation->set_rules($rules);
        if ($this->input->post('operation')){
            if ($this->form_validation->run() == FALSE){}
            else{
                //upload photo
                $post_data= $this->comman_model->array_from_post(array('title','affilate_url','name','seo_title','seo_keyword','seo_description','description','additional_desc','merchant_id','is_shopping_gold','is_featured'));
                if(!empty($_FILES['photo']['name'])){
                    //echo $_FILES['photo'.$i]['name'];
                    $result =$this->comman_model->do_upload('photo','./assets/uploads/websites');
                    if($result[0]=='error'){
                        $this->session->set_flashdata('error',$result[1]);
                        redirect($this->_redirect);
                    }
                    else if($result[0]=='success'){
                        $image = $result[1];
                    }
                }            

                $post_data['image']= $image;
                $post_data['slug'] = url_title($this->input->post('title'), 'dash', true);
                $post_data['slug'] = str_replace('&','-',str_replace(' ','-',trim($this->input->post('title'),' ')));
                $post_data['category_id'] = serialize($this->input->post('category_id'));
                $registerForm = $this->comman_model->save($this->_table_names,$post_data);
                if($this->input->post('category_id')){
                    foreach ($this->input->post('category_id') as $key => $value){
                        $this->comman_model->save('website_category',array('category_id'=>$value,'website_id'=>$registerForm));
                    }
                }
                $this->comman_model->save('sys_routes',array('slug'=>$this->input->post('slug'),'controller'=>'websites/index/'.$this->input->post('slug'),'type'=>'website','website_id'=>$registerForm));
                $this->session->set_flashdata('success', 'Website has successfully created.');
                redirect($this->_redirect);
            }
        }
        $this->data['categories'] = $this->comman_model->get_by('categories',array('status'=>1),false,false,false);        
        $this->data['merchants'] = $this->comman_model->get_by('merchants',array('status'=>1),false,array('email'=>'desc'),false);        
        //$this->data['login'] = $this->session->all_userdata();
        $this->data['subview'] = $this->_subView.'add';
        $this->load->view('admin/_layout_main',$this->data);
    }

    function edit($id= false){
        $this->data['name'] = 'Edit';    
        if(!$id){
            redirect($this->_redirect);
          
        }
        $this->data['title'] ='Edit | '.$this->data['site_name']->value;
        
        $edit_data = $this->comman_model->get_by($this->_table_names,array('id'=>$id),FALSE,FALSE,TRUE);
        if(count($edit_data)==0){
           redirect($this->_redirect);
        }
        $setting_rules = $this->admin_model->website_rules;
        $this->form_validation->set_rules($setting_rules);
        if($this->form_validation->run()==TRUE){
            if (!empty($_FILES['photo']['name'])){                    
                $result =$this->comman_model->do_upload('photo','./assets/uploads/websites');
                if($result[0]=='error'){
                    $this->session->set_flashdata('error',$result[1]);
                    redirect($this->_redirect.'/edit/'.$id);
                }
                else if($result[0]=='success'){
                    $image = $result[1];
                }
            }    
            else{
                $image = $edit_data->image;
            }

            $post_data =$this->comman_model->array_from_post(array('title','affilate_url','name','seo_title','seo_keyword','seo_description','description','additional_desc','is_shopping_gold','is_featured'));
            $post_data['category_id'] = serialize($this->input->post('category_id'));
            $post_data['image']= $image;
            //$post_data['slug'] = url_title($this->input->post('title'), 'dash', true);
            $post_data['slug'] = str_replace('&','-',str_replace(' ','-',trim($this->input->post('title'),' ')));
            $this->comman_model->save($this->_table_names,$post_data,$id);
            $this->comman_model->update('sys_routes',array('slug'=>$this->input->post('slug'),'controller'=>'websites/index/'.$this->input->post('slug')),array('website_id'=>$id));
            if($this->input->post('category_id')){
                $this->comman_model->delete_by_id('website_category',array('website_id'=>$id));
                foreach ($this->input->post('category_id') as $key => $value){
                    $this->comman_model->save('website_category',array('category_id'=>$value,'website_id'=>$id));
                }
            }
            $this->session->set_flashdata('success','Website has been successfully updated.');
            redirect($this->_redirect);
        }
        
        $this->data['edit_data'] =$edit_data;
        $this->data['categories'] = $this->comman_model->get_by('categories',array('status'=>1),false,false,false);        
        $this->data['merchants'] = $this->comman_model->get_by('merchants',array('id'=>$edit_data->merchant_id),false,false,true);        
        //var_dump($this->data['admin_details']);
        $this->data['subview'] = $this->_subView.'edit';        
        $this->load->view('admin/_layout_main',$this->data);      
    }    

    function set_option(){
        $id = $this->input->post('id');
        $action = $this->input->post('action');
        if($action=='shopping'){
            $post_data = array('is_shopping_gold'=>$this->input->post('value'));
            $this->comman_model->save($this->_table_names,$post_data,$id);
        }
        else if($action=='featured'){
            $post_data = array('is_featured'=>$this->input->post('value'));
            $this->comman_model->save($this->_table_names,$post_data,$id);            
        }    
    }

    function get_status(){
        $id = $this->input->post('id');
        $post_data = array('enabled'=>$this->input->post('enable'));
        $result = $this->comman_model->save($this->_table_names,$post_data,$id);
    }

    function delete($id = false){
        if(!$id){
            redirect($this->_redirect);
        }

        //$this->comman_model->update('categories',array('parent_id'=>0),array('parent_id'=>$id));
        //$this->comman_model->delete_by_id('files',array('category_id'=>$id));
        $check_coupon = $this->comman_model->get_by('coupons',array('website_id'=>$id),false,false,false);
        if($check_coupon){
            foreach($check_coupon as $set_coupon){
                $this->comman_model->delete_by_id('coupon_save',array('coupon_id'=>$set_coupon->id));
                $this->comman_model->delete_by_id('coupon_comment',array('coupon_id'=>$set_coupon->id));
                $this->comman_model->delete_by_id('coupons',array('id'=>$set_coupon->id));
            }
        }        
        $this->comman_model->delete_by_id($this->_table_names,array('id'=>$id));

        $this->session->set_flashdata('success','Website has successfully deleted.');
        redirect($this->_redirect);        

    }
}

/* End of file admin.php */
/* Location: ./application/controllers/admin.php */
==========

and this in model

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

class comman_model extends CI_Model {
    protected $_primary_key = 'id';
    protected $_primary_filter = 'intval';
    protected $_timestamps = TRUE;

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

    public function save($table,$data, $id = NULL){    
        // Set timestamps
        if ($this->_timestamps == TRUE) {
            $now = time();
            $id || $data['created'] = $now;
            $data['modified'] = $now;
        }
        
        // Insert
        if ($id === NULL) {
            !isset($data[$this->_primary_key]) || $data[$this->_primary_key] = NULL;
            $this->db->set($data);
            $this->db->insert($table);
            $id = $this->db->insert_id();
        }
        // Update
        else {
            $filter = $this->_primary_filter;
            $id = $filter($id);
            $this->db->set($data);
            $this->db->where($this->_primary_key, $id);
            $this->db->update($table);
        }
        //echo $this->db->last_query();die;
        return $id;
    }

    public function update($table,$data, $id = NULL){    
        // Set timestamps
        $this->db->set($data);
        $this->db->where($id);
        $this->db->update($table);
        //echo $this->db->last_query();die;
       //return $id;
    }

    public function get_query($string){
        $query = $this->db->query($string);        
        //echo $this->db->last_query();die;
        return $query->result();
    }
    
    public function get_by($table,$where, $like = false,$order = false, $single = FALSE){
        
        if($like){
            $this->db->like($like);
        }        
        if($order){
            foreach($order as $set =>$value){                
                $this->db->order_by($set,$value);
            }
        }
        $this->db->where($where);
           //$this->get($table, $single);
           //echo $this->db->last_query();die;
        
        return $this->get($table, $single);
    }

    public function get($table,$single = FALSE){
        $this->_table_name = $table;
          if($single == TRUE) {
            $method = 'row';
        }
        else {
            $method = 'result';
        }
    
/*        if (!count($this->db->ar_orderby)) {
            $this->db->order_by($this->_order_by);
        }*/
        //$this->db->get($this->_table_name)->$method();
       // echo $this->db->last_query();die;
        return $this->db->get($this->_table_name)->$method();
    }

    public function array_from_post($fields){
        $data = array();
        foreach ($fields as $field) {
            $data[$field] = $this->input->post($field);
        }
        return $data;
    }
  
    function do_upload($file_name,$path){
        $result = array();
        $field_name = $file_name;
        $config['upload_path'] = $path.'/full/';
        $config['allowed_types'] = 'gif|jpg|jpeg|png|GIF|JPG|JPEG|PNG';
        $config['max_size']    = '900000';
        $config['max_width']  = '50000';
        $config['max_height']  = '503000';        
        $this->load->library('upload', $config);

        if (!$this->upload->do_upload($field_name)){
            array_push($result,'error',$this->upload->display_errors());
            return $result;
        }
        else{
            $upload_data = $this->upload->data();
            $this->load->library('image_lib');
            $config['image_library'] = 'gd2';
            $config['source_image'] = $path.'/full/'.$upload_data['file_name'];
            $config['new_image']    = $path.'/thumbnails/'.$upload_data['file_name'];
            $config['maintain_ratio'] = TRUE;
            $config['width'] = 450;
            $config['height'] = 450;
            $this->image_lib->initialize($config);
            $this->image_lib->resize();
            $this->image_lib->clear();

            $config['image_library'] = 'gd2';
            $config['source_image'] = $path.'/full/'.$upload_data['file_name'];
            $config['new_image'] = $path.'/small/'.$upload_data['file_name'];
            $config['maintain_ratio'] = TRUE;
            $config['width'] = 100;
            $config['height'] = 100;
            $this->image_lib->initialize($config);
            $this->image_lib->resize();
            $this->image_lib->clear();        
            array_push($result,'success',$upload_data['file_name']);
            return $result;
        }
    }





    function userLogin($array){
        $query = $this->db->get_where('user',$array);
        //echo $this->db->last_query();die;
        return $query->row_array();
    }

    function add($table,$array){
        $query = $this->db->insert($table,$array);
        //echo $this->db->last_query();die;
        //return $query->row_array();
        return $this->db->insert_id();
    }
    
    function record_count($table) {
        return $this->db->count_all($table);
    }
    
    function get_data_by_pagination($table,$limit,$start,$field,$value){
        $this->db->order_by($field, $value);
        $this->db->limit($limit, $start);
        $query = $this->db->get($table);
        /*if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
        return $data;
        }*/
        //echo $this->db->last_query();die;
        return $query->result_array();
    }

    function get_username($data){        
        $query = $this->db->get_where('user', $data);
        //echo $this->db->last_query();die;
        return $query->row_array();
    }

    function get_average_by_id($table,$data,$field_name){        
        $query =$this->db->select_avg($field_name)->get_where($table, $data);
        //echo $this->db->last_query();die;
        return $query->row_array();
    }

    function get_sum_by_id($table,$data,$field_name){        
        $query =$this->db->select_sum($field_name)->get_where($table, $data);
        //echo $this->db->last_query();die;
        return $query->row_array();
    }

    function get_top_ranking($table,$field1,$field2,$new_name,$order){
        $this->db->select($field1);
        $this->db->select_sum($field2, $new_name);
        $this->db->group_by($field1);
        $this->db->order_by($new_name,$order);
        $query = $this->db->get($table, 10);
    //    echo $this->db->last_query();die;
        return $query->result_array();
    }

    function query_result($query){
        $query = $this->db->query($query);
        //echo $this->db->last_query();die;
        return $query->result_array();
    }

    public function delete($table,$where){
        $this->db->delete($table, $where);
    }
        
    function delete_by_id($table,$where)
    {        
        $this->db->delete($table, $where);
    }
        
    function update_by_id($table_Name,$updatequery, $id){
        $this->db->where('id', $id);
        $this->db->update($table_Name, $updatequery);    
    }

    function update_by($table_Name,$updatequery,$condition){
        $this->db->where($condition);
        $this->db->update($table_Name, $updatequery);    
        //echo $this->db->last_query();die;
    }

    function update_data_by_id($table_Name,$updatequery, $field_name,$value){
        $this->db->where($field_name, $value);
        $this->db->update($table_Name, $updatequery);    
        //echo $this->db->last_query();die;
    }                    
}



/* End of file super_admin_model.php */
/* Location: ./system/application/models/super_admin_model.php */
?>
===========

But when I edit or enter some data in /website/  the slug is updated as 0 in the DB and other entry not updated!!
Thanks everyone
Reply
#2

What does your view look like?

In your controller's edit method, you appear to be updating the 'websites' table with a slug which is generated from the title field in the view, but when you update the 'sys_routes' table, you attempt to pull the slug field from $this->input->post().

In the controller's add method, you generate slug from the title twice before attempting to insert it into the 'websites' table, and, again, you attempt to pull the slug field from $this->input->post() when you insert into the sys_routes table.

If the slug was in $this->input->post() all along, I'm not sure why you would generate it from the title when you send it to the 'websites' table.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB