0

I have 2 tables:

table: tbl_project
---------------------------------------
| PID | ProjectName | StartDate |
---------------------------------------

table: tbl_chart
-------------------------------------------------------------------------
| PID | P_ProjectPreparation | P_ConceptualDesign | P_Realization |
-------------------------------------------------------------------------

PID in table tbl_project is the Primary Key. And the ProjectID in tbl_chart references PID in tbl_project.

here is my add project form

I want if I click save for the data to insert into tbl_project also in tbl_chart insert the new PID automatically, and the value at column P_ProjectPreparation etc is 0.

I've tried query at the Model like this :

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

class admin_m extends CI_Model {
    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }

    public function save($data)
    {
        $sql = "insert into tbl_project values('".$data['PID']."','".$data['ProjectName']."', '".$data['StartDate']."') ; insert into tbl_chart (PID) values ('".$data['PID']."')";
        $this->db->query($sql);
     } 
}

Here is my controller :

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

class admin_c extends CI_Controller {

    public function __construct()
    {
        parent::__construct();

        $this->load->database();
        $this->load->helper('url');
        $this->load->model('admin_m');
    }

    public function index()
    {
        $this->load->view('admin_v');
    }

    public function save()
    {
        $data['PID'] = $this->input->post('PID');
        $data['ProjectName'] = $this->input->post('ProjectName');
        $data['StartDate'] = $this->input->post('StartDate');

        $this->admin_m->save($data);
        $this->load->view('admin_v');
     } 
}

Here is my view code for the save button :

<form action="<?PHP echo site_url(); ?>/admin_c/save" method="post">

I get an error from the model. Any ideas what could be wrong with the query?

Jeff Puckett
  • 37,464
  • 17
  • 118
  • 167
Santi
  • 13
  • 4
  • 1
    **WARNING**: Don't forget to [parameterize your queries](http://stackoverflow.com/questions/10968527/escaping-sql-queries-in-codeigniter) because this has gaping [SQL injection holes](http://bobby-tables.com/). – tadman Jul 28 '16 at 19:39
  • 1
    What error do you get? – Daniel Waghorn Jul 28 '16 at 19:51
  • yeah i'm going to pile on and say - you are using codeigniter - so use query builder - its easier, faster, and safer. http://www.codeigniter.com/user_guide/database/query_builder.html – cartalot Jul 28 '16 at 21:00
  • Add `return $this->db->affected_rows()` to your model. You are returning nothing from model for now. Also it would be helpful if you shared and pasted error you have read yourself. I could suggest you studying MySQL triggers since those kind of jobs you are trying to achieve should be MySQL job IMHO. – Tpojka Jul 28 '16 at 22:00

1 Answers1

0

you can try making somthing like

application/controllers/Admin_c .php

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

class admin_c extends CI_Controller {

    public function __construct(){
        parent::__construct();

        $this->load->database();
        $this->load->helper('url');
        $this->load->library('form_validation');
        $this->load->model('admin_m');
    }

    public function index(){
        $this->load->view('admin_v');
    }

    public function save(){
        $this->form_validation->set_rules('PID', 'PID', 'trim|required|xss_clean');
        $this->form_validation->set_rules('ProjectName', 'Project Name', 'trim|required|xss_clean');
        $this->form_validation->set_rules('StartDate', 'Date', 'trim|required|xss_clean');

        if( $this->form_validation->run() === FALSE ){
            print_r(validation_errors());
        }
        else{
            $pid   = $this->input->post('PID', TRUE);
            $pname = $this->input->post('ProjectName', TRUE);
            $date_s= $this->input->post('StartDate', TRUE);

            $this->admin_m->save($pid, $pname, $date_s);
            $this->load->view('admin_v');
        }
     } 
}

application/models/admin_m.php

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

class admin_m extends CI_Model {
    function __construct(){
        parent::__construct();
    }

    public function save( $pid, $project_name, $start_date ){
        $this->db->insert('tbl_project', array('ProjectName' => $project_name, 'StartDate' => $start_date));

        $last_id = $this->db->insert_id();

        $this->db->insert('tbl_chart', array('PID' => $last_id, 'P_ProjectPreparation' => NULL, 'P_ConceptualDesign' => NULL, 'P_Realization' => NULL));
     } 
}
elddenmedio
  • 1,030
  • 1
  • 8
  • 15
  • I get some error like this : Unable to access an error message corresponding to your field name PID.(xss_clean) Unable to access an error message corresponding to your field name ProjectName.(xss_clean) Unable to access an error message corresponding to your field name StartDate.(xss_clean) – Santi Jul 29 '16 at 07:50
  • remove the `xss_clean`, it was occupied in ci2, the custom – elddenmedio Jul 29 '16 at 13:59