簡體   English   中英

codeigniter : 在數據庫中插入數據

[英]codeigniter : insert data in database

我正在嘗試將發布的數據插入數據庫,如下所示。

public function submit_signup(){
            if($this->input->post('submit')){
                $user_data['data']=array(
                    'username'   =>     $this->input->post('username'),
                    'email'      =>     $this->input->post('email'),
                    'password'   =>     $this->input->post('password'),
                    'mobile'     =>     $this->input->post('mobile') 
                );
            }
            $this->load->model('user_model');
            if($this->user_model->register_user($user_data)){
                echo 'data entered';
            }
            $this->load->view('templates/header');
            $this->load->view('test',$user_data);
            $this->load->view('templates/footer');
        }

我的型號:

class user_model extends CI_Model {
    public function __construct() {
        parent::__construct();
    }

    public function register_user($data){
        if(!$this->db->insert('user',$data)) {
            echo 'Data not entered';
        }
        return true; 
    }
}

當我按下表格時,這就是我得到的

遇到 PHP 錯誤

嚴重性:注意

消息:數組到字符串的轉換

文件名:mysql/mysql_driver.php

需要你的幫助

使您的陣列為

 $user_data=array(
                    'username'   =>     $this->input->post('username'),
                    'email'      =>     $this->input->post('email'),
                    'password'   =>     $this->input->post('password'),
                    'mobile'     =>     $this->input->post('mobile') 
                );

刪除['data'] 。然后將其用於插入查詢

檢查手冊

為什么不將確切的數組$user_data['data']傳遞給模型函數

if($this->user_model->register_user($user_data['data']))
{
    echo 'data entered';
}

將數據插入數據庫你可以試試這個:

$this->load->model('user_model');

$user_data=[
  'username'   =>     $this->input->post('username'),
  'email'      =>     $this->input->post('email'),
  'password'   =>     $this->input->post('password'),
  'mobile'     =>     $this->input->post('mobile') 
];
$this->user_model->register_user($user_data)

//無模型

$this->db->insert('table_name', $user_data);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM