I have some problem with CodeIgniter, I can't update data in a record in CodeIgniter. 'id_user' is the primary key of my user table. I have tried to solve this problem but I couldn't. I hope someone can help with this problem. Thanks a lot :)
I have posted the code below :
HTML5 code :
<form action="<?php echo base_url(). 'crud/update'; ?>" method="post">
<input name="id_user" type="text" class="form-control" value="<?php echo $id_usernya ?>">
<div class="form-group">
<label>Alamat :</label>
<textarea name="alamat_user" class="form-control" rows="5" placeholder="Alamat" ><?php echo $usernya->alamat_user ?></textarea>
</div>
<div class="form-group">
<input name="kodepos_user" type="text" class="form-control" value="<?php echo $usernya->kodepos_user ?>">
</div>
<div class="form-group">
<input type="hidden" name="provinsi_user">
<select name="provinsi_id_user" class="form-control">
<option value="">Pilih Provinsi</option>
<option selected value="<?php echo $usernya->provinsi_user ?>"><?php echo $usernya->provinsi_user ?></option>
<?php echo $provinsi ?>
</select>
</div>
<div class="form-group">
<input type="hidden" name="kota_user">
<select name="kota_id_user" class="form-control">
<option value="">Pilih Kota</option>
<option selected value="<?php echo $usernya->kota_user ?>"><?php echo $usernya->kota_user ?></option>
</select>
</div>
<input type="submit" class="btn btn-primary" value="Simpan">
</form>
Controller :
class Crud extends CI_Controller{
function __construct(){
parent::__construct();
$this->load->model('m_data');
$this->load->helper('url');
}
function edit($id_user){
$where = array('id_user' => $id_user);
$data['user'] = $this->m_data->edit_data($where,'user')->result();
$this->load->view('back/user/v_beranda',$data);
redirect('beranda/index');
}
function update(){
$id_user = $this->input->post('id_user');
$alamat_user = $this->input->post('alamat_user');
$kodepos_user = $this->input->post('kodepos_user');
$provinsi_id_user = $this->input->post('provinsi_id_user');
$kota_id_user = $this->input->post('kota_id_user');
$data = array(
'alamat_user' => $alamat_user,
'kodepos_user' => $kodepos_user,
'provinsi_id_user' => $provinsi_id_user,
'kota_id_user' => $kota_id_user
);
$where = array(
'id_user' => $id_user
);
$this->m_data->update_data($where,$data,'user');
redirect('beranda/index');
}
Model code :
<?php
class M_data extends CI_Model{
function edit_data($where,$table){
return $this->db->get_where($table,$where);
}
function update_data($where,$data,$table){
$this->db->where($where);
$this->db->update($table,$data);
}
}
Initialize your Model like what your have done on your Controller
class M_data extends CI_Model{
public function __construct() {
parent::__construct();
$this->load->database();
}
function edit_data($where,$table){
return $this->db->get_where($table,$where);
}
function update_data($where,$data,$table){
$this->db->where($where);
$this->db->update($table,$data);
}
}
try to print query to check what exact query getting executed
$this->db->last_query():
and which error its shows when you execute above query
Related
It shows me this error: Column 'zeme_idZeme' cannot be null
I thought <select name="zeme_IdZeme"> this would be sufficient, but I was wrong about it.
View:
<body>
<div class="main-block">
<h1>Film</h1>
<form method="post" action="<?php echo base_url('/form') ?>">
<div class="info">
<input type="text" name="cesky_nazev" required="vyžadováno" placeholder="Český název filmu">
<input type="text" name="originalni_nazev" required="vyžadováno" placeholder="Původní název filmu">
<input type="number" name="delka_filmu" required="vyžadováno" placeholder="Délka filmu">
<input type="text" name="typ_filmu" required="vyžadováno" placeholder="Typ filmu">
<select name="zeme_IdZeme">
<option disabled selected>Země</option>
<?php
$query = $db->query("SELECT * FROM zeme");
foreach ($query->getResult() as $row)
{ ?>
<option value="5"> <?php echo $row->nazev;}?></option>
</select>
</div>
<button type="submit" class="button">Odeslat</button>
</form>
</div>
</body>
Controller:
public function form() {
$data = [ 'cesky_nazev' =>$this->request->getVar('cesky_nazev'),
'originalni_nazev' =>$this->request->getVar('originalni_nazev'),
'delka_filmu' =>$this->request->getVar('delka_filmu'),
'typ_filmu' =>$this->request->getVar('typ_filmu'),
'zeme_idZeme' =>$this->request->getVar('zeme_idZeme'),
'zanrFilmu_idZanrFilmu' =>$this->request->getVar('zanrFilmu_idZanrFilmu'),
'promitani_idPromitani' =>$this->request->getVar('promitani_idPromitani'),
'jazyky_idJazyky' =>$this->request->getVar('jazyky_idJazyky') ];
/*
$db = \Config \Database::connect();
$builder = $db->table('film');
$builder->insert($data); */
$model = new Film_formular();
if ($model->insert($data))
{
echo view('templates/header');
?><style>.center {text-align: center;color: red;}</style><?php
echo "<h3 class='center'>Úspěšně přidáno</h3>";
echo view('film_formular');
echo view('templates/footer');
}
else
{
echo "nepřidáno";
}
}
model:
<?php namespace App\Models;
use CodeIgniter\Model;
class Film_formular extends Model
{
protected $table = 'film';
protected $allowedFields = ['cesky_nazev', 'originalni_nazev', 'delka_filmu', 'typ_filmu', 'zeme_idZeme', 'zanrFilmu_idZanrFilmu','promitani_idPromitani','jazyky_idJazyky'];
}
The option close tag aren't inside foreach. Move the bracket after option close tag
<select name="zeme_IdZeme">
<option disabled selected>Země</option>
<?php
$query = $db->query("SELECT * FROM zeme");
foreach ($query->getResult() as $row)
{
?>
<option value="5"> <?php echo $row->nazev;?></option>
<?php
}
?> </select>
I'm new with codeigniter and to the point I don't understand about this one. It's work in another view (form add) while this one (form edit) didn't working.
and there isn't any notification of an error.
my case is i would like to show dropdown menu from database in form edit like in the form add.
this is my code (the working one for form add):
controllers Admin.php :
for add data
class Admin extends CI_Controller {
public function tambah_data(){
$this->load->helper('form');
//kategori
$this->load->model('models_kategori_barang', 'mkb');
$datakb = $this->mkb->GetKategoriBarang();
//model
$this->load->model('models_model_barang', 'modb');
$datamdb = $this->modb->GetModelBarang();
//material
$this->load->model('models_material_barang', 'matb');
$datamatb = $this->matb->GetMaterialBarang();
//merk
$this->load->model('models_merk_barang', 'merb');
$datamerb = $this->merb->GetMerkBarang();
$this->load->view('templates/admin/tambah_data',array(
'datakb' => $datakb,
'datamdb' => $datamdb,
'datamatb' => $datamatb,
'datamerb' => $datamerb));
}
}
view tambah_data.php
<?php echo form_open_multipart('crud_barang/do_insert'); ?>
<label>Kode Barang<br>
<input type="text" autofocus placeholder="Kode" name="kode_barang" autofocus required></label>
<label>Nama<br>
<input type="text" placeholder="Nama" name="nama_barang" required></label>
<label>Kategori<br>
<select name="kategori_barang" required>
<option disabled selected>Pilih Katergori...</option>
<?php foreach ($datakb as $dkb) { ?>
<option><?php echo $dkb['nama_kategori_barang'];?></option>
<?php }?>
</select>
</label>
<label>Model<br>
<select name="model_barang" required>
<option disabled selected>Pilih Model...</option>
<?php foreach ($datamdb as $dmdb) { ?>
<option><?php echo $dmdb['nama_model_barang'];?></option>
<?php }?>
</select>
</label>
<label>Material<br>
<select name="material_barang" required>
<option disabled selected>Pilih Material...</option>
<?php foreach ($datamatb as $dmtb) { ?>
<option><?php echo $dmtb['nama_material_barang'];?></option>
<?php }?>
</select>
</label>
<label>Merk<br>
<select name="merk_barang" required>
<option disabled selected>Pilih Merk...</option>
<?php foreach ($datamerb as $dmb) { ?>
<option><?php echo $dmb['nama_merk_barang'];?></option>
<?php }?>
</select>
</label>
<label>Harga<br><input type="text" placeholder="Harga" name="harga_barang" required></label>
<label>Ukuran<br><input type="text" placeholder="Ukuran" name="size_barang" required></label>
<label>Keterangan<br>
<textarea placeholder="Keterangan" name="ket_barang"></textarea>
</label>
<p><input type="submit" value="Masukan" class="btn"></p>
<?php echo form_close(); ?>
model models_barang.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Models_barang extends CI_Model {
public function GetBarang($where=""){
$datab = $this->db->get('barang'.$where);
return $datab->result_array();
}
public function InsertData($tabelName, $datab){
$res = $this->db->insert($tabelName,$datab);
return $res;
}
public function UpdateData($tabelName,$datab,$where){
$res = $this->db->update($tabelName,$datab,$where);
return $res;
}
public function DeleteData($tabelName,$where){
$res = $this->db->delete($tabelName,$where);
return $res;
}
}
this one not work while i put it in form edit :
controller crud_barang.php
class crud_barang extends CI_Controller {
//funciton insert was here
public function edit_data($id_barang){
/*load model dahulu, karena bukan global model*/
$this->load->model('models_barang', 'mb');
$brg = $this->mb->GetBarang(" where id_barang = '$id_barang'");
$datab = array(
'id_barang' => $brg[0]['id_barang'],
'kode_barang' => $brg[0]['kode_barang'],
'nama_barang' => $brg[0]['nama_barang'],
'material_barang'=> $brg[0]['material_barang'],
'model_barang' => $brg[0]['model_barang'],
'kategori_barang' => $brg[0]['kategori_barang'],
'harga_barang' => $brg[0]['harga_barang'],
'size_barang' => $brg[0]['size_barang'],
'merk_barang' => $brg[0]['merk_barang'],
'ket_barang' => $brg[0]['ket_barang']
);
$this->load->view('templates/admin/form_edit_barang',$datab);
$this->load->model('models_kategori_barang', 'mkb');
$this->load->model('models_model_barang', 'modb');
$this->load->model('models_material_barang', 'matb');
$this->load->model('models_merk_barang', 'merb');
$datakb = $this->mkb->GetKategoriBarang();
$datamodb = $this->modb->GetModelBarang();
$datamatb = $this->matb->GetMaterialBarang();
$datamerb = $this->merb->GetMerkBarang();
$this->load->view('templates/admin/form_edit_barang',array(
'datakb' => $datakb,
'datamodb' => $datamodb,
'datamatb' => $datamatb,
'datamerb' => $datamerb), TRUE);
}
public function do_update(){
/*load model dahulu, karena bukan global model*/
$this->load->model('models_barang', 'mb');
$id_barang = $_POST['id_barang'];
$kode_barang = $_POST['kode_barang'];
$nama_barang = $_POST['nama_barang'];
$material_barang = $_POST['material_barang'];
$model_barang = $_POST['model_barang'];
$kategori_barang = $_POST['kategori_barang'];
$harga_barang = $_POST['harga_barang'];
$size_barang = $_POST['size_barang'];
$merk_barang = $_POST['merk_barang'];
$ket_barang = $_POST['ket_barang'];
$data_update = array(
'id_barang' => $id_barang,
'kode_barang' => $kode_barang,
'nama_barang' => $nama_barang,
'material_barang'=> $material_barang,
'model_barang' => $model_barang,
'kategori_barang' => $kategori_barang,
'harga_barang' => $harga_barang,
'size_barang' => $size_barang,
'merk_barang' => $merk_barang,
'ket_barang' => $ket_barang
);
$where = array('id_barang' => $id_barang);
$res = $this->mb->UpdateData('barang', $data_update, $where);
if($res>=1){
$this->session->set_flashdata('pesan_barang','- Update Data Barang Sukses');
redirect('admin/lihat_data');
}else{
echo "<h2>Update Data Barang Gagal</h2>";
}
}
view form_edit_barang.php
<?php echo form_open_multipart('crud_barang/do_update'); ?>
<label>Id Barang<br>
<input type="text" autofocus placeholder="Id Barang" name="id_barang" value="<?php echo $id_barang; ?>" readonly>
</label>
<label>Kode Barang<br>
<input type="text" autofocus placeholder="Kode" name="kode_barang" value="<?php echo $kode_barang; ?>">
</label>
<label>Nama<br>
<input type="text" placeholder="Nama" name="nama_barang" value="<?php echo $nama_barang; ?>">
</label>
<label>Kategori<br>
<select name="kategori_barang">
<option><?php echo $kategori_barang;?></option>
<?php foreach ($datakb as $dkb) { ?>
<option><?php echo $dkb['nama_kategori_barang'];?></option>
<?php }?>
</select>
</label>
<label>Model<br>
<select name="model_barang" required>
<option><?php echo $model_barang;?></option>
<?php foreach ($datamdb as $dmdb) { ?>
<option><?php echo $dmdb['nama_model_barang'];?></option>
<?php }?>
</select>
</label>
<label>Material<br>
<select name="material_barang" required>
<option><?php echo $material_barang;?></option>
<?php foreach ($datamatb as $dmtb) { ?>
<option><?php echo $dmtb['nama_material_barang'];?></option>
<?php }?>
</select>
</label>
<label>Merk<br>
<select name="merk_barang" required>
<option><?php echo $merk_barang;?></option>
<?php foreach ($datamerb as $dmb) { ?>
<option><?php echo $dmb['nama_merk_barang'];?></option>
<?php }?>
</select>
</label>
<label>Harga<br><input type="text" placeholder="Harga" name="harga_barang" value="<?php echo $harga_barang; ?>"></label>
<label>Ukuran<br><input type="text" placeholder="Ukuran" name="size_barang" value="<?php echo $size_barang; ?>"></label>
<label>Keterangan<br><textarea placeholder="Keterangan" name="ket_barang""><?php echo $ket_barang; ?></textarea></label>
<p><input type="submit" value="Simpan" class="btn"></p>
<?php echo form_close(); ?>
the dropdown menu data from database won't show in form edit. i don't understand why, do i miss something or what? cause there isn't notification of error. help me pls, thank you :)
the result in form add (working):
1
while in form edit, only show the original data :
2
I'm guessing its because in your edit_data function you are loading your view twice. Why that isn't giving you notices for undefined $id_barang .etc. is beyond me; also not sure why you aren't seeing the view twice (probably because you have errors suppressed).
Please note the comments.
public function edit_data($id_barang) {
/* load model dahulu, karena bukan global model */
$this->load->model('models_barang', 'mb');
$brg = $this->mb->GetBarang(" where id_barang = '$id_barang'");
$datab = array(
'id_barang' => $brg[0]['id_barang'],
'kode_barang' => $brg[0]['kode_barang'],
'nama_barang' => $brg[0]['nama_barang'],
'material_barang' => $brg[0]['material_barang'],
'model_barang' => $brg[0]['model_barang'],
'kategori_barang' => $brg[0]['kategori_barang'],
'harga_barang' => $brg[0]['harga_barang'],
'size_barang' => $brg[0]['size_barang'],
'merk_barang' => $brg[0]['merk_barang'],
'ket_barang' => $brg[0]['ket_barang']
);
// DUPLICATE, REMOVED
//$this->load->view('templates/admin/form_edit_barang', $datab);
$this->load->model('models_kategori_barang', 'mkb');
$this->load->model('models_model_barang', 'modb');
$this->load->model('models_material_barang', 'matb');
$this->load->model('models_merk_barang', 'merb');
$datakb = $this->mkb->GetKategoriBarang();
$datamodb = $this->modb->GetModelBarang();
$datamatb = $this->matb->GetMaterialBarang();
$datamerb = $this->merb->GetMerkBarang();
// MERGED ARRAY HERE
$this->load->view('templates/admin/form_edit_barang', array_merge($datab, array(
'datakb' => $datakb,
'datamodb' => $datamodb,
'datamatb' => $datamatb,
'datamerb' => $datamerb)));
}
This is my html code. I want to pass "$category_edit->c_name" value to my Update() controller. I am getting "$category_edit" variable from another controller.
I am using CodeIgniter framework.
<form method="post" action="<?php echo base_url('admin/category/update');?>">
<label>Parent Category: </label></br>
<select name="parent_id">
<?php
echo '<option value="' .$category_edit->id .'">';
echo $category_edit->p_name;
echo '</option>';
?>
</select>
<label>Category</label>
<input type="text" name="<?php echo $category_edit->c_name; ?>" id="category_name" value="<?php echo $category_edit->c_name; ?>">
<button>Update</button>
</form>
This is my update() controller.
I am getting Error:
Undefined variable: category_edit
Trying to get property of non-object
public function update(){
$this->load->model('category_model');
echo $category_edit->c_name;
}
Please kindly check this reference code:
public function update_view()
{
$this->load->model('category_model');
$data['category_edit'] = $this->category_model->get_category_values(); // return array
$data['extra_variable'] = 'lorem ipsum';
$this->load->view('category/update_view', $data);
}
at your category/update_view.php :
<form method="post" action="<?php echo base_url('admin/category/update');?>">
<label>Parent Category: </label></br>
<select name="parent_id">
<?php
echo '<option value="' .$category_edit['id'] .'">';
echo $category_edit['p_name'];
echo '</option>';
?>
</select>
<label>Category</label>
<input type="text" name="<?php echo $category_edit['c_name']; ?>" id="category_name" value="<?php echo $category_edit['c_name']; ?>">
<button>Update</button>
</form>
EDIT:
Please refer: http://www.codeigniter.com/user_guide/general/views.html#adding-dynamic-data-to-the-view
Make some changes in HTML(See below code)
<input type="text" name="category_name" id="category_name" value="<?php echo $category_edit->c_name; ?>">
public function update()
{
$this->load->model('category_model');
echo $this->input->post('category_name');
}
You need to add name of field static not any variable.
so try to add like this
<input type="text" name="category_name" id="category_name" value="<?php echo $category_edit->c_name; ?>">
and on controller you can get value of it like
$this->input->post('category_name');
I am trying to update the data to the database by php CodeIgniter and display the result in the same page during update. Below is my code.The problem is it didn't show any error message and didn't update the values in the database and It is not displaying any values in the page after update. Please help.
View Page
<?php
echo form_open('Welcome/service1');
?>
<input type="hidden" name="servicer_id" value="<?php echo $value['service_id']?>">
<input type="hidden" name="servicer_name" value="<?php echo $value['servicer_name']?>">
<input type="hidden" name="servicer_email" value="<?php echo $value['servicer_email']?>">
<input type="hidden" name="servicer_checkin" value="<?php echo $value['servicer_checkin']?>">
<input type="hidden" name="servicer_checkout" value="<?php echo $value['servicer_checkout']?>">
<input type="hidden" name="servicer_requestdate" value="<?php echo $value['servicer_requestdate']?>">
<input type="hidden" name="servicer_detail" value="<?php echo $value['servicer_detail']?>">
<input type="hidden" name="servicer_contact" value="<?php echo $value['servicer_contact']?>">
<input type="hidden" name="servicer_priority" value="<?php echo $value['servicer_priority']?>">
<label class="radio-inline">
<input type="radio" <?php if($value['status']=="Complete") {?> checked="checked"<?php } ?> name="current_status" value="Complete">Complete
</label>
<label class="radio-inline">
<input type="radio" <?php if($value['status']=="Ongoing") {?> checked="checked"<?php } ?> name="current_status" value="Ongoing">Ongoing
</label>
<label class="radio-inline">
<input type="radio" <?php if($value['status']=="Rejected") {?> checked="checked"<?php } ?> name="current_status" value="Rejected">Rejected
</label>
<button type="submit" class="btn btn-default">
<span>Status</span>
</button>
<?php
echo form_close();
?>
Controller page
public function service1()
{
$this->load->helper('url');
$page_id =$this->uri->segment(3);
$this->load->model('Login_set');
$this->Login_set->add_status();
$data['s']=$this->Login_set->select2();
$this->load->view('App_stay/pages/hotel1_service.php',$data);
}
Model page
public function add_status()
{
this->load->database();
this->load->helper('url');
hotel_id=1;
servicer_name= $this->input->post('servicer_name');
servicer_email= $this->input->post('servicer_email');
servicer_checkin= $this->input->post('servicer_checkin');
servicer_checkout= $this->input->post('servicer_checkout');
servicer_requestdate= $this->input->post('servicer_requestdate');
servicer_detail= $this->input->post('servicer_detail');
servicer_contact= $this->input->post('servicer_contact');
servicer_priority= $this->input->post('servicer_priority');
status= $this->input->post('status');
service_id= $this->input->post('servicer_id');
data=array('hotel_id'=>$hotel_id,'servicer_name'=>$servicer_name,'servicer_email'=>$servicer_email,'servicer_checkin'=>$servicer_checkin,'servicer_checkout'=>$servicer_checkout,'servicer_requestdate'=>$servicer_requestdate,'servicer_detail'=>$servicer_detail,'servicer_contact'=>$servicer_contact,'servicer_priority'=>$servicer_priority,'status'=>$status);
this->db->where('service_id',$service_id);
this->db->update('service_hotel1',$data);
}
You are passing inputs to the model but they are getting passed to the controller function.. please update
Controller:
public function service1()
{
/** you are posting the form to this function and not to the model.
* which is why all input->post must be here
*/
$servicer_name = $this->input->post('servicer_name');
$servicer_email = $this->input->post('servicer_email');
$servicer_checkin = $this->input->post('servicer_checkin');
$servicer_checkout = $this->input->post('servicer_checkout');
$servicer_requestdate= $this->input->post('servicer_requestdate');
$servicer_detail = $this->input->post('servicer_detail');
$servicer_contact = $this->input->post('servicer_contact');
$servicer_priority = $this->input->post('servicer_priority');
$status = $this->input->post('status');
$service_id = $this->input->post('servicer_id');
$this->load->helper('url');
$page_id = $this->uri->segment(3);
$this->load->model('Login_set');
$this->Login_set->add_status($servicer_name, $servicer_email, $servicer_checkin, $servicer_checkout, $servicer_requestdate, $servicer_detail, $servicer_contact, $servicer_priority, $status, $service_id);
$data['s'] = $this->Login_set->select2();
$this->load->view('App_stay/pages/hotel1_service',$data); //also view must be loaded without .php so $this->load->view('App_stay/pages/hotel1_service',$data);
}
Model:
public function add_status($servicer_name, $servicer_email, $servicer_checkin, $servicer_checkout, $servicer_requestdate, $servicer_detail, $servicer_contact, $servicer_priority, $status, $service_id)
{
$this->load->database();
$this->load->helper('url');
$hotel_id =1 ;
$data = array(
'hotel_id' => $hotel_id,
'servicer_name' => $servicer_name,
'servicer_email' => $servicer_email,
'servicer_checkin' => $servicer_checkin,
'servicer_checkout' => $servicer_checkout,
'servicer_requestdate' => $servicer_requestdate,
'servicer_detail' => $servicer_detail,
'servicer_contact' => $servicer_contact,
'servicer_priority' => $servicer_priority,
'status' => $status
);
$this->db->where('service_id',$service_id);
$this->db->update('service_hotel1',$data);
}
OK, so I have looked everywhere for a solution to my problem but found none so far.My code looks like this.I have created a dynamic view page in which edit and add views are loaded dynamically.But the problem arises when i try to retain the value of select dropdown during editing.I would be grateful if someone could help me out.
View
<div class="panel-body">
<div class="row">
<div class="col-lg-6">
<?php if(#$patient_info){
echo '<h1 class="page-header">Edit Patient</h1>';
}else{
echo '<h1 class="page-header">Add Patient</h1>';
}
?>
<?php if($this->session->flashdata('error')){ ?>
<div class="alert alert-danger"><?php echo $this->session->flashdata('error'); ?></div>
<?php } ?>
<?php if($this->session->flashdata('erroredit')){ ?>
<div class="alert alert-danger"><?php echo $this->session->flashdata('erroredit'); ?></div>
<?php } ?>
<?php //echo validation_errors('<div class="alert alert-danger">','</div>'); ?>
<form role="form" method="post" action="<?php echo isset($patient_info) ? site_url('home/patient/edit') .'/' .$patient_info->patientID : site_url('home/patient/new'); ?>">
<div class="form-group">
<?php echo form_error('pname', '<div class="alert alert-danger">', '</div>'); ?>
<label for="pname">Patient Name</label>
<input class="form-control" placeholder="Enter Patient Name" name="pname" value="<?php echo isset($patient_info) ? $patient_info->patientName : ''; ?>">
</div>
<!-- Dropdown menu for selecting clinic -->
<div class="form-group">
<label for="select">Select Clinic Name</label>
<select class="form-control" name="selectClinic">
<option value="none">Select Clinic Below</option>
<?php foreach($allclinic as $key=>$clinic){ ?>
<!--<?php //foreach($clinicByPatient as $clin): ?>-->
<option value="<?php $clinic->clinicID; ?>"
<?php if(isset($patient_info)){
echo 'selected="selected"';
}
?>
>
<?php echo $clinic->clinicName; ?>
</option>
<?php //endforeach; ?>
<?php } ?>
</select>
</div>
<!-- Select Clinic ends-->
<div class="form-group">
<label for="select">Select Dentist</label>
<select class="form-control" name="selectDentist">
<option value="">Select Dentist</option>
<?php foreach($dentistdet as $key=>$dentist){ ?>
<option value="<?php echo $did = $dentist->dentistID;?>"><?php echo $dentist->dentistName; ?></option>
<?php } ?>
</select>
</div>
<div class="form-group">
<input type="submit" class="btn btn-primary btn-lg btn-block" name="submit" value="<?php echo isset($patient_info) ? 'Update Patient' : 'Add Patient'; ?>" >
</div>
</form>
<div class="form-group">
<input type="submit" class="btn btn-danger btn-lg btn-block" value="Cancel">
</div>
Edit controller
public function edit(){
$id = $this->uri->segment(4);
$this->load->library('form_validation');
$this->load->model('clinic_model');
$this->load->model('dentist_model');
$data['patient_info'] = $this->patient_model->getPatientById($id);
$data['clinicByPatient'] = $this->patient_model->getPatientByClinic();
$data['allclinic'] = $this->clinic_model->getAllClinics();
// $data['clinicdet'] = $this->patient_model->getPatientByClinic();
if($_POST){
$this->form_validation->set_rules('pname', 'Patient Name', 'trim|required|xss_clean');
$this->form_validation->set_rules('paddress', 'Patient Address', 'trim|required|xss_clean');
$this->form_validation->set_rules('pcontact', 'Patient Contact', 'trim|required|xss_clean');
if($this->form_validation->run()== FALSE){
$data['subview'] = 'patient/patient_new';
$this->load->view('includes/layout', $data);
}else{
$patientname = $this->input->post('pname');
$patientaddress = $this->input->post('paddress');
$patientcontact = $this->input->post('pcontact');
$select = $this->input->post('selectClinic');
$option = $this->input->post('selectDentist');
$edited = $this->patient_model->editpatient($id, $patientname, $patientaddress, $patientcontact, $select, $option);
if($edited){
$this->session->set_flashdata('successedit', 'Successfully updated the record');
redirect('home/patient');
}
}
}else{
$data['subview'] = 'patient/patient_new';
$this->load->view('includes/layout',$data);
}
}
Model looks like this
<?php if( ! defined('BASEPATH')) exit('No direct script access allowed');
class Patient_model extends CI_Model{
public function countPatients(){
$countPatient = $this->db->count_all('patient');
return $countPatient;
}
public function getallpatients(){
$query = $this->db->get('patient');
if($query->num_rows()>0){
return $query->result();
}
else{
return FALSE;
}
}//getallpatients function ends
public function getPatientByClinic(){
$this->db->select('*');
$this->db->from('patient');
$this->db->join('clinic', 'patient.clinicID = clinic.clinicID', 'left');
$this->db->join('dentist', 'patient.dentistID = dentist.dentistID', 'left');
$query = $this->db->get();
if($query->num_rows>0){
return $query->result();
}
}
public function addPatientByClinic($patientname, $patientadd, $patientcontact, $select, $option){
$data = array(
'patientName' => $patientname,
'patientAddress' => $patientadd,
'patientContact' => $patientcontact,
'clinicID' => $select,
'dentistID' => $option
);
return $this->db->insert('patient',$data);
}// method ends
public function deletePatient($id){
$verifyID = array('patientID' => $id);
// $affRows = $this->db->affected_rows();
// $obj = new Patient_model;
if($verifyID){
$this->db->where($verifyID);
$this->db->delete('patient');
if($this->db->affected_rows()){
return TRUE;
}
}
}
public function editpatient($id, $patientname, $patientaddress, $patientcontact, $select, $option){
$data = array(
'patientName' => $patientname,
'patientAddress' => $patientaddress,
'patientContact' => $patientcontact,
'clinicID' => $select,
'dentistID' => $option
);
$query = $this->db->where('patientID', $id)
->update('patient', $data);
if($query){
return true;
}
}//method ends
public function getPatientById($id){
$query = $this->db->where('patientID', $id)
->get('patient');
return $query->row();
}
}//class ends
?>
In the code for your select box, you aren't actually echoing $clinic->clinicID. Thus, when the form is submitted, the value will be empty.
You also need to be careful with how you are choosing which select element will be selected by default - you aren't comparing with anything that will change within the loop. Should you be checking against $clinic->clinicID?