0

This is the modal of sign-in

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

under this is my javascript to insert the input from modals to my database

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

this is my login controller

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

and this is my Users model

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

It wont let me insert the data from sign-in modal to my database it wont let me when I check the url http://localhost/ci_testblog/index.php/login/addUsers it says that:

A Database Error Occurred

Error Number: 1048

Column 'username' cannot be null

INSERT INTO users (username, email, password) VALUES (NULL, NULL, NULL)

Filename: C:/xampp/htdocs/ci_testblog/system/database/DB_driver.php

Line Number: 691

What could be wrong on this one?

Mary
  • 197
  • 1
  • 15
Lestah
  • 63
  • 1
  • 2
  • 9

2 Answers2

0

First you have to check the values is include in the array or not by print_r($field); because in your values all fields are null in db error.You have to set a default value in db also for the columns. Thanks

Anand Pandey
  • 2,025
  • 3
  • 20
  • 39
0

please take modal button input type="submit" insted of input type="button" give controller's function as action of form tag in modal

user7596840
  • 137
  • 15