I have an issue when I click on a link to edit a sale http://domain/admin/editsale/index/21/sale-name I seem to get a blank page loaded so I have a feeling that it is not getting the $id but I cannot spot my issue.
Controller:
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Editsale extends CI_Controller {
function __construct() {
parent::__construct();
}
function index($id) {
if(!$this->session->userdata('logged_in'))redirect('admin/home');
if($this->input->post('submit')) {
#Set The Validation Rules
$this->form_validation->set_rules('name', 'Name', 'trim|required');
$this->form_validation->set_rules('location', 'Location', 'trim|required');
$this->form_validation->set_rules('bedrooms', 'Bedrooms', 'trim|is_natural');
$this->form_validation->set_rules('bathrooms', 'Bathrooms', 'trim');
$this->form_validation->set_rules('condition', 'Condition', 'trim');
$this->form_validation->set_rules('description', 'Description', 'trim');
$this->form_validation->set_rules('price', 'Price', 'trim');
if($this->form_validation->run() == FALSE) {
#Set the $data for the view if FALSE
$data['cms_pages'] = $this->navigation_model->getCMSPages($id);
$data['sales_pages'] = $this->sales_model->getSalesPages($id);
$data['sale'] = $this->sales_model->getSalesContent($id);
$data['content'] = $this->load->view('admin/editsale', $data, TRUE); #Loads the "content"
$this->load->view('admintemplate', $data); #Loads the given template and passes the $data['content'] into it
}
#Form Validation Was Correct So Lets Continue
#Lets Set What We Are Sending To The DB
$content = array(
'name' => $this->input->post('name', TRUE),
'location' => $this->input->post('location', TRUE),
'bedrooms' => $this->input->post('bedrooms', TRUE),
'bathrooms' => $this->input->post('bathrooms', TRUE),
'condition' => $this->input->post('condition', TRUE),
'description' => $this->input->post('description', TRUE),
'price' => $this->input->post('price', TRUE)
);
if($this->sales_model->updateSale($id, $content)) {
$data['success'] = TRUE; #displays sale updated
$data['cms_pages'] = $this->navigation_model->getCMSPages($id);
$data['sales_pages'] = $this->sales_model->getSalesPages($id);
$data['sale'] = $this->sales_model->getSalesContent($id);
$data['content'] = $this->load->view('admin/editsale', $data, TRUE); #Loads the "content"
} // Sale Update End
}else{
$data['cms_pages'] = $this->navigation_model->getCMSPages($id);
$data['sales_pages'] = $this->sales_model->getSalesPages($id);
$data['sale'] = $this->sales_model->getSalesContent($id);
$data['content'] = $this->load->view('admin/editsale', $data, TRUE); #Loads the "content"
}#Submit End
} #Index End
}
Model:
function getSalesPages($id = NULL) {
$query = $this->db->get('sales');
if($query->num_rows() > 0) return $query->result();
}
function getSalesContent($id = NULL) {
$this->db->where('id', $id);
$query = $this->db->get('sales', 1);
if($query->num_rows() > 0) {
$row = $query->result_array();
return $row;
}else{
return FALSE;
}
}
View:
<?php
//Setting form attributes
$formEditSale = array('id' => 'editSale', 'name' => 'editSale');
$formName = array('id' => 'name', 'name' => 'name');
$formLocation = array('id' => 'location', 'name' => 'location');
$formBedrooms = array('id' => 'bedrooms','name' => 'bedrooms');
$formBathrooms = array('id' => 'bathrooms','name' => 'bathrooms');
$formCondition = array('id' => 'condition','name' => 'condition');
$formDescription = array('id' => 'description','name' => 'description');
$formPrice = array('id' => 'price','name' => 'price');
if($success == TRUE) {
echo '<section id = "validation">Sale Updated</section>';
}
?>
?>
<section id = "validation"><?php echo validation_errors();?></section>
<?php
echo form_open_multipart('admin/editsale/index/'.$sale[0]['id'].'/'.url_title($sale[0]['name'],'dash', TRUE),$formEditSale);
echo form_fieldset();
echo form_label('Name:', 'name');
echo form_input($formName, $sale[0]['name']);
echo form_label ('Location', 'location');
echo form_input($formLocation, $sale[0]['location']);
echo form_label ('Bedrooms', 'bedrooms');
echo form_input($formBedrooms, $sale[0]['bedrooms']);
echo form_label ('Bathrooms', 'bathrooms');
echo form_input($formBathrooms, $sale[0]['bathrooms']);
echo form_label ('Condition', 'condition');
echo form_input($formCondition, $sale[0]['condition']);
echo form_label ('Price', 'price');
echo form_input($formPrice, $sale[0]['sale']);
echo form_label ('Description', 'description');
echo form_textarea($formDescription, $sale[0]['description']);
echo form_submit('submit','Submit');
echo form_fieldset_close();
echo form_close();
Fixed, I was loading the view before the data
Related
I want to change the database, but my coding deletes all its contents.
I use codeigneter 3
my controller:
public function update_() {
$id = $this->input->post('id');
$nama = $this->input->post('nama');
$alamat = $this->input->post('alamat');
$number = $this->input->post('number');
$rekening = $this->input->post('rekening');
$email = $this->input->post('email');
$data = array(
'nama' => $nama,
'alamat' => $alamat,
'number' => $number,
'rekening' => $rekening,
'email' => $email,
);
$where = array (
'id' => $id
);
$this->Modellaptop->update_client($where, $data, 'tb_invoice ');
$this->session->set_flashdata('message',
'<div class="alert alert-success" role="alert">Update success </div>'
);
redirect('Admin/invoice');
}
my model:
public function update_client($where,$data,$table)
{
$this->db->where($where);
$this->db->update($table, $data);
}
maybe you can try like this
in controller
public function update_() {
$id = $this->input->post('id');
$nama = $this->input->post('nama');
$alamat = $this->input->post('alamat');
$number = $this->input->post('number');
$rekening = $this->input->post('rekening');
$email = $this->input->post('email');
$data = [
'nama' => $nama,
'alamat' => $alamat,
'number' => $number,
'rekening' => $rekening,
'email' => $email
];
$where = array (
'id' => $id
);
$this->Modellaptop->update_client($where, $data);
$this->session->set_flashdata('message',
'<div class="alert alert-success" role="alert">Update success </div>'
);
redirect('Admin/invoice');
}
in model
public function update_client($where,$data = [])
{
$this->db->where($where);
$this->db->update('tb_invoice(your table name)', $data);
//dont forget to return
return $this->db->affected_rows();
}
i want to access unit_id from get_material() method which return data from the modal product_modal.. The function codes..
public function get_material() {
$query = $this->db->get('tbl_receivings_items');
return $query->result();
}
The second function which I want to use unit_id from the above function is
public function get_material_item_unit($p_unit_id){
$sql = "SELECT * FROM tbl_unit WHERE unit_id = ?";
$q = $this->db->query($sql, $p_unit_id);
if($q->num_rows() >0){
return $q->result();
}else{
return null;
}
}
I access both functions from the controller known as manufacture **, in which both functions have been called inside **add_product() function as follows
public function add_product(){
$formRules = array(
array(
'field' => 'prod_name',
'label' => 'Product Name',
'rules' => 'trim|required',
),array(
'field' => 'partner',
'label' => '',
'rules' => 'trim',
),array(
'field' => 'prod_qty',
'label' => 'Quantity To Produce',
'rules' => 'trim|required|greater_than_equal_to[1]',
)
);
$this->form_validation->set_rules($formRules);
if($this->form_validation->run() == TRUE){
$can_be_consumed_value = $this->input->post('can_be_consumed');
$can_be_solded_value = $this->input->post('can_be_sold');
if($can_be_consumed_value == false){
$can_be_consumed_value ="0";
}else{
$can_be_consumed_value ="1";
}
if($can_be_solded_value == false){
$can_be_solded_value ="0";
}else{
$can_be_solded_value ="1";
}
$data =array(
'prod_name' => $this->input->post('prod_name'),
'can_be_consumed' => $can_be_consumed_value,
'can_be_sold' => $can_be_solded_value,
'prod_partner' => $this->input->post('partner'),
'created_by' => $this->input->post('created_by'),
'date_created' => date('Y-m-d'),
'prod_desc' => $this->input->post('prod_desc'),
'prod_qty' =>$this->input->post('prod_qty')
);
$query = $this->product_model->add_product($data);
if($query){
$data['initial_data'] = $this->product_model->get_product_data($query);
$data['material_list'] = $this->product_model->get_material();
$m_list = $this->product_model->get_material();
if($m_list->num_rows() > 0)
{
foreach($m_list->result() as $m){
$data['unit_list'] = $this->product_model->get_material_item_unit($m->unit_id);
}
}
$this->session->set_flashdata('success_msg', 'Product Created Successfully');
$this->load->view('manufacture/manufacture_step_2', $data,'refresh');
}else{
$this->session->set_flashdata('error_msg', 'Sorry! Fail to create product');
redirect('manufacture');
}
}else{
echo '<div class="alert alert-danger error">'.validation_errors().'</div>';
}
}
The add_product() function works properly means that it send data to db.
My Problem: every time I try to access the unit_id through a while loop I get an error of Undefined property: stdClass::$unit_id. i will appreciate your help.
the while loop
$data['initial_data'] = $this->product_model->get_product_data($query);
$data['material_list'] = $this->product_model->get_material();
$m_list = $this->product_model->get_material();
if($m_list->num_rows() > 0)
{
foreach($m_list->result() as $m){
$data['unit_list'] = $this->product_model->get_material_item_unit($m->unit_id);
}
}
I don't know what is the SQL extension that you are using in PHP but normally when you get the results of a query these results are an array of values.
You are trying to get the data like an object and this is the reason that PHP shows you this error because the var $m is not an object.
You can try to do a var_dump($m) and you can see what is the type and the structure of the variable. If $m is an array you must use $m['columnName'] to get the value.
I am trying to insert an element into my 'category' table and use the inserted element's id to insert another element into my 'subcategory' table.
This is my code in my controller
public function insertCategory(){
$category = $this->input->post('category');
$subcategory = $this->input->post('subcategory');
$this->form_validation->set_rules('category', 'Category', 'required');
$this->form_validation->set_rules('subcategory', 'Subcategory', 'required');
if($this->form_validation->run() == FALSE){
$this->load->view('ADMIN_ADDCategory');
}
else{
$category_id = $this->admin_model->insertCategory($category);
$this->admin_model->insertSubcategory($category_id, $subcategory);
}
}
And this is my code in my model
function insertCategory($category){
$data = array(
'category' => $category,
'status' => 1
);
$this->db->insert('category', $data);
$this->db->select('id');
$this->db->from('category');
$this->db->where('category', $category);
$this->db->limit(1);
$query = $this->db->get();
return $query->result();
}
function insertSubcategory($category_id, $subcategory){
$data = array(
'category_id' => $category_id,
'subcategory' => $subcategory,
'status' => 1
);
$this->db->insert('subcategory', $data);
}
However I am getting this error
I already tried using $category_id = (array) $this->admin_model->insertCategory($category); but it still doesn't work
How do I overcome this error? Thank you for the help.
$this->admin_model->insertSubcategory($category_id, $subcategory);
$category_id is an array , so , you must do it like this:
$this->admin_model->insertSubcategory($category_id[0]->field, $subcategory);
You should try this
$this->admin_model->insertSubcategory($category_id[0]->id;, $subcategory);
try this code
Model
public function insert($table, $data)
{
if($this->db->insert($table,array $data))
{
return $this->db->insert_id();
}else
return false
}
controller
public function insertCategory(){
$this->form_validation->set_rules('category', 'Category', 'required');
$this->form_validation->set_rules('subcategory', 'Subcategory', 'required');
if($this->form_validation->run() == FALSE){
$this->load->view('ADMIN_ADDCategory');
}
else{
$category = $this->input->post('category');
$subcategory = $this->input->post('subcategory');
//data must be an array
$data = [
'category' => $category,
'status' => '1'
];
$category_id = $this->admin_model->insert('tablename',$data)
if($category_id != false)
{
$data = [
'category_id' => $category_id,
'subcategory' => $subcategory,
'status' => 1
];
if($this->admin_model->insert('table name', $data) != false)
{
echo 'success';
//or load your success page
}else{
echo 'failed';
//or load your success page
}
}else{
echo 'failed';
//or load your success page
}
}
}
I got trouble inputing the radio button value to database, when i choose "submit" it won't add into database. This is the form view:
<?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(); ?>
This is the controller (tambah is "insert" function to database)
<?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);
}
}
This is the model:
<?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' => '|< 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;
}
}
I got no trouble in the formfield. The trouble is the radio button "lampu"
The best thing to do, I think, is to check where it's going wrong. I usually do this, in this case, by checking if the value is being passed back to the controller and model. This way you understand better what's going on inside your code. Do something like this:
In the model:
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;
}
}
I hope this helps a bit....
I am using Ionauth library in codeigniter and edited edit_user() method in Auth controller to enable individual user updating his/her own user settings. So when a logged in user goes to : siteurl/auth/edit_user it shows the user settings just fine. But when I hit the save button I got an error: "This form post did not pass our security checks". Though the default url (siteurl/auth/edit_user/userID) works fine, For individual non-admin user I want to keep the url without userID at the end.
here is my edit_user() method:
//edit a user
function edit_user($id=NULL)
{
$this->data['title'] = "Edit User";
if (!$this->ion_auth->logged_in() || (!$this->ion_auth->is_admin() && !($this->ion_auth->user()->row()->id == $id) && !($id==NULL )))
//if (!$this->ionauth->logged_in() || !$this->ion_auth->is_admin())
{
redirect('auth', 'refresh');
}
if($id==NULL){
$user = $this->ion_auth->user()->row();
}else{
$user = $this->ion_auth->user($id)->row();
}
$groups=$this->ion_auth->groups()->result_array();
$currentGroups = $this->ion_auth->get_users_groups($id)->result();
//process the phone number
/**if (isset($user->phone) && !empty($user->phone))
{
$user->phone = explode('-', $user->phone);
} **/
//validate form input
$this->form_validation->set_rules('first_name', $this->lang->line('edit_user_validation_fname_label'), 'required|xss_clean');
$this->form_validation->set_rules('last_name', $this->lang->line('edit_user_validation_lname_label'), 'required|xss_clean');
if(!($this->input->post('email')==$user->email)){
$this->form_validation->set_rules('email', $this->lang->line('edit_user_validation_email_label'), 'required|valid_email|is_unique[users.email]');
}else{
$this->form_validation->set_rules('email', $this->lang->line('edit_user_validation_email_label'), 'required|valid_email');
}
/** $this->form_validation->set_rules('phone2', $this->lang->line('edit_user_validation_phone2_label'), 'required|xss_clean|min_length[3]|max_length[3]');
$this->form_validation->set_rules('phone3', $this->lang->line('edit_user_validation_phone3_label'), 'required|xss_clean|min_length[4]|max_length[4]');
$this->form_validation->set_rules('company', $this->lang->line('edit_user_validation_company_label'), 'required|xss_clean'); **/
$this->form_validation->set_rules('groups', $this->lang->line('edit_user_validation_groups_label'), 'xss_clean');
//$this->form_validation->set_message('is_unique[users.email]','Email already exists or Invalid');
if (isset($_POST) && !empty($_POST))
{
// do we have a valid request?
if ($this->_valid_csrf_nonce() === FALSE || $id != $this->input->post('id'))
{
show_error($this->lang->line('error_csrf'));
}
$data = array(
'first_name' => $this->input->post('first_name'),
'last_name' => $this->input->post('last_name'),
'email' => $this->input->post('email'),
/** 'phone' => $this->input->post('phone1') . '-' . $this->input->post('phone2') . '-' . $this->input->post('phone3'), **/
);
//if($this->ion_auth->is_admin()){
//Update the groups user belongs to
$groupData = $this->input->post('groups');
if (isset($groupData) && !empty($groupData)) {
$this->ion_auth->remove_from_group('', $id);
foreach ($groupData as $grp) {
$this->ion_auth->add_to_group($grp, $id);
}
}
//}
//update the password if it was posted
if ($this->input->post('password'))
{
$this->form_validation->set_rules('password', $this->lang->line('edit_user_validation_password_label'), 'required|min_length[' . $this->config->item('min_password_length', 'ion_auth') . ']|max_length[' . $this->config->item('max_password_length', 'ion_auth') . ']|matches[password_confirm]');
$this->form_validation->set_rules('password_confirm', $this->lang->line('edit_user_validation_password_confirm_label'), 'required');
$data['password'] = $this->input->post('password');
}
if ($this->form_validation->run() === TRUE)
{
$this->ion_auth->update($user->id, $data);
//check to see if we are creating the user
//redirect them back to the admin page
$this->session->set_flashdata('message', "User Saved");
redirect("auth", 'refresh');
}
}
//display the edit user form
$this->data['csrf'] = $this->_get_csrf_nonce();
//set the flash data error message if there is one
$this->data['message'] = (validation_errors() ? validation_errors() : ($this->ion_auth->errors() ? $this->ion_auth->errors() : $this->session->flashdata('message')));
//pass the user to the view
$this->data['user'] = $user;
//if($this->ion_auth->is_admin()){
$this->data['groups'] = $groups;
$this->data['currentGroups'] = $currentGroups;
//}
$this->data['first_name'] = array(
'name' => 'first_name',
'id' => 'first_name',
'type' => 'text',
'value' => $this->form_validation->set_value('first_name', $user->first_name),
);
$this->data['last_name'] = array(
'name' => 'last_name',
'id' => 'last_name',
'type' => 'text',
'value' => $this->form_validation->set_value('last_name', $user->last_name),
);
$this->data['email'] = array(
'name' => 'email',
'id' => 'email',
'type' => 'text',
'value' => $this->form_validation->set_value('email', $user->email),
);
/** $this->data['phone1'] = array(
'name' => 'phone1',
'id' => 'phone1',
'type' => 'text',
'value' => $this->form_validation->set_value('phone1', $user->phone[0]),
);
$this->data['phone2'] = array(
'name' => 'phone2',
'id' => 'phone2',
'type' => 'text',
'value' => $this->form_validation->set_value('phone2', $user->phone[1]),
);
$this->data['phone3'] = array(
'name' => 'phone3',
'id' => 'phone3',
'type' => 'text',
'value' => $this->form_validation->set_value('phone3', $user->phone[2]),
); **/
$this->data['password'] = array(
'name' => 'password',
'id' => 'password',
'type' => 'password'
);
$this->data['password_confirm'] = array(
'name' => 'password_confirm',
'id' => 'password_confirm',
'type' => 'password'
);
$this->load->view('header');
$this->_render_page('auth/edit_user', $this->data);
$this->load->view('footer');
}
and this is my view file (edit_user.php):
<h1><?php echo lang('edit_user_heading');?></h1>
<div id="body">
<p><?php echo lang('edit_user_subheading');?></p>
<div id="infoMessage"><?php echo $message;?></div>
<?php echo form_open(uri_string());?>
<p>
<?php echo lang('edit_user_fname_label', 'first_name');?> <br />
<?php echo form_input($first_name);?>
</p>
<p>
<?php echo lang('edit_user_lname_label', 'last_name');?> <br />
<?php echo form_input($last_name);?>
</p>
<p>
<?php echo lang('edit_user_email_label', 'email');?> <br />
<?php echo form_input($email);?>
</p>
<!--
<p>
<?php echo lang('edit_user_phone_label', 'phone');?> <br />
<?php echo form_input($phone1);?>-<?php echo form_input($phone2);?>-<?php echo form_input($phone3);?>
</p>
-->
<p>
<?php echo lang('edit_user_password_label', 'password');?> <br />
<?php echo form_input($password);?>
</p>
<p>
<?php echo lang('edit_user_password_confirm_label', 'password_confirm');?><br />
<?php echo form_input($password_confirm);?>
</p>
<?php //if($this->ion_auth->is_admin()){ ?>
<h3><?php echo lang('edit_user_groups_heading');?></h3>
<?php foreach ($groups as $group):?>
<label class="checkbox">
<?php
$gID=$group['id'];
$checked = null;
$item = null;
foreach($currentGroups as $grp) {
if ($gID == $grp->id) {
$checked= ' checked="checked"';
break;
}
}
?>
<input type="checkbox" name="groups[]" value="<?php echo $group['id'];?>"<?php echo $checked;?>>
<?php echo $group['name'];?>
</label>
<?php endforeach?>
<?php //} ?>
<?php echo form_hidden('id', $user->id);?>
<?php echo form_hidden($csrf); ?>
<p><?php echo form_submit('submit', lang('edit_user_submit_btn'));?></p>
<?php echo form_close();?>
The csrf check is failing.
Try taking out $id = NULL in the method declaration (you don't need it anyway if you're sending the id via POST). Or explicitly set $id = $this->input->post('id'); before doing the csrf check.
In my case I was using relative URLs for the images and css files used in the site. Using base_url() to all the URLs present in the site fixed the problem. No issue now.