【问题标题】:Column 'username' cannot be null列“用户名”不能为空
【发布时间】:2016-10-28 11:46:12
【问题描述】:

我想知道为什么会出现此错误消息。列“用户名”不能为空 ?

我将 Null 设置为 No。


发生数据库错误

错误号:1048

“用户名”列不能为空

插入loginusernameemailpasswordrole)值 (空,空, '$2y$10$FaVW7V1yEDB0NnlmgwPHQ.SQi34ZCXi9ABJDOebDflZ.cqcSwZAoW', NULL)

文件名:C:/Program 文件/EasyPHP-Devserver-16.1/eds-www/companygiondaci/system/database/DB_driver.php

行号:691

views/addusers.php

   <div class="row-fluid">
            <div class="span12">

                <?php $this->load->library('form_validation'); ?>

                <?php echo $successmessage; ?>

                <?php echo validation_errors(); ?>

                <?php echo form_open('cpages/addusersdb'); ?>

                <div class="widget-box">
                    <div class="widget-title"><h5>Add User</h5></div>
                    <div class="widget-content">

                    <table border="0" style="width: 100%; height: 90px;">
                        <tr>
                            <td>USERNAME</td>
                            <td><input type="text" name="username"></td>
                        </tr>
                        <tr>
                            <td>EMAIL</td>
                            <td><input type="text" name="email"></td>
                        </tr>
                        <tr>
                            <td>PASSWORD</td>
                            <td><input type="password" name="password"></td>
                        </tr>
                        <tr>
                            <td>ROLE</td>
                            <td><?php echo form_dropdown('roles', $options, 'administrator'); ?></td>
                        </tr>
                        <tr>
                            <td></td>
                            <td><input type="submit" class="edit" value="ADD"></td>
                        </tr>                           
                    </table>            
                    </div>
                </div>                  
            </div>
        </div>

models/Mpages.php

public function add_user()
{   
    $options = [
      'roles' => 'administrator',
    ];

    $hash = password_hash($this->input->post('password'), PASSWORD_BCRYPT, $options);

    $data = array(
        'username' => $this->input->post('username'),
        'email' => $this->input->post('email'),
        'password' => $hash,
        'role' => $this->input->post('roles')       
    );      

    return $this->db->insert('login', $data);

}

【问题讨论】:

  • 设置为allow null
  • allow null 不是解决方案。 检查并修复此错误唯一解决方案。
  • 为什么要在视图中加载表单验证库? codeigniter.com/user_guide/libraries/…你真的需要看一下用户指南,
  • 并将您的用户名设置为类似 varchar 100 的名称,并将电子邮件设置为类似的名称

标签: codeigniter


【解决方案1】:
    if($this->input->post()) {

            $hash = password_hash($this->input->post('password'), PASSWORD_BCRYPT, $options);

            $data = array(
                'username' => $this->input->post('username'),
                'email' => $this->input->post('email'),
                'password' => $hash,
                'role' => $this->input->post('roles')       
            ); 
            $this->YOUR_MODEL->add_user($data);
    }

这应该进入你的控制器。

而你的模型函数应该是这样的

public function add_user($data)
{     

    return $this->db->insert('login', $data);

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-09
    • 1970-01-01
    • 1970-01-01
    • 2016-07-30
    • 2017-07-15
    • 2022-09-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多