在Codeigniter 3中无法将数据添加到数据库中,只需重新加载相同的页面

92dk7w1h  于 2023-09-28  发布在  其他
关注(0)|答案(1)|浏览(110)

我正在学习使用谷歌Map的API通过利用CodeIgniter Google Maps V3 API Library的库来制作CRUD GIS Web项目。但我被卡在向数据库添加数据的阶段。表单标记中的提交按钮无法向数据库添加数据。当我点击提交按钮,它不给予任何错误,只是重新加载相同的页面和数据在不插入数据库。下面是我的代码:
Autoloadautoload.php已经使用libraries & helper

$autoload['libraries'] = array('session', 'database', 'form_validation', 'upload');
$autoload['helper'] = array('url', 'form', 'file');

控制器Dbmaps.php

<?php

defined('BASEPATH') or exit('No direct script access allowed');

class Dbmaps extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();

        // Load Dependencies
        $this->load->library('googlemaps');
        $this->load->model('m_dbmaps');
    }

    // Get All Data
    public function index()
    {
        $data = array(
            'title' => 'Database Maps &mdash; Sales Tracking',
            'section' => 'Database Maps',
            'map' => $this->googlemaps->create_map(),
            'dbmaps' => $this->m_dbmaps->lists(),
            'isi' => 'dbmaps/v_lists'
        );
        $this->load->view('template/v_wrapper', $data, FALSE);
    }

    // Create New Data
    public function input()
    {
        // ------------------ Google Maps ------------------
        // Config Maps
        $config = array(
            'center' => '-6.981782663363796, 110.40922272688273',
            'zoom' => '15',
            'map_height' => '600px'
        );
        // Mengeksekusi Config Maps + Show Maps
        $this->googlemaps->initialize($config);

        // Setting Markers
        $marker = array(
            'position' => '-6.981782663363796, 110.40922272688273',
            'draggable' => true,
            'ondragend' => 'setToForm(event.latLng.lat(), event.latLng.lng());'
        );
        // Mengeksekusi Markers
        $this->googlemaps->add_marker($marker);
        // ------------------ Google Maps ------------------

        // ------------------ Form Validation ------------------
        // Buatkan form validation
        $this->form_validation->set_rules('nama_maps', 'Nama Maps', 'required');
        $this->form_validation->set_rules('no_telpon', 'Nomor Telpon', 'required');
        $this->form_validation->set_rules('alamat', 'Alamat', 'required');
        $this->form_validation->set_rules('latitude', 'Latitude', 'required');
        $this->form_validation->set_rules('longitude', 'Longitude', 'required');
        $this->form_validation->set_rules('deskripsi', 'Deskripsi', 'required');

        // Jika form validation gagal
        if ($this->form_validation->run() == FALSE) {
            $data = array(
                'title' => 'Input Database Maps &mdash; Sales Tracking',
                'section' => 'Input Database Maps',
                'map' => $this->googlemaps->create_map(),
                'isi' => 'dbmaps/v_add'
            );
            $this->load->view('template/v_wrapper', $data, FALSE);
        }

        // Jika form validation berhasil
        else {
            $data = array(
                'nama_maps' => $this->input->post('nama_maps'),
                'no_telpon' => $this->input->post('no_telpon'),
                'alamat' => $this->input->post('alamat'),
                'latitude' => $this->input->post('latitude'),
                'longitude' => $this->input->post('longitude'),
                'deskripsi' => $this->input->post('deskripsi')
            );
            $this->m_dbmaps->input($data);
            $this->session->set_flashdata('pesan', 'Data berhasil ditambahkan!');
            redirect('dbmaps');
        }
        // ------------------ Form Validation ------------------
    }
}

/* End of file Dbmaps.php */

型号M_dbmaps.php

<?php

defined('BASEPATH') or exit('No direct script access allowed');

class M_dbmaps extends CI_Model
{

    // Get Data
    public function lists()
    {
        $this->db->select('*');
        $this->db->from('tbl_dbmaps');
        $this->db->order_by('id_maps', 'desc');
        return $this->db->get()->result();
    }

    // Input Data
    public function input($data)
    {
        $this->db->insert('tbl_dbmaps', $data);
    }
}

视图v_add,尤其是输入表单。

<!-- Form Input -->
<?= form_open('dbmaps/input'); ?>
<div class="form-group">
    <label>Nama Maps</label>
    <div class="input-group">
        <div class="input-group-prepend">
            <div class="input-group-text">
                <i class="fas fa-file-signature"></i>
            </div>
        </div>
        <input type="text" class="form-control" name="nama_maps" placeholder="Nama Maps" required>
    </div>
</div>
<div class="form-group">
    <label>Nomor Telpon</label>
    <div class="input-group">
        <div class="input-group-prepend">
            <div class="input-group-text">
                <i class="fas fa-phone"></i>
            </div>
        </div>
        <input type="text" class="form-control phone-number" name="no_telpon" placeholder="Nomor Telpon" required>
    </div>
</div>
<div class="form-group">
    <label>Alamat</label>
    <div class="input-group">
        <div class="input-group-prepend">
            <div class="input-group-text">
                <i class="fas fa-address-card"></i>
            </div>
        </div>
        <input type="text" class="form-control" name="alamat" placeholder="Alamat Maps" required>
    </div>
</div>
<div class="form-group">
    <label>Latitude</label>
    <div class="input-group">
        <div class="input-group-prepend">
            <div class="input-group-text">
                <i class="fas fa-map-marker-alt"></i>
            </div>
        </div>
        <input type="text" class="form-control" name="latitude" placeholder="Latitude Maps" required disabled>
    </div>
</div>
<div class="form-group">
    <label>Longitude</label>
    <div class="input-group">
        <div class="input-group-prepend">
            <div class="input-group-text">
                <i class="fas fa-map-marker-alt"></i>
            </div>
        </div>
        <input type="text" class="form-control" name="longitude" placeholder="Longitude Maps" required disabled>
    </div>
</div>
<div class="form-group">
    <label>Deskripsi</label>
    <textarea type="text" class="form-control" name="deskripsi" style="height: 100px;" placeholder="..." required></textarea>
</div>
<div class="form-group">
    <button class="btn btn-primary" type="submit">Save</button>
    <button class="btn btn-danger" type="reset">Cancel</button>
</div>
<?= form_close(); ?>
2g32fytz

2g32fytz1#

不应禁用名为latitude和longitude的窗体中的。

相关问题