【问题标题】:Cant write data in database using codeigniter无法使用codeigniter将数据写入数据库
【发布时间】:2019-08-27 17:54:35
【问题描述】:

我有一个控制器、模型和视图文件。我正在尝试使用视图文件上的表单将数据写入数据库。我正在尝试使用 Codeigniter 验证库和由定义的一些方法来验证发布数据。此验证在 Controller 中完成。然后我试图将数据数组传递给模型。在模型中,我试图读取数据数组并构建查询以在数据库中插入数据。

问题是没有数据写入数据库。 我在浏览器中看不到任何可见的错误。我已经坚持了一段时间了。任何帮助表示赞赏。提前致谢。

控制器

function add_customer() {

    $this->load->model('Customer_Model');
    $data['title'] = "Add New Customer";


    $this->load->view('templates/header' , $data);
    $this->load->view('dashboard/add_customer' , $data);
    $this->load->view('templates/footer' , $data);

    if($this->input->post())
    {

        $this->form_validation->set_rules('name', 'Name', 'trim|required');
        $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|customer_email_exists');
        $this->form_validation->set_rules('mobile', 'Mobile', 'trim|required|customer_mobile_exists');
        $this->form_validation->set_rules('address', 'Address', 'trim|required');

        if ($this->form_validation->run()){
              $customer_data = array(
                        'name' => $this->validation->name,
                        'email' => $this->validation->email,
                        'mobile' => $this->validation->mobile,
                        'address' => $this->validation->address
                        );
                $this->Customer_Model->add_customer($customer_data);
            }else{

            }
    }
}
public function customer_email_exists($email) {
    $this->load->model('Customer_Model');   
    if(!$this->Customer_Model->email_exists($email)){
            return true;
        }else{
            $this->form_validation->set_message('email_exists', 'Email already registered, try another one.');
            return false;
        }
    }
public function customer_mobile_exists($mobile) {
    $this->load->model('Customer_Model');   
    if(!$this->Customer_Model->mobile_exists($mobile)){
            return true;
        }else{
            $this->form_validation->set_message('email_exists', 'Email already registered, try another one.');
            return false;
        }
    }

}

型号

class Customer_Model extends CI_Model{
function add_customer($customer_data)
{

    $data = array(
        'id'=>'',
        'name'=>$customer_data["name"],
        'email'=>$customer_data["email"],
        'mobile'=>$customer_data["mobile"],
        'address'=>$customer_data["address"]
    );
    $this->db->insert('customer',$data);
    $this->db->query($query);
}
public function email_exists($email) {
        $this->db->where("email = '$email' AND email != ''");
        $query = $this->db->get('customer');
        if ($query->num_rows() > 0){
            return true;
        }else{
            return false;
        }
    }
    public function mobile_exists($mobile) {
        $this->db->where('mobile',$mobile);
        $query = $this->db->get('customer');
        if ($query->num_rows() > 0){
            return true;
        }else{
            return false;
        }
    }}

查看

<section class="versir-section">
<div class="container">
    <div class="row">
        <div class="col-12">
            <?php echo validation_errors(); ?>
            <form method="post">
                <table width="600" border="1" cellspacing="5" cellpadding="5">
                  <tr>
                    <td width="230">Customer Name</td>
                    <td width="329"><input type="text" name="name"/></td>
                  </tr>
                  <tr>
                    <td>Customer Email </td>
                    <td><input type="text" name="email"/></td>
                  </tr>
                  <tr>
                    <td>Customer Mobile </td>
                    <td><input type="text" name="mobile"/></td>
                  </tr>
                   <tr>
                    <td>Customer Address </td>
                    <td><input type="text" name="address"/></td>
                  </tr>
                  <tr>
                    <td colspan="2" align="center"><input type="submit" name="save" value="Save Data"/></td>
                  </tr>
                </table>

        </form>
        </div>
    </div>
</div>

【问题讨论】:

  • 如果您在进入模型之前在控制器中输出$customer_data - 其中是否有任何数据?
  • 数组包含所有数据
  • 好的 - 下一步不是做$this-&gt;db-&gt;insert('customer',$data); 而是做$this-&gt;db-&gt;get_compiled_insert('customer',$data);。输出它,然后直接在数据库中运行查询,看看会发生什么。
  • 就可以了。直接在数据库中运行查询
  • 那么直接运行查询可以使用正确的数据吗?

标签: sql model-view-controller codeigniter-3


【解决方案1】:

嗯!我认为在此代码部分中,您应该从输入中获取发布数据,而不是从验证中获取

if ($this->form_validation->run()){
  $customer_data = array(
      'name' => $this->input->post('name'),
      'email' => $this->input->post('email'),
      'mobile' => $this->input->post('mobile'),
      'address' => $this->input->post('address'),
  );
  $this->Customer_Model->add_customer($customer_data);
}else{

}            

【讨论】:

  • 试过没有效果
  • 您是否陷入了将所有数据添加到Customer_Model的方法中?并且数组键与表中的列名匹配?
  • 让我再检查一遍
  • 如果 id 字段是自动递增的,则不应将其传递给 insert
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-10-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-08
相关资源
最近更新 更多