繁体   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