Postingan lainnya
input data form tidak bisa tersimpan ke dalam database namun bisa di submit
Tutorial ini saya dapatkan dari David Insider Forum yang mengunakan CI versi 2 namun saat ini saya mengunakan CI versi 3 sebelumnya saya sudah mencoba menginputkan data lewat codeigniter dan dapat di submit, namun data yang telah di input tersebut tidak masuk ke dalam database
//controller
<?php
class Store_item_colours extends MX_controller
{
function __construct() {
parent:: __construct();
}
function submit($update_id)
{
if(!is_numeric($update_id)) {
redirect('site_security/not_allowed');
}
$this->load->library('session');
$this->load->module('site_security');
$this->site_security->_make_sure_is_admin();
$update_id = $this->uri->segment(3);
$submit = $this->input->post('submit', TRUE);
$colour = trim($this->input->post('colour', TRUE));
if ($submit="Finished") {
redirect('store_items/create/'.$update_id);
} elseif ($submit=="ubmit") {
//attempt an insert
if ($colour!="") {
$data['item_id'] = $update_id;
$data['colour'] = $colour;
$this->_insert($data);
$flash_msg = "The new colour was successfully added.";
$value = '<div class="alert alert-success" role="alert">'.$flash_msg.'</div>;';
$this->session->set_flashdata('item', $value);
}
}
redirect('store_item_colours/update/'.$update_id);
}
function update($update_id)
{
if(!is_numeric($update_id)) {
redirect('site_security/not_allowed');
}
$this->load->library('session');
$this->load->module('site_security');
$this->site_security->_make_sure_is_admin();
$data['headline'] = "Update Item Colours";
$data['update_id'] = $update_id;
$data['flash'] = $this->session->flashdata('item');
$data['view_file'] = "update";
$this->load->module("templates");
$this->templates->admin($data);
}
function get($order_by)
{
$this->load->model('mdl_store_item_colours');
$query = $this->mdl_store_item_colours->get($order_by);
return $query;
}
//test
function get_with_limit($limit, $offset, $order_by)
{
$this->load->model('mdl_store_item_colours');
$query = $this->mdl_store_item_colours->get_with_limit($limit, $offset, $order_by);
return $query;
}
function get_where($id)
{
$this->load->model('mdl_store_item_colours');
$query = $this->mdl_store_item_colours->get_where($id);
return $query;
}
function get_where_custom($col, $value)
{
$this->load->model('mdl_store_item_colours');
$query = $this->mdl_store_item_colours->get_where_custom($col, $value);
return $query;
}
function _insert($data)
{
$this->load->model('mdl_store_item_colours');
$this->mdl_store_item_colours->_insert($data);
}
function _update($id, $data)
{
$this->load->model('mdl_store_item_colours');
$this->mdl_store_item_colours->_update($id, $data);
}
function _delete($id)
{
$this->load->model('mdl_store_item_colours');
$this->mdl_store_item_colours->delete($id);
}
function count_where($column, $value)
{
$this->load->model('mdl_store_item_colours');
$count = $this->mdl_store_item_colours->count_where($column, $value);
}
function get_max()
{
$this->load->model('mdl_store_item_colours');
$max_id = $this->mdl_store_item_colours->get_max();
return $max_id;
}
function _custom_query($mysql_query)
{
$this->load->model('mdl_store_item_colours');
$query = $this->mdl_store_item_colours->_custom_query($mysql_query);
return $query;
}
}
//Model
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Mdl_store_item_colours extends CI_Model {
function __construct() {
parent::__construct();
}
function get_table() {
$table = "store_item_colours";
return $table;
}
function get($order_by){
$table = $this->get_table();
$this->db->order_by($order_by);
$query = $this->db->get($table);
return $query;
}
function get_with_limit($limit, $offset, $order_by) {
$table = $this->get_table();
$this->db->limit($limit, $ffset);
$this->db->order_by($order_by);
$query = $this->db->get($table);
return $query;
}
function get_where($col, $value) {
$table = $this->get_table();
$this->db->where('id', $id);
$query = $this->db->get_where($table);
return $query;
}
function get_where_custom($col, $value) {
$table = $this->get_table();
$this->db->where($col, $value);
$query = $this->db->get_where_custom($table);
return $query;
}
function _insert($data){
$table = $this->get_table();
$this->db->insert($table, $data);
}
function _update($id, $data){
$table = $this->get_table();
$this->db->where('id', $id);
$this->db->update($table, $data);
}
function _delete($id){
$table = $this->get_table();
$this->db->where('id', $id);
$this->db->delete($table);
}
function count_where($column, $value){
$table = $this->get_table();
$this->db->where($column, $value);
$query = $this->db->get($table);
$num_rows = $query->num_rows();
return $num_rows;
}
function count_all(){
$table = $this->get_table();
$query = $this->db->get($table);
$num_rows = $query->num_rows();
return $num_rows;
}
function get_max(){
$table = $this->get_table();
$this->select_max('id');
$query = $this->db->get($table);
$row = $query->row();
$id = $row->id;
return $id;
}
function _custom_query($mysql_query){
$query = $this->db->query($mysql_query);
return $query;
}
function item_check($str)
{
$item_id = url_title($str);
$mysql_query = "select * from store_item_colours where item_id='$str' and item_id='$item_id'";
$update_id = $this->uri->segment(3);
if (is_numeric($update_id)) {
//this is an update
$mysql_query.=" and id!=$update_id";
}
$query = $this->_custom_query($mysql_query);
$num_rows = $query->num_rows();
if ($num_rows>0)
{
$this->form_validation->set_message('item_check', 'The item title that you submitted is not available');
return FALSE;
}
else
{
return TRUE;
}
}
}
// view //update.php
<h1><?= $headline ?><h1>
<?= validation_errors("<p style='color: red;font-size:12px;line-height:70%'>", "</p>") ?>
<?php
if (isset($flash)) {
echo $flash;
}
?>
<div class="row-fluid sortable">
<div class="box span12">
<div class="box-header" data-original-title>
<h2><i class="halflings-icon white edit"></i><span class="break"></span>New Colour Option</h2>
<div class="box-icon">
<a href="#" class="btn-minimize"><i class="halflings-icon white chevron-up"></i></a>
<a href="#" class="btn-close"><i class="halflings-icon white remove"></i></a>
</div>
</div>
<div class="box-content">
<?php
$form_location = base_url()."store_item_colours/submit/".$update_id;
?>
<form class="form-horizontal" method="post" action="<?= $form_location ?>">
<fieldset>
<div class="control-group">
<label class="control-label" for="typeahead">Item Title </label>
<div class="controls">
<input type="text" class="span6" name="colour">
</div>
</div>
<div class="form-actions">
<button type="submit" class="btn btn-primary" name="submit" value="Submit">Submit</button>
<button type="submit" class="btn" name="Finished" value="Finished">Finished</button>
</div>
</fieldset>
</form>
</div>
</div><!--/span-->
</div><!--/row-->
mengapa data yang telah di input bisa ter submit namun tidak tersimpan di database,memang ada data tabel data lain yang telah saya buat "table_items" yang menggunakan function form validation bisa ter submit dan bisa tersimpan di database Mohon pencerahannya mastah :)
2 Jawaban:
Mungkin di method Submit nya gan, itu setelah If == "Finished", dibawahnya elseif-nya, nilai pembandingnya elseif == "ubmit", kurang "S" mungkin gan, kalo ane liat sii di formnya button namenya "Submit" sama "Finished",
function submit($update_id)
{
if(!is_numeric($update_id)) {
redirect('site_security/not_allowed');
}
$this->load->library('session');
$this->load->module('site_security');
$this->site_security->_make_sure_is_admin();
$update_id = $this->uri->segment(3);
$submit = $this->input->post('submit', TRUE);
$colour = trim($this->input->post('colour', TRUE));
if ($submit="Finished") {
redirect('store_items/create/'.$update_id);
} elseif ($submit=="submit") {
//attempt an insert
if ($colour!="") {
$data['item_id'] = $update_id;
$data['colour'] = $colour;
$this->_insert($data);
$flash_msg = "The new colour was successfully added.";
$value = '<div class="alert alert-success" role="alert">'.$flash_msg.'</div>;';
$this->session->set_flashdata('item', $value);
}
}
redirect('store_item_colours/update/'.$update_id);
}
sudah aku tambah S / s kak di "ubmit"nya tetep gak bisa