如何将此表单输入插入我的数据库

时间:2017-08-08 12:30:37

标签: ajax codeigniter

这是登录模式

<!-- Signin Small Modal-->
    <div class="modal fade bd-example-modal-sm" id="mySignModal" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true">
    <!--<div class="modal fade bd-example-modal-sm" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true">-->
      <div class="modal-dialog modal-sm" role="document">
        <div class="modal-content">
           <div class="modal-header btn-primary">
            <h5 class="modal-title">Sign-in</h5>
            <!--<button type="button" class="close" data-dismiss="modal" aria-label="Close">
              <span aria-hidden="true">&times;</span>
            </button>-->
          </div>
          <div class="modal-body">
            <form id="signinForm" action="" method="post">
              <input type="hidden" name="txtId" value="0">
              <div class="form-group">
                <label for="txtUsername" style="font-size: 12px;">Username</label>
                <input type="text" class="form-control" name="txtUsername" placeholder="Enter username">
                <!--<small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>-->
              </div>
              <div class="form-group">
                <label for="txtEmail" style="font-size: 12px;">Email</label>
                <input type="email" class="form-control" name="txtEmail" aria-describedby="emailHelp" placeholder="Enter email">
                <!--<small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>-->
              </div>
              <div class="form-group">
                <label for="txtPassword" style="font-size: 12px;">Password</label>
                <input type="password" class="form-control" name="txtPassword" placeholder="Password">
              </div>
              <div class="form-group">
                <label for="txtConfirmPass" style="font-size: 12px;">Confirm Password</label>
                <input type="password" class="form-control" name="txtConfirmPass" placeholder="Password">
              </div>
            </form>
          </div>
          <div class="modal-footer">
            <button type="button" id="btnRegister" class="btn-sm btn-success">Submit</button>
            <button type="button" class="btn-sm btn-secondary" data-dismiss="modal">Close</button>
          </div>

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

这是我的javascript,用于将模态的输入插入到我的数据库

<script>

        $(function(){

            $('#btnCreateAccount').click(function(){
                $('#mySignModal').modal('show');
            });

            $('#btnRegister').click(function(){
                $('#mySignModal').find('.modal-title').text('Sign-in');
                $('#signinForm').attr('action', '<?php echo base_url() ?>index.php/login/addUsers');
            });

        });
    </script>

这是我的登录控制器

public function addUsers(){
        $result = $this->em->addUsers();
        $msg['success'] = false;
        $msg['type'] = 'add';
        if($result){
            $msg['success'] = true;
        }
        echo json_encode($msg);
    }

这是我的用户模型

public function addUsers(){
        $field = array(
            'username'=>$this->input->post('txtUsername'),
            'email'=>$this->input->post('txtEmail'),
            'password'=>$this->input->post('txtPassword')
            //'created_at'=>date('Y-m-d H:i:s')
            );

        /*$field = array(
            'username'=>'hardcoded username',
            'email'=>'hardcoded email',
            'password'=>'hardcoded email'
            //'created_at'=>date('Y-m-d H:i:s')
            );*/

        $this->db->insert('users', $field);
        if($this->db->affected_rows() > 0){
            return true;
        }else{
            return false;
        }
    }

它不会让我将登录模式中的数据插入到我的数据库中,当我检查网址http://localhost/ci_testblog/index.php/login/addUsers时它不会让我这样说:

  

发生数据库错误

     

错误号码:1048

     

列'username'不能为null

     

INSERT INTO usersusernameemailpassword)VALUES(NULL,   NULL,NULL)

     

文件名:C:/xampp/htdocs/ci_testblog/system/database/DB_driver.php

     

行号:691

这个可能有什么问题?

2 个答案:

答案 0 :(得分:0)

首先,您必须通过print_r($ field)检查数组中是否包含值;因为在您的值中,所有字段在db error中都为null。您还必须在db中为列设置默认值。感谢

答案 1 :(得分:0)

请取模态按钮输入类型=“提交”输入类型=“按钮”输入控制器的功能作为模式中表单标签的动作