CodeIgniter Forums

Full Version: A PHP Error was encountered Severity: Notice Message: Undefined index: Satker
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am very confused of this error messages. Please help me


Quote:A PHP Error was encountered
Severity: Notice
Message: Undefined index: Satker
Filename: controllers/Members.php
Line Number: 67
Backtrace:
File: C:\xampp\htdocs\nonameapp\application\controllers\Members.php
Line: 67
Function: _error_handler
File: C:\xampp\htdocs\nonameapp\index.php
Line: 315
Function: require_once




Quote:A Database Error Occurred
Error Number: 1048
Column 'satker' cannot be null
INSERT INTO `t_satker` (`kode`, `satker`, `created`, `modified`) VALUES ('asas', NULL, '2019-10-14 13:29:23', '2019-10-14 13:29:23')
Filename: C:/xampp/htdocs/nonameapp/system/database/DB_driver.php
Line Number: 691


This is my controller
Code:
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Members extends CI_Controller {
   
    function __construct() {
        parent::__construct();
       
        // Load member model
        $this->load->model('member');
       
        // Load form validation library
        $this->load->library('form_validation');
       
        // Load file helper
        $this->load->helper('file');
    }
   
    public function index(){
        $data = array();
       
        // Get messages from the session
        if($this->session->userdata('success_msg')){
            $data['success_msg'] = $this->session->userdata('success_msg');
            $this->session->unset_userdata('success_msg');
        }
        if($this->session->userdata('error_msg')){
            $data['error_msg'] = $this->session->userdata('error_msg');
            $this->session->unset_userdata('error_msg');
        }
       
        // Get rows
        $data['members'] = $this->member->getRows();
       
        // Load the list page view
        $this->load->view('members/index', $data);
    }
   
    public function import(){
        $data = array();
        $memData = array();
       
        // If import request is submitted
        if($this->input->post('importSubmit')){
            // Form field validation rules
            $this->form_validation->set_rules('file', 'CSV file', 'callback_file_check');
           
            // Validate submitted form data
            if($this->form_validation->run() == true){
                $insertCount = $updateCount = $rowCount = $notAddCount = 0;
               
                // If file uploaded
                if(is_uploaded_file($_FILES['file']['tmp_name'])){
                    // Load CSV reader library
                    $this->load->library('CSVReader');
                   
                    // Parse data from CSV file
                    $csvData = $this->csvreader->parse_csv($_FILES['file']['tmp_name']);
                   
                    // Insert/update CSV data into database
                    if(!empty($csvData)){
                        foreach($csvData as $row){ $rowCount++;
                           
                            // Prepare data for DB insertion
                            $memData = array(
                                'kode' => $row['Kode'],
                                'satker' => $row['Satker'],
                            );
                           
                            // Check whether email already exists in the database
                            $con = array(
                                'where' => array(
                                   
                                ),
                                'returnType' => 'count'
                            );
                            $prevCount = $this->member->getRows($con);
                           
                            if($prevCount > 0){
                                // Update member data
                                $condition = array();
                                $update = $this->member->update($memData, $condition);
                               
                                if($update){
                                    $updateCount++;
                                }
                            }else{
                                // Insert member data
                                $insert = $this->member->insert($memData);
                               
                                if($insert){
                                    $insertCount++;
                                }
                            }
                        }
                       
                        // Status message with imported data count
                        $notAddCount = ($rowCount - ($insertCount + $updateCount));
                        $successMsg = 'Members imported successfully. Total Rows ('.$rowCount.') | Inserted ('.$insertCount.') | Updated ('.$updateCount.') | Not Inserted ('.$notAddCount.')';
                        $this->session->set_userdata('success_msg', $successMsg);
                    }
                }else{
                    $this->session->set_userdata('error_msg', 'Error on file upload, please try again.');
                }
            }else{
                $this->session->set_userdata('error_msg', 'Invalid file, please select only CSV file.');
            }
        }
        redirect('members');
    }
   
    /*
     * Callback function to check file value and type during validation
     */
    public function file_check($str){
        $allowed_mime_types = array('text/x-comma-separated-values', 'text/comma-separated-values', 'application/octet-stream', 'application/vnd.ms-excel', 'application/x-csv', 'text/x-csv', 'text/csv', 'application/csv', 'application/excel', 'application/vnd.msexcel', 'text/plain');
        if(isset($_FILES['file']['name']) && $_FILES['file']['name'] != ""){
            $mime = get_mime_by_extension($_FILES['file']['name']);
            $fileAr = explode('.', $_FILES['file']['name']);
            $ext = end($fileAr);
            if(($ext == 'csv') && in_array($mime, $allowed_mime_types)){
                return true;
            }else{
                $this->form_validation->set_message('file_check', 'Please select only CSV file to upload.');
                return false;
            }
        }else{
            $this->form_validation->set_message('file_check', 'Please select a CSV file to upload.');
            return false;
        }
    }
}

And this is view
Code:
<div class="container">
    <h2>Members List</h2>
    
    <!-- Display status message -->
    <?php if(!empty($success_msg)){ ?>
    <div class="col-xs-12">
        <div class="alert alert-success"><?php echo $success_msg; ?></div>
    </div>
    <?php } ?>
   
    <?php if(!empty($error_msg)){ ?>
    <div class="col-xs-12">
        <div class="alert alert-danger"><?php echo $error_msg; ?></div>
    </div>
    <?php } ?>
    
    <div class="row">
        <!-- Import link -->
        <div class="col-md-12 head">
            <div class="float-right">
                <a href="javascript:void(0);" class="btn btn-success" onclick="formToggle('importFrm');"><i class="plus"></i> Import</a>
            </div>
        </div>
        
        <!-- File upload form -->
        <div class="col-md-12" id="importFrm" style="display: none;">
            <form action="<?php echo base_url('members/import'); ?>" method="post" enctype="multipart/form-data">
                <input type="file" name="file" />
                <input type="submit" class="btn btn-primary" name="importSubmit" value="IMPORT">
            </form>
        </div>
       
        <!-- Data list table -->
        <table class="table table-striped table-bordered">
            <thead class="thead-dark">
                <tr>
                    <th>#ID</th>
                    <th>Kode Satker</th>
                    <th>Nama Satker</th>
                </tr>
            </thead>
            <tbody>
                <?php if(!empty($members)){ foreach($members as $row){ ?>
                <tr>
                    <td><?php echo $row['id']; ?></td>
                    <td><?php echo $row['kode']; ?></td>
                    <td><?php echo $row['satker']; ?></td>
                </tr>
                <?php } }else{ ?>
                <tr><td colspan="5">No member(s) found...</td></tr>
                <?php } ?>
            </tbody>
        </table>
    </div>
</div>

<script>
function formToggle(ID){
    var element = document.getElementById(ID);
    if(element.style.display === "none"){
        element.style.display = "block";
    }else{
        element.style.display = "none";
    }
}
</script>
@alazhar.faiz,

Have you tried testing the insert statement to see if it is correct?
Hi alazhar.faiz,

You could also try making sure that the database definition allows NULL in the table.

The error suggests that the "satker" column is defined as NOT NULL. Or you could change the hard coded NULL into "", which would insert a string (assuming it is a string of course), or the correct dummy type.

If the error is from the imported data, then check the source data for a NULL value.