Подключение радио кнопки к базе данных с помощью 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'
 )
 );
?>

<!-- pesan start -->
<!--?php if (! empty($pesan)) : ?-->
 <div>
 <!--?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>
 <!--?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>
 <!--?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>
 <!--?php echo form_label('Lampu', 'LAMPU'); ?-->
  Lulus
  Gagal
 </p>
 <p>
 <!--?php echo form_submit($form['submit']); ?-->
 <!--?php echo anchor('pengujian', 'Batal', array('class' =--> 'cancel')) ?>
 </p>
<!--?php echo form_close(); ?-->

Это контроллер (тамба - функция "вставить" в базу данных)

<!--?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 ****** 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' => '');
 $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' => '|< First',
 'last link' => 'Last >|',
 'next link' => 'Next >',
 'prev_link' => '< 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;
 }
 }

У меня нет проблем в поле формы. Беда в том, что радиокнопка "lampu"

1 ответ

Думаю, лучше всего проверить, где это происходит. Я обычно делаю это, в этом случае, проверяя, передается ли значение обратно контроллеру и модели. Таким образом, вы лучше понимаете, что происходит внутри вашего кода. Сделайте что-то вроде этого:

В модели:

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

Надеюсь, это немного поможет.

licensed under cc by-sa 3.0 with attribution.