簡體   English   中英

使用Codeigniter將數據插入數據庫失敗

[英]Failed insert data to database with codeigniter

沒有顯示錯誤,但是無法插入,並且在我的數據庫中未插入數據。 我是CI的新手。

這是我的控制器

class Register extends CI_Controller {

    function __construct(){
        parent::__construct();
        $this->load->model('m_register');
    }

    public function index()
    {
        $this->load->view('user/register');
    }

    public function daftar_akun(){

        $username = $this->input->post('reg_username');
        $password = $this->input->post('reg_password');
        $email = $this->input->post('email');
        $no_telp = $this->input->post('no_telp');
        $nama_lengkap = $this->input->post('nama_lengkap');
        $no_ktp = $this->input->post('no_ktp');
        $alamat = $this->input->post('alamat');
        $role_id = 3;
        $status = 0;

        $data = array(
            'username'=> $username,
            'password' => $password,
            'email' => $email,
            'no_telp' => $no_telp,
            'nama_lengkap' => $nama_lengkap,
            'no_ktp' => $no_ktp,
            'alamat' => $alamat,
            'role_id' => $role_id,
            'status' => $status
            );
        $this->m_register->register_akun($data);
        redirect(base_url("login"));
    }
}

這是我的模特

class M_register extends CI_Model{

    function register_akun($data){
        $this->db->insert('user',$data);
    }
}

您不在模型頁面中返回任何東西。 請試試這個。

function register_akun($data){
    if($this->db->insert('user',$data)){
        return true;
    }
    return false;
}

您可以檢查模型功能以查看最后輸入的ID(如果插入了記錄)。 就像是:

public function insert($data)
         {
             $db = $this->db;
             $db->insert('', $data);
             $id = $db->insert_id();

             if ($id > 0)
             {
                 return $id;
             }
             else
             {
                 return false;
             }
         }

但是在調用此函數之前,請在daftar_akun()函數中為$數據數組提供一個var_dump,以查看其是否正確填充。 祝好運

對於錯誤顯示,請在codeigniter項目根目錄的index.php中設置錯誤報告1。 這將為您全力以赴。 喜歡

define('ENVIRONMENT', 'production');
/*
*---------------------------------------------------------------
* ERROR REPORTING
*---------------------------------------------------------------
*
* Different environments will require different levels of error  reporting.
* By default development will show errors but testing and live will hide them.
*/

if (defined('ENVIRONMENT'))
{
switch (ENVIRONMENT)
{
    case 'development':
        error_reporting(1);
    break;

    case 'testing':
    case 'production':
        error_reporting(1);
    break;

    default:
        exit('The application environment is not set correctly.');
    }
}

暫無
暫無

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

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