PHP / Codeigniter - 如何使用相同的sql将数据插入多表?

时间:2014-03-05 20:20:51

标签: php mysql sql codeigniter

我想在PHP-codeigniter框架的同一个sql中将我的表单中的数据插入到多表中。我找不到一些问题。请帮我解决一下。

谢谢。

控制器在这里:customer.php

...
    public function editprofile(){

     $this->load->library('form_validation');
      $this->form_validation->set_rules('input_email', 'Email Adresi', 'trim|required|min_lenght[6]|max_lenght[25]|valid_email|xss_clean');
      $this->form_validation->set_rules('input_birthday', 'Doğum Tarihi', 'trim|required|min_lenght[2]|max_lenght[14]|xss_clean');
      $this->form_validation->set_rules('cinsiyet[]', 'Cinsiyet', 'trim|required|min_lenght[6]|max_lenght[20]|matches[input_password2]|xss_clean');
      $this->form_validation->set_rules('select_city', 'Şehir', 'trim|required|min_lenght[6]|max_lenght[20]|xss_clean');
      $this->form_validation->set_rules('input_phone', 'Telefon Numarası', 'trim|required|min_lenght[2]|max_lenght[14]|xss_clean'); 
      $this->form_validation->set_rules('input_comSname', 'Firma Kısa Adı', 'trim|required|min_lenght[2]|max_lenght[14]|xss_clean'); 
      $this->form_validation->set_rules('input_comname', 'Firma Tam Adı', 'trim|required|min_lenght[2]|max_lenght[14]|xss_clean'); 
      $this->form_validation->set_rules('select_comcity', 'Firma Yeri', 'trim|required|min_lenght[2]|max_lenght[14]|xss_clean'); 
      $this->form_validation->set_rules('input_comemail', 'Firma Email Adresi', 'trim|required|min_lenght[6]|max_lenght[25]|valid_email|xss_clean');
      $this->form_validation->set_rules('comEmployernum', 'Çalışan Sayısı', 'trim|required|min_lenght[2]|max_lenght[14]|xss_clean');
      $this->form_validation->set_rules('input_comsector', 'Firma Sektörü', 'trim|required|min_lenght[2]|max_lenght[14]|xss_clean');
      $this->form_validation->set_rules('input_comnumber', 'Telefon Numarası', 'trim|required|min_lenght[2]|max_lenght[14]|xss_clean');
      $this->form_validation->set_rules('input_comwebsite', 'Web Sitesi', 'trim|required|min_lenght[2]|max_lenght[14]|xss_clean');
      $this->form_validation->set_rules('input_comservice', 'Hizmetler', 'trim|required|min_lenght[2]|max_lenght[14]|xss_clean');
      if($this->form_validation->run() == FALSE) {
          if($this->session->userdata('logged_in')){
             error_reporting(0);
             $session_data = $this->session->userdata('logged_in');
             $data1['email'] = $session_data['email'];

                 $this->load->view('navbarview', $data1);
             $this->load->view('addProfileInf',$data1);
         }else{
             redirect('home', 'refresh');
         }
      }
      else{
    //$id = $_GET['ID'];
         $this->load->model('CustomerModel');


     $result = $this->CustomerModel->addProfileInf();
     if($result) {

         if($this->session->userdata('logged_in')){
             error_reporting(0);
             $session_data = $this->session->userdata('logged_in');
             $data1['email'] = $session_data['email'];
             $this->load->view('Loginview',array('email' => $result));
            echo "<div class='alert alert-success'>
      <a href='MusteriEkle.php' type='button' class='close' data-dismiss='alert'>×</a>
      <strong>Well done!</strong>Your information was successfully saved ".$result."!</div>";
                 $this->load->view('navbarview', $data1);
             $this->load->view('LoginView');
         }else{
             redirect('home', 'refresh');
         }  }     } }...

这是Model:CustomerModel.php

...
public function addProfileInf () {
    $this->load->database();
$data = array (  
     $email = $this->input->post('input_email'),
      $birthday = $this->input->post('input_birthday'),
      $cinsiyet =  $this->input->post('cinsiyet[]'),
      $city = $this->input->post('select_city'),
      $phone = $this->input->post('input_phone'),
      $comSname = $this->input->post('input_comSname'),
      $comname = $this->input->post('input_comname'),
      $comcity = $this->input->post('select_comcity'),
      $comemail = $this->input->post('input_comemail'),
      $comnum = $this->input->post('comEmployernum'),
      $sector = $this->input->post('input_comsector'),
      $comnumber = $this->input->post('input_comnumber'),
      $website = $this->input->post('input_comwebsite'),
      $service = $this->input->post('input_comservice'));
$sql = "INSERT INTO data (city, birthdate) VALUES
          ('".$city."',
           '".$birthday."') INTO genders (cus_gender) VALUES ('".$cinsiyet."') INTO contacts (phone) VALUES ('".$phone."') INTO companies (comSname, comname) VALUES ('".$comSname."', '".$comname."') INTO com_contacts (comcity, comemail, comnumber, comwebsite) VALUES ('".$comcity."', '".$comemail."', '".$comnumber."', '".$website."') INTO com_data (comsector, comservice, comEmployernum) VALUES('".$sector."', '".$service."', '".$comnum."')";
 $result = $this->db->query($sql);
 if($this->db->affected_rows()=== 1) {
          return $email;
      }
      else {
          echo "Problem inserting user into database";
          return false;
      }
}...

这几乎是视图:addProfileInf.php

     ...
     <?php echo validation_errors();?>
                       <?php form_open('customers/editprofile');?>
.
.
.
<?php form_close();?>
...

此代码中的问题是什么?有任何错误消息。我想不能发布任何数据。因为当我尝试在addProfileInf.php中单击提交时,没有任何反应。

1 个答案:

答案 0 :(得分:0)

所以你想用一个sql将值插入多个表中?据我所知,你不能这样做......但是你可以使用事务。

我希望这会对您有所帮助:MySQL Insert into multiple tables? (Database normalization?)