数据插入codeignetter抛出错误

时间:2015-03-09 20:45:24

标签: codeigniter

  

遇到PHP错误严重性:通知消息:未定义   property:Registerform :: $ db文件名:core / Model.php行号:51   控制器文件:

遇到PHP错误

严重性:注意

消息:未定义属性:Registerform :: $ db

文件名:core / Model.php

行号:51

  

控制器文件:

<?php
class Registerform extends CI_Controller {
    function __construct() {
        parent::__construct();
        $this->load->model('Registerform_model');
    }

    public function index() {

        //$this->load->database();

        $this->load->helper("form");
        $this->load->library("form_validation");
        $this->form_validation->set_rules("first", "First Name","required");
        $this->form_validation->set_rules("last", "Last Name","required");
        $this->form_validation->set_rules("email", "Email Address","required|valid_email");
        $this->form_validation->set_rules("password", "Password","required");
        $this->form_validation->set_rules("phone", "Phone Number","required");
        $this->form_validation->set_rules("city", "City","required");
        $this->form_validation->set_rules("addrss", " Your Address","required");
        if ($this->form_validation->run() == false) {
            $this->load->view("registerform_view");
        } else {
            $first = $_POST["first"];
            $last = $_POST["last"];
            $email = $_POST["email"];
            $password = $_POST["password"];
            $phone = $_POST["phone"];
            $city = $_POST["city"];
            $addrss = $_POST["addrss"];
            $data = array(
                      'first'=>$first,
                      'last'=>$last,
                      'email'=>$email,
                      'password'=>$password,
                      'phone'=>$phone,
                      'city'=>$city,
                      'addrss'=>$addrss
             );                       

            //$data="insert into address (first_name,last_name,email,password,phone,city,add) values('$first','$last','$email','$password','$phone','$city','$add')";

             $gg = $this->Registerform_model->insert_address($data);
             echo $gg;
             $this->load->view("registerformsuccess");

        }
    }
}
?>

模型文件:

<?php
class Registerform_model extends CI_Model {
    function __construct() {
        parent::__construct();
    }

    public function insert_address($data){                  
        $this->db->insert("address", $data);                    
    }
}
?>

查看文件:

<?php               
echo validation_errors();           
echo form_open("Registerform");
?>
<label for="first">First Name</label>
<input type="text" name="first" id="first"><br>

<label for="last">Last Name</label>
<input type="text" name="last" id="last"><br>

<label for="email">Email Address</label>
<input type="text" name="email" id="email"><br>

<label for="password">Password</label>
<input type="password" name="password" id="password"><br>

<label for="phone">Phone</label>
<input type="text" name="phone" id="phone"><br>

<label for="city">City Name</label>
<input type="text" name="city" id="city"><br>

<label for="addrss">Your Address</label>
<input type="text" name="addrss" id="addrss"><br>

<input type="submit" name="submit" id="submit" value="Submit">
<?php echo form_close(); ?>

2 个答案:

答案 0 :(得分:1)

<?php
class Registerform_model extends CI_Model {
    function __construct() {
        parent::__construct();

        }
           public function insert_address($data){   
                 $this->load->database();               
        $this->db->insert("address", $data);                    
    }
        }
?>

它现在为我工作。

答案 1 :(得分:0)

您必须加载数据库。 将此行放在模型文件构造函数中:

 $this->load->database();