Codeigniter如何将数据从表单保存到数据库

时间:2013-03-30 17:14:04

标签: forms codeigniter send

您好。我想将表单数据发送到数据库。检查数据是否正常,但下一步该做什么?

我的对手

 <?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>

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

我的日期记录:

3 个答案:

答案 0 :(得分: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();
 }

答案 1 :(得分: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 :(得分:0)

您必须将数据发送到模型

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

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

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

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