【问题标题】:Codeigniter How to save data from a form to a databaseCodeigniter 如何将数据从表单保存到数据库
【发布时间】:2013-03-21 05:11:06
【问题描述】:

代码点火器

嗨。我想将表单数据发送到数据库。数据检查正常,接下来怎么办?

我的控件

 <?php

class Form extends CI_Controller {

    function index()
    {
        $this->load->helper(array('form', 'url'));

        $this->load->library('form_validation');

            $this->form_validation->set_rules('name', 'Imie', 'required|min_length[5]|max_length[25]|required|alpha'); 
            $this->form_validation->set_rules('surname', 'Nazwisko', 'required|min_length[5]|max_length[25]|required|alpha'); 
            $this->form_validation->set_rules('email', 'Email', 'required|valid_email'); 
            $this->form_validation->set_rules('number', 'Numer telefonu', 'required|alpha_numeric|max_length[10]'); 
            $this->form_validation->set_rules('brand', 'Marka', 'required|alpha'); 
            $this->form_validation->set_rules('model', 'Model', 'required|alpha'); 
            $this->form_validation->set_rules('year', 'Rok produkcji', 'required|alpha_numeric|max_length[5]'); 
            $this->form_validation->set_rules('km', 'Ilosc KM', 'required|alpha_numeric|max_length[5]'); 
            $this->form_validation->set_rules('licenceseplate', 'Tablica rejestracyjna', 'required|max_length[15]'); 
            $this->form_validation->set_rules('description', 'Opis', 'required|max_length[300]'); 
            $this->form_validation->set_rules('city', 'Miasto', 'required|max_length[30]'); 

        if ($this->form_validation->run() == FALSE)
        {
            $this->load->view('myform');

        }
        else
        {
            $this->load->view('formsuccess');
        }

    }


}
?>

我的看法

    <html>
<head>
<title>My Form</title>
</head>
<body>


<?php echo form_open('form'); ?>

<h5>Wpisz swoje imię</h5>
<?php echo form_error('name'); ?>
<input type="text" name="name" value="<?php echo set_value('name'); ?>" size="50" />

<h5>Nazwisko</h5>
<?php echo form_error('surname'); ?>
<input type="text" name="surname" value="<?php echo set_value('surname'); ?>" size="50" />

<h5>Email Address</h5>
<?php echo form_error('email'); ?>
<input type="text" name="email" value="<?php echo set_value('email'); ?>" size="50" />

<h5>Numer telefonu</h5>
<?php echo form_error('number'); ?>
<input type="text" name="number" value="<?php echo set_value('number'); ?>" size="50" />

<h5>Marka</h5>
<?php echo form_error('brand'); ?>
<input type="text" name="brand" value="<?php echo set_value('brand'); ?>" size="50" />

<h5>Model</h5>
<?php echo form_error('model'); ?>
<input type="text" name="model" value="<?php echo set_value('model'); ?>" size="50" />

<h5>Rok produkcji</h5>
<?php echo form_error('year'); ?>
<input type="text" name="year" value="<?php echo set_value('year'); ?>" size="50" />


<h5>km</h5>
<?php echo form_error('km'); ?>
<input type="text" name="km" value="<?php echo set_value('km'); ?>" size="50" />



<h5>Rejestracja </h5>
<?php echo form_error('licenceseplate'); ?>
<input type="text" name="licenceseplate" value="<?php echo set_value('licenceseplate'); ?>" size="50" />

<h5>Opis </h5>
<?php echo form_error('description'); ?>
<input type="text" name="description" value="<?php echo set_value('description'); ?>" size="50" />

<h5>Miasto </h5>
<?php echo form_error('city'); ?>
<input type="text" name="city" value="<?php echo set_value('city'); ?>" size="50" />



<div><input type="submit" value="Submit" /></div>

</form>

</body>
</html>

我不期待最终的解决方案,但我需要帮助,下一步是什么

我的数据库:

【问题讨论】:

    标签: forms codeigniter send


    【解决方案1】:

    好的 我的观点是这样的:

        <?php
    
    class Form extends CI_Controller {
    
        function index()
        {
            $this->load->helper(array('form', 'url'));
    
            $this->load->library('form_validation');
    
                $this->form_validation->set_rules('name', 'Imie', 'required|min_length[5]|max_length[25]|required|alpha'); 
                $this->form_validation->set_rules('surname', 'Nazwisko', 'required|min_length[5]|max_length[25]|required|alpha'); 
                $this->form_validation->set_rules('email', 'Email', 'required|valid_email'); 
                $this->form_validation->set_rules('number', 'Numer telefonu', 'required|alpha_numeric|max_length[10]'); 
                $this->form_validation->set_rules('brand', 'Marka', 'required|alpha'); 
                $this->form_validation->set_rules('model', 'Model', 'required|alpha'); 
                $this->form_validation->set_rules('year', 'Rok produkcji', 'required|alpha_numeric|max_length[5]'); 
                $this->form_validation->set_rules('km', 'Ilosc KM', 'required|alpha_numeric|max_length[5]'); 
                $this->form_validation->set_rules('licenceseplate', 'Tablica rejestracyjna', 'required|max_length[15]'); 
                $this->form_validation->set_rules('description', 'Opis', 'required|max_length[300]'); 
                $this->form_validation->set_rules('city', 'Miasto', 'required|max_length[30]'); 
    
            if ($this->form_validation->run() == FALSE)
            {
                $this->load->view('myform');
    
            }
            else
            {
                /*$this->load->view('formsuccess');*/
                $this->load->model ( 'form' );
                $this->form->save($data);
            }
    
        }
    
    
    }
    ?>
    

    我的模特

        class form extends Model{
    
      function form(){
        parent::Model();
        $this->load->helper('url');             
      }
    
    public function save($data) {
       $this->db->set($data);
       $this->db->insert(form);
       $this->db->insert_id();
    
     }
    
    
    
    }
    

    日期基名称形式的表格

    我不知道你想要什么

    【讨论】:

      【解决方案2】:

      表单验证后可以保存数据

      在你的控制器中

          if ($this->form_validation->run() == FALSE){
              $this->load->view('myform');
          }
          else {
           $this->your_model->save($data);           
          }
      

      your_model.php

       public function save($data) {
         $this->db->set($data);
         $this->db->insert(table name here);
         $this->db->insert_id();
       }
      

      【讨论】:

      • 您可以在 CI 的出色用户指南中找到与数据库类相关的所有信息。这是非常容易使用。请原谅我的英语:)
      【解决方案3】:

      您必须将数据发送到模型

      您可以使用活动记录或普通查询来保存数据

      必须注意,你需要在控制器中包含模型

      $this-&gt;load-&gt;model ( 'Your model name' ); 这应该包含在控制器中

      $this-&gt;yourmodelname-&gt;functioninmodel这是对模型的控制器函数调用

      【讨论】:

      • 请举例说明它的外观。 Kombinował 已经有模型,但我无法获得效果
      猜你喜欢
      • 2018-06-01
      • 1970-01-01
      • 2019-08-21
      • 1970-01-01
      • 2017-03-06
      • 1970-01-01
      • 2016-03-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多