2016-07-28 21 views
0

Ich habe 2 Tabellen:Einfügen von Daten in zwei Tabellen mit PHP und codeigniter

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

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

PID in Tabelle tbl_project ist der Primärschlüssel. Und die ProjectID in tbl_chart verweist PID in tbl_project.

here is my add project form

Ich möchte, wenn ich die Daten in tbl_project auch die neue PID automatisch einfügen in tbl_chart einfügen speichern für klicken, und der Wert in Spalte P_ProjectPreparation etc ist 0.

ich Abfrage versucht habe am Modell wie folgt aus:

<?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); 
    } 
} 

Hier ist mein 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'); 
    } 
} 

Hier ist meine Ansicht Code für die Schaltfläche speichern:

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

ich einen Fehler aus dem Modell zu bekommen. Irgendwelche Ideen, was könnte mit der Abfrage falsch sein?

+1

** WARNUNG **: Nicht vergiss [deine Anfragen zu parametrisieren] (http://stackoverflow.com/questions/10968527/escaping-sql-queries-in-codeigeiter), weil das gaping [SQL injection holes] hat (http://bobby-tables.com/). – tadman

+1

Welchen Fehler bekommen Sie? –

+0

yeah ich werde aufstocken und sagen - Sie verwenden codeigniter - verwenden Sie Abfrage Builder - es ist einfacher, schneller und sicherer. http://www.codeigniter.com/user_guide/database/query_builder.html – cartalot

Antwort

0

Sie versuchen können somthing machen wie

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)); 
    } 
} 
+0

ich einige Fehler wie folgt aus:.. Es konnte keine Fehlermeldung entsprechend Ihren Feldnamen PID zuzugreifen (xss_clean) kann nicht eine Fehlermeldung zu Ihrem Feldnamen Project entsprechend Zugriff (xss_clean) kann nicht eine Fehlermeldung zuzugreifen entsprechend Ihrem Feldnamen StartDate. (xss_clean) – Santi

+0

entfernen Sie die 'Xss_clean', es war besetzt in ci2, die benutzerdefinierte – elddenmedio

+0

Problem gelöst! Danke dir – Santi

Verwandte Themen