Skip to main content
Topic: Add Record Not Work (Read 1874 times) previous topic - next topic

Add Record Not Work

Dear All,

Somebody can help me for Add Record problem, the Submit Button not change / do insert action to database. I was created Roles and Permission table in this project, but in the current project, the conditions for authectification has disable.

Thanks

Code: [Select]
<?php 
/**
 * Reg_enduro Page Controller
 * @category  Controller
 */
class Reg_enduroController extends BaseController{
function __construct(){
parent::__construct();
$this->tablename = "reg_enduro";
}
/**
     * List page records
     * @param $fieldname (filter record by a field)
     * @param $fieldvalue (filter field value)
     * @return BaseView
     */
function index($fieldname = null , $fieldvalue = null){
$request = $this->request;
$db = $this->GetModel();
$tablename = $this->tablename;
$fields = array("ID",
"REG_ID",
"PHOTO",
"RIDER_NAME",
"START_NO",
"AGE",
"TEAM",
"MOTOR",
"CLASS_RACE_CD");
$pagination = $this->get_pagination(MAX_RECORD_COUNT); // get current pagination e.g array(page_number, page_limit)
//search table record
if(!empty($request->search)){
$text = trim($request->search);
$search_condition = "(
reg_enduro.REG_ID LIKE ? OR
reg_enduro.RIDER_NAME LIKE ? OR
reg_enduro.TEAM LIKE ? OR
reg_enduro.MOTOR LIKE ? OR
reg_enduro.CLASS_RACE LIKE ? OR
reg_enduro.EMAIL LIKE ?
)";
$search_params = array(
"%$text%","%$text%","%$text%","%$text%","%$text%","%$text%"
);
//setting search conditions
$db->where($search_condition, $search_params);
//template to use when ajax search
$this->view->search_template = "reg_enduro/search.php";
}
if(!empty($request->orderby)){
$orderby = $request->orderby;
$ordertype = (!empty($request->ordertype) ? $request->ordertype : ORDER_TYPE);
$db->orderBy($orderby, $ordertype);
}
else{
$db->orderBy("reg_enduro.ID", ORDER_TYPE);
}
$db->where("PAID_STATUS='Paid'");
if($fieldname){
$db->where($fieldname , $fieldvalue); //filter by a single field name
}
$tc = $db->withTotalCount();
$records = $db->get($tablename, $pagination, $fields);
$records_count = count($records);
$total_records = intval($tc->totalCount);
$page_limit = $pagination[1];
$total_pages = ceil($total_records / $page_limit);
$data = new stdClass;
$data->records = $records;
$data->record_count = $records_count;
$data->total_records = $total_records;
$data->total_page = $total_pages;
if($db->getLastError()){
$this->set_page_error();
}
$page_title = $this->view->page_title = "Reg Enduro";
$this->render_view("reg_enduro/list.php", $data); //render the full page
}
/**
     * View record detail
* @param $rec_id (select record by table primary key)
     * @param $value value (select record by value of field name(rec_id))
     * @return BaseView
     */
function view($rec_id = null, $value = null){
$request = $this->request;
$db = $this->GetModel();
$rec_id = $this->rec_id = urldecode($rec_id);
$tablename = $this->tablename;
$fields = array("ID",
"REG_ID",
"RIDER_NAME",
"START_NO",
"TEAM",
"PHONE",
"AGE",
"MOTOR",
"CLASS_RACE",
"CLASS_RACE_CD",
"PHOTO",
"EMAIL");
if($value){
$db->where($rec_id, urldecode($value)); //select record based on field name
}
else{
$db->where("reg_enduro.ID", $rec_id);; //select record based on primary key
}
$record = $db->getOne($tablename, $fields );
if($record){
$page_title = $this->view->page_title = "View  Reg Enduro";
$this->view->report_filename = date('Y-m-d') . '-' . $page_title;
$this->view->report_title = $page_title;
$this->view->report_layout = "report_layout.php";
$this->view->report_paper_size = "A4";
$this->view->report_orientation = "portrait";
}
else{
if($db->getLastError()){
$this->set_page_error();
}
else{
$this->set_page_error("No record found");
}
}
return $this->render_view("reg_enduro/view.php", $record);
}
/**
     * Insert new record to the database table
* @param $formdata array() from $_POST
     * @return BaseView
     */
function add($formdata = null){
if($formdata){
$db = $this->GetModel();
$tablename = $this->tablename;
$request = $this->request;
//fillable fields
$fields = $this->fields = array("EMAIL","RIDER_NAME","START_NO","TEAM","PHONE","AGE","MOTOR","CLASS_RACE_CD","CLASS_RACE","REGIST_AMOUNT","PHOTO");
$postdata = $this->format_request_data($formdata);
$this->validate_captcha = true; //will check for captcha validation
$modeldata = $this->modeldata = $postdata;
//Check if Duplicate Record Already Exit In The Database
$db->where("RIDER_NAME", $modeldata['RIDER_NAME']);
if($db->has($tablename)){
$this->view->page_error[] = $modeldata['RIDER_NAME']." Already exist!";
}
if($this->validated()){
$rec_id = $this->rec_id = $db->insert($tablename, $modeldata);
if($rec_id){
$this->set_flash_msg("PENDAFTARAN SUKSES", "success");
return $this->redirect("reg_enduro");
}
else{
$this->set_page_error();
}
}
}
$page_title = $this->view->page_title = "DAFTAR ENDURO";
$this->render_view("reg_enduro/add.php");
}
/**
     * Update table record with formdata
* @param $rec_id (select record by table primary key)
* @param $formdata array() from $_POST
     * @return array
     */
function edit($rec_id = null, $formdata = null){
$request = $this->request;
$db = $this->GetModel();
$this->rec_id = $rec_id;
$tablename = $this->tablename;
//editable fields
$fields = $this->fields = array("ID","EMAIL","RIDER_NAME","START_NO","TEAM","PHONE","AGE","MOTOR","CLASS_RACE_CD","CLASS_RACE","REGIST_AMOUNT","PHOTO");
if($formdata){
$postdata = $this->format_request_data($formdata);
$this->rules_array = array(
'EMAIL' => 'required|valid_email',
'RIDER_NAME' => 'required',
'START_NO' => 'required|numeric',
'TEAM' => 'required',
'PHONE' => 'required',
'AGE' => 'required|numeric',
'MOTOR' => 'required',
'CLASS_RACE_CD' => 'required',
'REGIST_AMOUNT' => 'required',
'PHOTO' => 'required',
);
$this->sanitize_array = array(
'EMAIL' => 'sanitize_string',
'RIDER_NAME' => 'sanitize_string',
'START_NO' => 'sanitize_string',
'TEAM' => 'sanitize_string',
'PHONE' => 'sanitize_string',
'AGE' => 'sanitize_string',
'MOTOR' => 'sanitize_string',
'CLASS_RACE_CD' => 'sanitize_string',
'CLASS_RACE' => 'sanitize_string',
'REGIST_AMOUNT' => 'sanitize_string',
'PHOTO' => 'sanitize_string',
);
$modeldata = $this->modeldata = $this->validate_form($postdata);
//Check if Duplicate Record Already Exit In The Database
if(isset($modeldata['RIDER_NAME'])){
$db->where("RIDER_NAME", $modeldata['RIDER_NAME'])->where("ID", $rec_id, "!=");
if($db->has($tablename)){
$this->view->page_error[] = $modeldata['RIDER_NAME']." Already exist!";
}
}
if($this->validated()){
$db->where("reg_enduro.ID", $rec_id);;
$bool = $db->update($tablename, $modeldata);
$numRows = $db->getRowCount(); //number of affected rows. 0 = no record field updated
if($bool && $numRows){
$this->set_flash_msg("Record updated successfully", "success");
return $this->redirect("reg_enduro");
}
else{
if($db->getLastError()){
$this->set_page_error();
}
elseif(!$numRows){
//not an error, but no record was updated
$page_error = "No record updated";
$this->set_page_error($page_error);
$this->set_flash_msg($page_error, "warning");
return $this->redirect("reg_enduro");
}
}
}
}
$db->where("reg_enduro.ID", $rec_id);;
$data = $db->getOne($tablename, $fields);
$page_title = $this->view->page_title = "Edit  Reg Enduro";
if(!$data){
$this->set_page_error();
}
return $this->render_view("reg_enduro/edit.php", $data);
}
/**
     * Update single field
* @param $rec_id (select record by table primary key)
* @param $formdata array() from $_POST
     * @return array
     */
function editfield($rec_id = null, $formdata = null){
$db = $this->GetModel();
$this->rec_id = $rec_id;
$tablename = $this->tablename;
//editable fields
$fields = $this->fields = array("ID","EMAIL","RIDER_NAME","START_NO","TEAM","PHONE","AGE","MOTOR","CLASS_RACE_CD","CLASS_RACE","REGIST_AMOUNT","PHOTO");
$page_error = null;
if($formdata){
$postdata = array();
$fieldname = $formdata['name'];
$fieldvalue = $formdata['value'];
$postdata[$fieldname] = $fieldvalue;
$postdata = $this->format_request_data($postdata);
$this->rules_array = array(
'EMAIL' => 'required|valid_email',
'RIDER_NAME' => 'required',
'START_NO' => 'required|numeric',
'TEAM' => 'required',
'PHONE' => 'required',
'AGE' => 'required|numeric',
'MOTOR' => 'required',
'CLASS_RACE_CD' => 'required',
'REGIST_AMOUNT' => 'required',
'PHOTO' => 'required',
);
$this->sanitize_array = array(
'EMAIL' => 'sanitize_string',
'RIDER_NAME' => 'sanitize_string',
'START_NO' => 'sanitize_string',
'TEAM' => 'sanitize_string',
'PHONE' => 'sanitize_string',
'AGE' => 'sanitize_string',
'MOTOR' => 'sanitize_string',
'CLASS_RACE_CD' => 'sanitize_string',
'CLASS_RACE' => 'sanitize_string',
'REGIST_AMOUNT' => 'sanitize_string',
'PHOTO' => 'sanitize_string',
);
$this->filter_rules = true; //filter validation rules by excluding fields not in the formdata
$modeldata = $this->modeldata = $this->validate_form($postdata);
//Check if Duplicate Record Already Exit In The Database
if(isset($modeldata['RIDER_NAME'])){
$db->where("RIDER_NAME", $modeldata['RIDER_NAME'])->where("ID", $rec_id, "!=");
if($db->has($tablename)){
$this->view->page_error[] = $modeldata['RIDER_NAME']." Already exist!";
}
}
if($this->validated()){
$db->where("reg_enduro.ID", $rec_id);;
$bool = $db->update($tablename, $modeldata);
$numRows = $db->getRowCount();
if($bool && $numRows){
return render_json(
array(
'num_rows' =>$numRows,
'rec_id' =>$rec_id,
)
);
}
else{
if($db->getLastError()){
$page_error = $db->getLastError();
}
elseif(!$numRows){
$page_error = "No record updated";
}
render_error($page_error);
}
}
else{
render_error($this->view->page_error);
}
}
return null;
}
/**
     * Delete record from the database
* Support multi delete by separating record id by comma.
     * @return BaseView
     */
function delete($rec_id = null){
Csrf::cross_check();
$request = $this->request;
$db = $this->GetModel();
$tablename = $this->tablename;
$this->rec_id = $rec_id;
//form multiple delete, split record id separated by comma into array
$arr_rec_id = array_map('trim', explode(",", $rec_id));
$db->where("reg_enduro.ID", $arr_rec_id, "in");
$bool = $db->delete($tablename);
if($bool){
$this->set_flash_msg("Record deleted successfully", "success");
}
elseif($db->getLastError()){
$page_error = $db->getLastError();
$this->set_flash_msg($page_error, "danger");
}
return $this->redirect("reg_enduro");
}
}
above is controller file