使用CodeIgniter将单选按钮连接到数据库

时间:2014-08-22 01:35:46

标签: php codeigniter

当我选择"提交"时,我无法将单选按钮值输入数据库。它不会添加到数据库中。这是表单视图:

<?php
$form = array(
        'no pengujian' => array(
                    'name' => 'NO_PENGUJIAN',
                    'size' => '30',
                    'class' => 'form_field',
                    'value' => set_value('NO_PENGUJIAN', isset($form_value['NO_PENGUJIAN']))),
        'id kendaraan' => array(
                    'name' => 'ID_KENDARAAN',
                    'size' => '30',
                    'class' => 'form_field',
                    'value' => set_value('ID_KENDARAAN', isset($form_value['ID_KENDARAAN']))),
        'no kendaraan' => array(
                    'name' => 'NO_KENDARAAN',
                    'size' => '30',
                    'class' => 'form_field',
                    'value' => set_value('NO_KENDARAAN', isset($form_value['NO_KENDARAAN']))),
        'lampu' => array(
                    'name' => 'LAMPU',
                    'size' => '30',
                    'class' => 'radio',
                    'value' => set_value('LAMPU', isset($_POST['LAMPU']))),
        'submit' => array(
                    'name' => 'submit',
                    'id' => 'submit',
                    'value' => 'Simpan'
                    )
                    );
?>
<h2><?php echo $breadcrumb ?></h2>
<!-- pesan start -->
<?php if (! empty($pesan)) : ?>
    <div class="pesan">
        <?php echo $pesan; ?>
    </div>
<?php endif ?>
<!-- pesan end -->

<!-- form start -->
<?php echo form_open($form_action); ?>
    <p>
        <?php echo form_label('No Pengujian', 'NO_PENGUJIAN'); ?>
        <?php echo form_input($form['no pengujian']); ?>
    </p>
    <?php echo form_error('NO_PENGUJIAN', '<p class = "field_error">', '</p>');?>
    <p>
        <?php echo form_label('Id Kendaraan', 'ID_KENDARAAN'); ?>
        <?php echo form_input($form['id kendaraan']); ?>
    </p>
    <?php echo form_error('ID_KENDARAAN', '<p class="field_error">', '</p>'); ?>    
    <p>
        <?php echo form_label('No Kendaraan', 'NO_KENDARAAN'); ?>
        <?php echo form_input($form['no kendaraan']); ?>
    </p>
    <?php echo form_error('NO_KENDARAAN', '<p class="field_error">', '</p>'); ?>
    <p>
        <?php echo form_label('Lampu', 'LAMPU'); ?>
        <input type ="radio" name = "lulus" value="Lulus"/> Lulus
        <input type ="radio" name = "lulus" value= "Gagal"/> Gagal
    </p>
    <p>
        <?php echo form_submit($form['submit']); ?>
        <?php echo anchor('pengujian', 'Batal', array('class' => 'cancel')) ?>
    </p>
<?php echo form_close(); ?>

这是控制器(tambah是&#34;插入&#34;数据库功能)

<?php if (!defined('BASEPATH')) exit ('No direct script access allowed');
class Pengujian extends MY_Controller
{
    public $data = array(
                    'modul'         => 'pengujian',
                    'breadcrumb'    => 'Pengujian',
                    'pesan'         => '',
                    'pagination'    => '',
                    'tabel_data'    => '',
                    'main_view'     => 'view_pengujian/pengujian_view',
                    'form_action'   => '',
                    'form_value'    => '',
                    'option_uji'    => '',
                    );
    public function __construct()
    {
        parent::__construct();
        $this->load->model('Pengujian_model', 'pengujian', TRUE);
        $this->load->helper('form');
        //$this->load->model('Penguji_model', 'penguji', TRUE);
    }
    public function index($offset = 0)
    {
        $this->session->unset_userdata('no_pengujian_sekarang', '');
        $pengujian = $this->pengujian->cari_semua($offset);
        if ($pengujian)
        {
            $tabel = $this->pengujian->buat_tabel($pengujian);
            $this->data['tabel_data'] = $tabel;
            $this->data['pagination'] = $this->pengujian->paging(site_url('pengujian/halaman'));
        }
        else
        {
            $this->data['pesan'] = 'Tidak ada data pengujian';
        }
        $this->load->view('template', $this->data);
    }
    public function tambah()
    {
        $this->data['breadcrumb']   = 'Pengujian > Tambah';
        $this->data['main_view']    = 'view_pengujian/pengujian_form';
        $this->data['form_action']  = 'pengujian/tambah';

        //$penguji = $this->penguji->cari_semua();
        //if($penguji)
        //{
         //   foreach($penguji as $row)
           // {
             //   $this->data['option_pengujian'][$row->id_penguji] = $row->penguji;
            //}
        //}
        //else
        //{
            $this->data['option_pengujian']['00'] = '-';
          //  $this->data['pesan'] = 'Data penguji tidak tersedia. Silahkan isi dahulu data penguji.';
            // if submit

            if($this->input->post('submit'))
            {
                if($this->pengujian->validasi_tambah())
                {
                    if($this->pengujian->tambah())
                    {
                        $this->session->set_flashdata('pesan', ' Proses tambah data berhasil');
                        redirect('pengujian');
                    }
                    else
                    {
                        $this->data['pesan'] = 'Proses tambah data gagal';
                        $this->load->view('template', $this->data);
                    }
                }
                else
                {
                    $this->load->view('template', $this->data);
                }
            }
            else
            {
                $this->load->view('template', $this->data);
            }
        }

这是模型:

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Pengujian_model extends CI_Model
{
    public $db_tabel    ='pengujian';
    public $per_halaman = 100;
    public $offset      = 0;
    public function cari_semua($offset = 0)
    {
        if (is_null($offset) || empty($offset))
        {
            $this->offset = 0;
        }
        else
        {
            $this->offset = ($offset * $this->per_halaman) - $this->per_halaman;
        }
        return $this->db->select('NO_PENGUJIAN, ID_KENDARAAN, NO_KENDARAAN, LAMPU, EMISI, REM, WAKTU_UJI')
        ->from($this->db_tabel)
        ->limit($this->per_halaman, $this->offset)
        ->order_by('NO_PENGUJIAN', 'ASC')
        ->get()
        ->result();
    }
    public function buat_tabel($data)
    {
        $this->load->library('table');
        $tmpl = array('row_alt_start' => '<tr class="zebra">');
        $this->table->set_template($tmpl);
        $this->table->set_heading('No', 'No Pengujian', 'Id Kendaraan', 'No Kendaraan', 'Lampu','Emisi','Rem', 'Waktu Uji', 'Aksi');
        $no = 0 + $this->offset;
        foreach ($data as $row)
        {
            $this->table->add_row(
            ++$no,
            $row->NO_PENGUJIAN,
            $row->ID_KENDARAAN,
            $row->NO_KENDARAAN,
            $row->LAMPU,
            $row->EMISI,
            $row->REM,
            $row->WAKTU_UJI,
            anchor('pengujian/edit/'.$row->NO_PENGUJIAN,'Edit',array('class' => 'edit')).' '.
            anchor('pengujian/hapus/'.$row->NO_PENGUJIAN,'Hapus',array('class' => 'delete','onclick'=>"return confirm('Anda yakin menghapus data ini?')")));
        }
        $tabel = $this->table->generate();
        return $tabel;
    }
    public function paging($base_url)
    {
        $this->load->library('pagination');
        $config = array(
                'base_url'      => $base_url,
                'total_rows'    => $this->hitung_semua(),
                'per_page'      => $this->per_halaman,
                'num_links'     => 4,
                'use_page_number'   => TRUE,
                'first link'    => '&#124;&lt; First',
                'last link'     => 'Last &gt;&#124;',
                'next link'     => 'Next &gt;',
                'prev_link'     => '&lt; Prev',
                );
        $this->pagination->initialize($config);
        return $this->pagination->create_links();
    }
    public function hitung_semua()
    {
        return $this->db->count_all($this->db_tabel);
    }
    private function load_form_rules_tambah()
    {
        $form = array(
                array(
                'field' => 'NO_PENGUJIAN',
                'label' => 'no pengujian',
                'rules' => 'required'
                ),
                array(
                'field' => 'ID_KENDARAAN',
                'label' => 'id kendaraan',
                'rules' => 'required'
                ),
                array(
                'field' => 'NO_KENDARAAN',
                'label' => 'no kendaraan',
                'rules' => 'required'
                ),
                array(
                'field' => 'LAMPU',
                'label' => 'lampu',
                'rules' => 'required'
                ),
                );
            return $form;
    }
    public function validasi_tambah()
    {
        $form = $this->load_form_rules_tambah();
        $this->form_validation->set_rules($form);
        if($this->form_validation->run())
        {
            return TRUE;
        }
        else
        {
            return FALSE;
        }
    }
    public function tambah()
    {
        $pengujian = array(
                'NO_PENGUJIAN' => $this->input->post('NO_PENGUJIAN'),
                'ID_KENDARAAN' => $this->input->post('ID_KENDARAAN'),
                'NO_KENDARAAN' => $this->input->post('NO_KENDARAAN'),
                'LAMPU' => $this->input->post('lampu[]'),
                //'EMISI' => $this->input->post('EMISI'),
                //'REM' => $this->input->post('REM')
                );
        $lulus = $_POST["lulus"];
        //$statement = "INSERT INTO pengujian VALUES($lulus)"
        $this->db->insert($this->db_tabel, $pengujian);
        if($this->db->affected_rows() > 0)
        {
            return TRUE;
        }
        else
        {
            return FALSE;
        }
    }

我在表格中没有遇到任何麻烦。问题是单选按钮&#34; lampu&#34;

1 个答案:

答案 0 :(得分:0)

我认为,最好的办法是检查出错的地方。在这种情况下,通常通过检查值是否传递回控制器和模型来执行此操作。通过这种方式,您可以更好地了解代码中的内容。做这样的事情:

在模型中:

     public function tambah()
{
    // Check to see if we get a value. If not, do the same in the controller
    var_dump($this->input->post('lampu'));
    exit;

    $pengujian = array(
            'NO_PENGUJIAN' => $this->input->post('NO_PENGUJIAN'),
            'ID_KENDARAAN' => $this->input->post('ID_KENDARAAN'),
            'NO_KENDARAAN' => $this->input->post('NO_KENDARAAN'),
            'LAMPU' => $this->input->post('lampu[]'),
            //'EMISI' => $this->input->post('EMISI'),
            //'REM' => $this->input->post('REM')
            );
    $lulus = $_POST["lulus"];
    //$statement = "INSERT INTO pengujian VALUES($lulus)"
    $this->db->insert($this->db_tabel, $pengujian);
    if($this->db->affected_rows() > 0)
    {
        return TRUE;
    }
    else
    {
        return FALSE;
    }
}

我希望这有点帮助......