Update the database with codeigniter and display the updation in same page - php

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

Related

Old values not appearing in text field when called

I'm trying to call the old values to be edited. What part am I wrong at?
<?php
if (isset($_GET['edit'])) {
$id = $_GET['edit'];
$update = true;
$record = mysqli_query($db, "SELECT * FROM bookinfo WHERE BookNo='$BookNo'");
if (mysqli_num_rows($record) == 1 ) {
$n = mysqli_fetch_array($record);
$BookNo = $n['BookNo'];
$ISBN = $n['ISBN'];
$title = $n['title'];
$author = $n['author'];
$publisher = $n['publisher'];
$status = $n['status'];
$cost = $n['cost'];
}
}
?>
<a href="viewBook.php?edit=<?php echo $row['BookNo']; ?>" class="edit_btn" >Edit</a>
</td>
<?php
if (isset($_GET['edit'])) { ?>
<form method="post" action = "viewBook.php">
<input type="hidden" name="BookNo" value="<?php echo $BookNo; ?>">
<input type="text" name="ISBN" value="<?php echo $ISBN; ?>">
<input type="text" name="title" value="<?php echo $title; ?>">
<input type="text" name="author" value="<?php echo $author; ?>">
<input type="text" name="publisher" value="<?php echo $publisher; ?>">
<input type="text" name="status" value="<?php echo $status; ?>">
<input type="text" name="cost" value="<?php echo $cost; ?>">
<?php if ($update == true): ?>
<button class="btn" type="submit" name="update" style="background: #556B2F;" >update</button>
<?php else: ?>
<button class="btn" type="submit" name="save" >Save</button>
<?php endif ?>
<?php } ?>
</form>
So far, what it does is, when the user clicks the edit button, it just shows 6 text fields. I thought by doing what I did, it was supposed to show the details already filled in the textbox.
When you do
$record = mysqli_query($db, "SELECT * FROM bookinfo WHERE BookNo='$BookNo'");
$BookNo is not defined.
maybe you wanted to do something like this:
$id = $_GET['edit'];
$update = true;
$record = mysqli_query($db, "SELECT * FROM bookinfo WHERE BookNo='$id'");
<form method="post" action = "viewBook.php">
your form method is "post" but you are checking $_GET You must check $_POST
if (isset($_GET['edit']))
you are passing value in $id And using $BookNo which not define.
only 6 input field will be show because first one is using hidden property.
<input type="hidden" name="BookNo" value="<?php echo $BookNo; ?>">
when you click on submit button data will be receive by $_POST

Codeigniter - Can't update data in MySQL Database

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

codeigniter: Update table from another table with session

I'm having some challenge updating table a form joined with session userdata from another table. Each time I run, it displays undefined function. Please let me know where I'm getting it wrong so could fix it.Please find time to review it
I'm a newbie.
Here's the controller
public function index()
{
if ( $this->session->userdata('logged_in') )
{
$id = $this->session->userdata('id');
$this->load->model('Userinfo_model');
$data['result'] = $this->Userinfo_model->get_users($id);
$this->load->view("usermain_info", $data);
}
}
public function update_user (){
$data = $this->input->post('userId');
$id = array(
'balance' => $this->input->post('balance'),
'id' => $this->input->post('id')
);
$this->Userinfo_model->update_user($id, $data);
$this->index();
}
Model
function get_users($userid)
{
if (isset($this->session->userdata['logged_in'])) {
$userid = ($this->session->userdata['logged_in']['id']);
} else {
header("location: nwpgroup/nwp2/index.php");
}
/* all the queries relating to the data we want to retrieve will go in here. */
$query = $this->db->query("SELECT * FROM walletbalance WHERE userId='$userid'");
return $query;
}
function update_user($id,$data){
if (isset($this->session->userdata['logged_in'])) {
$data = ($this->session->userdata['logged_in']['id']);
} else {
header("location: nwpgroup/nwp2/index.php");
}
$this->db->where('userId', $data);
$this->db->update('walletbalance', $id);
}
View
<form method="post" action="<?php echo base_url() . "index.php/userinfo/update_user"?>">
<?php if($result->num_rows() == 0){
echo 'No user found';
}
else {
foreach ( $result->result_array() as $new_user ){ ?>
<h4>Your name:<input value=" <?php echo $new_user['balance'] ?>" type="text" /> </h4><br />
<h4>Your name:<input value=" <?php echo $new_user['id'] ?>" type="text"/> </h4><br/>
<h4>Your name: <input value="<?php echo $new_user['userId'] ?>" type="hidden"/> </h4>
<input type="submit" id="submit" name="dsubmit" value="Update">
<?php }
}
?>
Error message
Message: Undefined property: Userinfo::$Userinfo_model
and
Message: Call to a member function update_user() on null
Thanks, I'm grateful
change your view as follows :
<form method="post" action="<?php echo base_url() . "index.php/userinfo/update_user"?>">
<?php if($result->num_rows() == 0){
echo 'No user found';
}
else {
foreach ( $result->result_array() as $new_user ){ ?>
<h4>Your name:<input value=" <?php echo $new_user['balance'] ?>" type="text" name="balance" /> </h4><br />
<h4>Your name:<input value=" <?php echo $new_user['id'] ?>" type="text" name="id" /> </h4><br/>
<h4>Your name: <input value="<?php echo $new_user['userId'] ?>" type="hidden" name="userId"/> </h4>
<input type="submit" id="submit" name="dsubmit" value="Update">
<?php }
}
?>
</form>
form will send data to server only if the element has a name
and you cannot submit a form multiple times. The above code will create update button for each row. So if you want to update all the records in a single updation, use update_batch() in codeigniter. and change view as follows :
foreach ( $result->result_array() as $new_user ){ ?>
<h4>Your name:<input value=" <?php echo $new_user['balance'] ?>" type="text" name="balance" /> </h4><br />
<h4>Your name:<input value=" <?php echo $new_user['id'] ?>" type="text" name="id" /> </h4><br/>
<h4>Your name: <input value="<?php echo $new_user['userId'] ?>" type="hidden" name="userId"/> </h4>
<?php } ?>
<input type="submit" id="submit" name="dsubmit" value="Update">
for reference : https://www.codeigniter.com/userguide3/database/query_builder.html#updating-data
In your update_user function you didn't include the usermain_info model. That is why the error is coming.
Please create constructor function and include the model in it. I have updated your code. Try with this.
<?php
function __construct() {
parent::__construct();
$this->load->library('session');
$this->load->model('Userinfo_model');
}
public function index(){
if ( $this->session->userdata('logged_in') ){
$id = $this->session->userdata('id');
$data['result'] = $this->Userinfo_model->get_users($id);
$this->load->view("usermain_info", $data);
}
}
public function update_user (){
$data = $this->input->post('userId');
$id = array(
'balance' => $this->input->post('balance'),
'id' => $this->input->post('id')
);
$this->Userinfo_model->update_user($id, $data);
$this->index();
}
?>

update multiple form using codeiginiter

I want to update data for multiple users using codeginiter, I used the bellow script but it only updates for one user.
<?php echo form_open(base_url() . 'get/save_payment', array('role' => 'form')); ?>
<?php foreach ($mature as $key => $value) { ?>
<input type="text" class="form-control" value="<?php echo $value->id; ?>" readonly name="id[]">
<input type="text" class="form-control" value="<?php echo $value->name; ?>" readonly name="name[]">
<input type="text" class="form-control" value="0" readonly name="amount[]">
<?php } ?>
<button type="submit" class="btn btn-success">Confirm Request</button>
<?php echo form_close(); ?>
CONTROLLER
public function save_payment() {
$this->load->library('form_validation');
$this->form_validation->set_rules('id[]', 'id', 'required|trim');
$this->form_validation->set_rules('name[]', 'name', 'trim');
$this->form_validation->set_rules('amount[]', 'Amount', 'required|max_length[4]|trim|strip_tags');
if ($this->form_validation->run() == FALSE) {
$this->session->set_flashdata('id', set_value('id'));
$this->session->set_flashdata('name', set_value('name'));
$this->session->set_flashdata('amount', set_value('amount'));
$this->payment();
} else {
$id = set_value('id[]');
$data['amount'] = set_value('amount[]');
$data['name'] = set_value('id[]');
$this->dashboard->update_ph($data,$id);
}
}
Here is the MODEL :
public function update_ph($data,$id)
{
$this->db->where('id', $id);
$this->db->update('gw_ph', $data);
}
Kindly explain on how I can go through this code and get it working without error.
Thanks
Codeigniter use 'name' attributte for get data from 'input' and you use same 'name' attributte for all of your readonly input

Codeigniter submit not working

Error
Submit button from the view does not pass the value.
After submit button is clicked, then pass value from view to controller, then to the model. Database structure consists of table(booking) and column name ( booked_id, name and mobile) . But the value is not passed to the controller neither to the database.
View
<form method="post" action="">
<input type="text" value="<?php echo isset($post)?$post->name:''; ?>" name="manakamana[name]"/>
<input type="text" value="<?php echo isset($post)?$post->mobile:''; ?>" name="manakamana[mobile]"/>
<button type="submit" class="btn btn-success">POST</button>
</form>
Controller
public function bookManakamana(){
if ($data = $this->input->post('manakamana')) {
$data['created_on'] = date('Y-m-d H:i:s');
$this->booking->add($data);
redirect('booking');
} else {
$this->load->view('index');
}
}
Model
function add($data)
{
$this->db->insert('booking', $data);
}
Form action is not given
<form method="post" action="<?php echo base_url().'controller name/function name' ?>">
<input type="text" value="<?php echo isset($post)?$post->name:''; ?>" name="name"/>
<input type="text" value="<?php echo isset($post)?$post->mobile:''; ?>" name="mobile"/>
<button type="submit" class="btn btn-success">POST</button>
in controller
public function bookManakamana(){
if ($this->input->post('name')) {
$data['created_on'] = date('Y-m-d H:i:s');
$data['name'] = $this->input->post('name');
$data['mobile'] = $this->input->post('mobile');
$this->booking_models->add($data); //booking_model is the name of model
redirect('booking');
} else {
$this->load->view('index');
}}

Categories