CodeIgniter Update Not Working - codeigniter

I have a form in a modal that I'm using to do an update on a record. The form seems to submit okay, the URL in the browser points to the right location (domain.net/cities/update_city/1) but no view shows up and the record is never updated. I've searched the internet but most tutorials seem to skim over the update or hardcode the id to update. I've only been using CI for a week.
Model Code
// Edit City
function edit_city($city_id) {
$data = array('city_name' => $this->input->post('city_name'));
$this->db->where('city_id', $city_id);
$this->db->update('cities', $data);
}
View Code (Form is in a modal and gets city_id added by jQuery)
<div class="modal-body">
<?php $this->load->helper('form'); ?>
<?php echo form_open('/cities/update_city/'); ?>
<label class="control-label" for="name">City</label>
<input type="text" name="city_name" id="city_name" value=""/><br />
<input type="submit" name="submit" class="btn-small btn-primary" value="Edit City" />
<button class="btn-small" data-dismiss="modal">Cancel</button>
<?php echo form_close(); ?>
</div>
<script>
// scripts for modal windows
$(document).on("click", ".edit-modal", function () {
var city_name = $(this).data('name');
var city_id = $(this).data('id');
$(".modal-body #city_name").val( city_name );
//set the forms action to include the city_id
$(".modal-body form").attr('action','/cities/update_city/'+city_id);
$('#editCityDialog').modal('show');
});
Controller Code
// Update City
function update_city($city_id) {
$this->load->model('cities_model');
if ($this->input->post('submit')) {
$data = array('city_name' => $this->input->post('city_name'));
$this->cities_model->edit_city('cities', array('city_id' => $city_id),$data);
redirect('/cities');
}
}

Here is an example, you can use it to update easily (i'm bad at english, sorry).
Model:
class Cities_model extends CI_Model{
function edit($table,$where,$data){
foreach ($where as $key => $value){
$this->db->where($key,$value);
}
$this->db->update($table,$data);
}
}
Controller:
function update_city($city_id){
$this->load->model('cities_model');
if($this->input->post('submit'){
$data = array('city_name' => $this->input->post('city_name'));
$this->cities_model->edit('cities',array('city_id' => $city_id),$data); //(table,where,data); see in model.
redirect('');
}
}
* if you go a link as "http://......update_city/2" and a function update_city($city_id), $city_id will receive as "2" and you need not to write uri_segment to get $city_id. Hope that help you
EDIT :
Controller:
function update_city($city_id) {
$this->load->model('cities_model');
if ($this->input->post('submit')) {
$this->cities_model->edit_city($city_id);
redirect('/cities');
}
}

Related

How to insert record in 2 tables using single form (Laravel)?

CONTROLLER
public function store_resto(Request $request){
// dd($request->all());
$restaurant = new Restaurant();
$restaurant->name = $request->input('name');
$restaurant->email = $request->input('email');
$restaurant->address = $request->input('address');
$restaurant->save();
$image = $request->hasfile('image');
$photo = rand(1,9999).'.'.$image;
$path = public_path().'/files/';
$image->move($path, $photo);
RestoImage::create([
'image'=>$image,
'resto_id'=>$restaurant->id,
]);
$request->session()->flash('status', 'Restaurant added successfully');
return redirect('list');
}
VIEW FILE
<form method="post" action="{{route('store_resto')}}" enctype="multipart/form-data">
#csrf
<div class="form-group">
<label>Resto Name</label>
<input type="name" name="name" class="form-control">
</div>
<div class="form-group">
<label>Email</label>
<input type="email" name="email" class="form-control">
</div>
<div class="form-group">
<label>Address</label>
<input type="text" name="address" class="form-control">
</div>
<div class="form-group">
<label>Image</label>
<input type="file" name="image" class="form-control">
</div><br>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
RestoImage Model
class RestoImage extends Model
{
use HasFactory;
protected $fillable = ['image','resto_id'];
public function restaurants(){
$this->belongsTo(Restaurant::class, 'resto_id');
}
}
Restaurant Model
class Restaurant extends Model
{
use HasFactory;
public $timestamps = false;
public function menus(){
$this->hasMany(Menu::class);
}
public function restoimage(){
$this->hasOne(RestoImage::class, 'resto_id');
}
}
Each restaurant will have 1 image. When an admin submits the form, 1 record should be inserted in both tables i.e. restaurants and resto_images. I tried this way but when I submit the form, It shows error "Call to a member function move() on bool". Please correct me if I am doing wrong. Thanks in advance.
Here i Worked on your code to explain how these things works.This is an example can help you. Not for two you can add so many tables from one function of controller. Approve my answer if you find solution or reason for getting error.
You have error because code doesn't find your image format or mine:type(png, jpeg)
$photo = rand(1,9999).'.'.$image;
Solution- you have to get image format or extention by this code
$extention = $emp_image_file->getClientOriginalExtension();
Your solution should be like this
$path1 = 'assets/img/emp/';
$destinationPath1 = $path1;
$photo_file = $request->file('image');
$photo='';
if($photo_file){
$file_size = $photo_file->getSize();
$image_name = $photo_file->getClientOriginalName();
$extention = $photo_file->getClientOriginalExtension();
$photo = value(function() use ($photo_file){
$filename = time().'.'. $photo_file->getClientOriginalExtension();
return strtolower($filename);
});
$photo_file->move($destinationPath1, $photo);
}
Put js in your view file
<script type="text/javascript">
function readURL(input) {
if (input.image && input.image[0]) {
var reader = new FileReader();
reader.onload = function(e) {
$('#imagePreview').attr('src', e.target.result);
}
reader.readAsDataURL(input.files[0]);
}
}
</script>
This is you input
<input type="file" class="form-control" name="image" >
I Also Worked For Other Visitors See Once
public function store_resto(Request $request){
<!-- validation code begins -->
$this->validate($request, [
'name'=>'required|max:120',
'email'=>'required|email|unique:users',
]);
<!-- validation code ends -->
$data = $request->all();
$table1 = Required_Model1::create([
'name' =>$data['emp_name'],
'email' =>$data['email'],
]);
$table2 = Required_Model2::create([
'name' => $data['emp_name'],
'code' => $data['emp_code'],
'status' => $data['emp_status'],
'email' => $data['email'],
'gender' => $data['gender'],
'table1_id' => $table1->id,
]);
$table3 = Required_Model3::create([
'role' => $data['role'],
'table1_id' => $table1->id,
'table2_id' => $table2->id,
if(isset($table1, $table2, $table3)) {
$request->session()->flash('status', 'Restaurant added successfully');
return redirect()->route('employee-manager');
}else{
return redirect()->back();
}
}
Comment or delete this part of code if you doesn't want to validate or mandatory.
$this->validate($request, [
'name'=>'required|max:120',
'email'=>'required,
]);
Above code explains
column name must be filled with 120 characters or not be blank.
column email must be filled.
if these two doesn't satisfy it will redirect back.
This below code
If validation is set like above code this will check and work as defined. If validation is set they check two fields name and email, if they filled or not blank it will proceed further. If validation is set fields are not filled or blank they redirect back. If validation is not set it will proceed further.
if(isset($table1, $table2, $table3)) {
$request->session()->flash('status', 'Restaurant added successfully');
return redirect()->route('employee-manager');
}else{
return redirect()->back();
}
Change these two lines
<input type="name" name="name" class="form-control" required="true" />
<input type="email" name="email" class="form-control" required="true" />
Model 1 should be like this
class Required_Model1 extends Model
{
protected $fillable = ['name','email'];
}
Model 2 should be like this
class Required_Model2 extends Model
{
protected $fillable = ['name','code', 'status', 'email', 'gender', 'table1_id'];
}
Model 3 should be like this
class Required_Model3 extends Model
{
protected $fillable = ['role','table1_id', 'table2_id'];
}
Let's talk on your error as you posted
You have face error because you want to move your image name in form of boolean. Here is gave you an standard code you can use it
$path1 = 'assets/img/emp/';
$destinationPath1 = $path1;
$emp_image_file = $request->file('employee_images');
$emp_image='';
if($emp_image_file){
$file_size = $emp_image_file->getSize();
$image_name = $emp_image_file->getClientOriginalName();
$extention = $emp_image_file->getClientOriginalExtension();
$emp_image = value(function() use ($emp_image_file){
$filename = time().'.'. $emp_image_file->getClientOriginalExtension();
return strtolower($filename);
});
$emp_image_file->move($destinationPath1, $emp_image);
}
Put this in which table you wanted to save
'photo' => $emp_image,
Add this in your view make sure you edit like your requirement
<script type="text/javascript">
function readURL(input) {
if (input.employee_images && input.employee_images[0]) {
var reader = new FileReader();
reader.onload = function(e) {
$('#employee_imagesPreview').attr('src', e.target.result);
}
reader.readAsDataURL(input.files[0]);
}
}
</script>
This is input
<input type="file" class="form-control" name="employee_images" >
$image = $request->hasfile('image');
This method is a boolean method. It will return true/false. Instead use
$request->file('image');
So first, here:
$image = $request->hasfile('image');
You are setting $image to a boolean by checking if it has that file and then later you want to run move on a that boolean which is not possible. Rather do:
if($request->hasfile('image'))
{
$image = $request->file('image');
$image->move($path, $photo);
}

How to Make Joomla 3x Custom Toolbar Button Work

I am trying to add a couple of custom toolbar buttons to my component, and at the moment the buttons are showing alright but can't get them to work.
My main problem is how to pass the id variable from the view layout to the sub-controller to perform a task in the case update a single column in the database.
These are my code structure
THE VIEW (view.html.php)
class LoanmanagerViewLoan extends JViewLegacy
{
protected $loanDetail;
public function display($tpl = null){
//Data from loanlist Model
$model=$this->getModel('Loan');
$this->loanDetail = $model->get_loan_detail();
$this->addToolbar();
parent::display($tpl);
}
protected function addToolbar()
{
// Get the toolbar object instance
$bar = JToolbar::getInstance('toolbar');
JToolBarHelper::Title(JText::_('Loan Details'));
//TRYING TO MAKE THIS BUTTON WORK
JToolBarHelper::custom('loan.approve', 'approve.png', 'icon-save.png', 'Approve Loan', false, false);
JToolBarHelper::custom('loan.deny', 'deny.png', 'deny.png', 'Deny Loan', false, false);
}
}
VIEW LAYOUT (tmpl/default.php)
JHtml::_('behavior.formvalidator');
<form action="<?php echo JRoute::_('index.php?option=com_loanmanager&view=loan&type=softloan&id='. (int) $loan->id); ?>" method="post" name="adminForm" id="loan-form" enctype="multipart/form-data">
<input type="hidden" name="option" value="com_loanmanager" />
<input type="hidden" name="task" value="" />
<?php echo JHtml::_('form.token'); ?>
</form>
SUBCONTROLLER (controllers/loan.php)
class LoanmanagerControllerLoan extends JControllerLegacy
{
public function approve()
{
$jinput = JFactory::getApplication()->input;
$id = $input->post->get('id', 0, 'INT');
//Perform some SQL query with the $id
return parent::display();
}
}
you need to write an input with the id in the form itself.
<input type="hidden" name="id" value="<?= (int) $loan->id ?>" />
alternatively, don't get the id from post, as you have put it in the action get url of the form
$id = $input->getInt('id');

Autocomplete dropdown in codeigniter doesn't works

I'm still new using jquery in codeigniter so i hope you can gladly help me.
I want to create autocomplete dropdown where when one option of autocomplete is selected, it will show other related fields.
The output become like this, there's no value to display
And this is my codes :
Model
function search_mtk($kode_mtk){
$this->db->like('kode_mtk', $kode_mtk , 'both');
$this->db->order_by('kode_mtk', 'ASC');
$this->db->limit(5);
return $this->db->get('m_mata_kuliah')->result();
}
Controller
public function get_mtk(){
if (isset($_GET['term'])) {
$result = $this->kit_model->search_mtk($_GET['term']);
if (count($result) > 0) {
foreach ($result as $row)
$arr_result[] = array(
'kode' => $row->kode_mtk,
'nama' => $row->nama_mtk,
);
echo json_encode($arr_result);
}
}
}
<script type="text/javascript">
$( "#kode_mtk" ).autocomplete({
source: "<?php echo base_url('fak/kit/get_mtk/?');?>",
select: function (event, ui) {
$('[name="kode_mtk"]').val(ui.item.kode);
$('[name="nama_mtk"]').val(ui.item.nama);
}
});
});
</script>
<?php echo form_open_multipart('fak/kit/file_data');?>
<div class="form-group">
<label for="program">Kode Matakuliah <span style="color:#FF0000">*</span>:</label>
<input type="text" class="form-control" id="kode_mtk" name="kode_mtk" placeholder="Type course code" />
</div>
<div class="form-group">
<label for="program">Nama Matakuliah<span style="color:#FF0000">*</span>:</label>
<input type="text" class="form-control" name="nama_mtk" placeholder="Type course name" />
</div>
<button type="submit" class="btn btn-success">Submit</button>
I hope anyone can help me with this.
Your code seems working fine but the label value not showing in the drop-down. just add value and label key value in the array to check its working or not.
Your code should be like below
$arr_result[] = array(
'kode' => $row->kode_mtk,
'nama' => $row->nama_mtk,
'value' => $row->nama_mtk,
'label' => $row->nama_mtk,
);
also, add the following call back function inside autocomplete jquery function
$( "#kode_mtk" ).autocomplete({
source: "<?php echo base_url('fak/kit/get_mtk/?');?>",
select: function (event, ui) {
$('[name="kode_mtk"]').val(ui.item.kode);
$('[name="nama_mtk"]').val(ui.item.nama);
},
response: function(event, ui){
if(ui.content.length === 0){
console.log('No results loaded!');
}else{
console.log('success!');
}
},
});
});

"Unable to load the requested file: search_result.php" error

i want create a search box , where we input student name then show all records of that students.my controller code is
public function search_function_in_controller()
{
if ($this->session->userdata('admin_login') != 1)
redirect('login', 'refresh');
$keyword = $_POST['keyword']; // you can also use $this->input->post('keyword');
$data['search_result'] = $this->crud_model->search($keyword);
$this->load->view('search_result', $data);
}
my model is :
function search($keyword)
{
$this->db->like('name',$keyword);
$query = $this->db->get('student');
return $query->result();
}
my view is :
<body>
<form action="<?=site_url('admin/search_function_in_controller')?>" method="post">
search: <input type="text" name="keyword" />
<input type="submit" value="Submit" />
</form>
<div>
<?php
// List up all results.
foreach ($results as $val)
{
echo $val['username'];
}
?>
</div>
</div>
</body>
but when we give input on search box then it give "Unable to load the requested file: search_result.php" , can anyone help me??
Verify the path that you've called in your view.
I think you intended to call the view admin/search_result in your function search_function_in_controller;

Codeigniter loop through post array

So i have this form:
<form id="stepform" action="#" method="post">
<fieldset>
<legend>Step #1</legend>
<label>Title</label>
<input type="hidden" name="csrf_modo" value="b94961394f8e6f7efaa4e37ca9007822">
<input type="text" name="field[]" class="input-xlarge">
<label>Body</label>
<textarea class="input-xlarge" name="field[]"></textarea>
</fieldset>
</form>
When user clicks a button jquery dynamically appends another two exactly same fields:
count = 2;
$("#addstep").click(function(){
$('#stepform').append('<legend>Step #' + (count++) + '</legend>');
$('#stepform').append('<label>Title</label><input type="text" name="field[]" class="input-xlarge">');
$('#stepform').append('<label>Body</label><textarea class="input-xlarge" name="field[]"></textarea>');
$('#addstep').scrollintoview();
return false;
});
As you can see one step has 2 fields, when user clicks on the button step increments and adds another 2 fields to that step and so on...
After that i send data to the controller via ajax request. Now i'm stuck on the actual query which should insert new row for every step. How could i accomplish this?
Btw i'm using codeigniter and it's bind queries:
$this->db->query($sql, $data);
Update
I corrected the handling of the difference between textareas and input fields.
Sidenote: The whole Controller logic belongs into a model. I just put it into the Controller here for simplification reasons.
HTML
<form id="stepform" action="#" method="post">
<fieldset>
<legend>Step #1</legend>
<label>Title</label>
<input type="hidden" name="csrf_modo" value="b94961394f8e6f7efaa4e37ca9007822">
<input type="text" name="field[input][]" class="input-xlarge">
<label>Body</label>
<textarea class="input-xlarge" name="field[textarea][]"></textarea>
</fieldset>
</form>
JS
count = 2;
$("#addstep").click(function(){
$('#stepform').append('<legend>Step #' + (count++) + '</legend>');
$('#stepform').append('<label>Title</label><input type="text" name="field[input][]" class="input-xlarge">');
$('#stepform').append('<label>Body</label><textarea class="input-xlarge" name="field[textarea][]"></textarea>');
$('#addstep').scrollintoview();
return false;
});
PHP
class SomeController extends MY_Controller{
public function process_request()
{
$insert_data = array();
$field_data = $this->input->post('field');
for($i = 0; $i < count($field_data['input']); $i++)
{
$insert_data[] = array(
'db_col_name_input' => $field_data['input'][$i],
'db_col_name_textarea' => $field_data['textarea'][$i]
);
}
$this->db->insert_batch('my_table', $insert_data);
}
}
Old Answer:
Since you're appending square brackets to your input fields name you will get an array with the values of all fields that are having this name. So you can go trough them with a foreach loop and store all the values in an array and use CodeIgniters insert_batch() Method to insert multiple data at the same time.
class SomeController extends MY_Controller{
public function process_request()
{
$insert_data = array();
foreach($this->input->post('field') AS $field)
{
$insert_data[] = array(
'db_col_name' => $field
)
}
$this->db->insert_batch('my_table', $insert_data);
}
}

Resources