【问题标题】:how to insert this form input to my database如何将此表单输入插入我的数据库
【发布时间】:2018-01-16 00:43:46
【问题描述】:

这是登录的模式

<!-- 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;
        }
    }

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

发生数据库错误

错误号:1048

“用户名”列不能为空

插入usersusernameemailpassword)值(NULL, 空,空)

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

行号:691

这有什么问题?

【问题讨论】:

  • 您可以将数据库表字段值设置为默认NULL
  • 我建议使用用户名插入。 @NikunjRathod- 为什么用户名会适当地为空?
  • 你试过echo你的$this->input->post('txtUsername') 看看他们有没有一些数据

标签: ajax codeigniter


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    请使用模态按钮 input type="submit" insted of input type="button" 将控制器的功能作为模态中表单标签的操作

    【讨论】:

      猜你喜欢
      • 2016-11-13
      • 1970-01-01
      • 2016-01-20
      • 2022-09-23
      • 2014-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多