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?

4
  • 1
    WARNING: Don't forget to parameterize your queries because this has gaping SQL injection holes. Commented Jul 28, 2016 at 19:39
  • 1
    What error do you get? Commented Jul 28, 2016 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. codeigniter.com/user_guide/database/query_builder.html Commented Jul 28, 2016 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. Commented Jul 28, 2016 at 22:00

1 Answer 1

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));
     } 
}
Sign up to request clarification or add additional context in comments.

2 Comments

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)
remove the xss_clean, it was occupied in ci2, the custom

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.