i have a problem in updating data in codeigniter. There's no error message but The data cannot be updated when i change the data name and click submit the button.
I'm new in CI so i cant really figure out where did i do wrong. can you help me?
thankyou in advance
my view (views/form/form_edit_bank.php)
<form action="<?php echo site_url('bankdatel/updatebank/'.$row_bank['idbank']);?>" method="post">
<div class="box-body">
<div class="form-group">
<label>Nama Bank: </label>
<input type="text" name="namabank" value="<?php echo $row_bank['namabank']?>">
</div>
</div>
<div class="box-footer">
<button type="submit" class="btn btn-primary">Ubah</button>
</div>
</form>
controller (controllers/bankdatel.php)
public function editbank(){
$this->load->model('model_bankdatel');
$data['row_bank'] = $this->model_bankdatel->selectbank($this->uri->segment(2));
$this->load->view('template/header');
$this->load->view('form/form_edit_bank', $data);
$this->load->view('template/footer');
}
public function updatebank(){
$arrdata = array(
'namabank' => $this->input->post('namabank')
);
$this->load->model('model_bankdatel');
$this->model_bankdatel->updatebank($arrdata, $this->uri->segment(3));
$this->session->set_flashdata('berkas', "<script>alert('Nama Bank Berhasil Diubah');</script>");
redirect('bankdatel');
}
model (model/model_bankdatel.php)
public function updatebank($data, $id){
$this->db->where('idbank', $id);
$this->db->update('mt_bank', $data);
}
public function selectbank($id)
{
$this->db->where('idbank', $id);
return $this->db->get('mt_bank')->row();
}
Your form URL is site_url('bankdatel/updatebank/'.$row_bank['idbank']). It means 1 argument is passing to function. So get $bankid like this
public function updatebank($bankid){
$arrdata = array(
'namabank' => $this->input->post('namabank')
);
$this->load->model('model_bankdatel');
$this->model_bankdatel->updatebank($arrdata, $bankid);
$this->session->set_flashdata('berkas', "<script>alert('Nama Bank Berhasil Diubah');</script>");
redirect('bankdatel');
}
And use return with update query in the model
return $this->db->update('mt_bank', $data);
Related
I have a small problem with my Controller action. I can't update my "link" in Database, bt dd method work is correctly when I'm try to check data.
Form
<form class="col-lg-push-6" action="/admin/links/{{$link->id}}/update" method="POST">
#csrf
<div class="form-group bmd-form-group">
<label class="bmd-label-floating">New Link Value</label>
<input type="text" class="form-control" size="100" name="value">
<button class="btn btn-primary" type="submit">Update</button>
</div>
</form>
Controller
public function update(Request $request, $id)
{
$this->validate($request, [
'value' => 'required'
]);
$link=RedirectUrl::AllLinks()->where('id', $id);
$link->value = $request->input('value');
return redirect()->back()->with('message', 'Link Updated!');
}
Model
public function scopeAllLinks($query){
return $query->get();
}
Route
Route::prefix('admin')->middleware('auth')->group(function(){
Route::get('/', 'Admin\IndexController#index');
Route::get('dashboard', 'Admin\IndexController#index')->name('dashboard');
Route::get('links', 'Admin\LinkController#index')->name('links');
Route::get('links/{id}', 'Admin\LinkController#linkById');
Route::post('links/{id}/update', 'Admin\LinkController#update');
});
Few things here:
Your scopeAllLinks scope is incorrect, you don't call get inside a scope, instead you return the query builder instance.
You can use find since you're passing in a record id:
$link = RedirectUrl::find($id);
You never call save or update on the record:
$link->value = $request->input('value');
$link->save(); // <--- add this
Good evening , for school i am trying to create a simple CRUD app, using laravel 6 and mongoDB.
I can get read, update and delete working but creat fails with The POST method is not supported for this route. Supported methods: GET, HEAD.. I have searched the answers here and other sites but im stuck for 2 days now (could be something very silly but im not seeing it)
my routes are:
Route::get('/home', 'HomeController#index')->name('home');
Route::get('/post/{_id?}', 'PostController#form')->name('post.form');
Route::post('/post/save/', 'PostController#save')->name('post.save');
Route::put('/post/update/{_id}', 'PostController#update')->name('post.update');
Route::get('/post/delete/{_id}', 'PostController#delete')->name('post.delete');
form.blade is:
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">Post Form</div>
<div class="card-body">
#if($data)
<form action = "{{Route ('post.update', $data->_id)}}" method="post">
#csrf
#method('PUT')
<div class="form-group">
<label for="usr">Title:</label>
<input type="text" class="form-control" name="title" value = "{{$data->title}}" >
</div>
<div class="form-group">
<label for="comment">Content:</label>
<textarea class="form-control" rows="5" name="content">{{$data->content}}</textarea>
</div>
<p align="center"> <button class="btn btn-primary">save</button></p>
</form>
#else
<form action = "{{Route ('post.form')}}" method="post">
#csrf
<div class="form-group">
<label for="usr">Title:</label>
<input type="text" class="form-control" name="title">
</div>
<div class="form-group">
<label for="comment">Content:</label>
<textarea class="form-control" rows="5" name="content"></textarea>
</div>
<p align="center"> <button class="btn btn-primary">save</button></p>
</form>
#endif
</div>
</div>
</div>
</div>
#endsection
and my PostController is:
<?php
namespace App\Http\Controllers;
use App\Post;
use Illuminate\Http\Request;
class PostController extends Controller
{
//
public function form($_id = false){
if($_id){
$data = Post::findOrFail($_id);
}
$data = false;
return view ('post.form', compact('data'));
}
public function save (Request $request){
$data = new Post($request->all());
$data->save();
if($data){
return redirect()->route('home');
}else{
return back();
}
}
public function update (Request $request, $_id){
$data = post::findOrFail($_id);
$data->title = $request->title;
$data->content = $request->content;
$data->save();
/* return response()->json([
'name' => 'Abigail',
'state' => 'CA'
]); */
if($data){
return redirect()->route('home');
}else{
return back();
}
}
public function delete($_id){
$data = post::destroy($_id);
if($data) {
return redirect()->route('home');
}
else {
dd('error cannot delete this post');
}
}
}
Anybody any idea what i am missing?
Thanks in advance
You have to replace this line <form action = "{{Route ('post.form')}}" method="post"> with <form action = "{{Route ('post.save')}}" method="post">
You are using wrong route. Please change to Route ('post.save')
EDIT: I found that one myself, the PostControler didnt return a view if there was an $_id
Thanks for the help everyone!
Thanks for pointing that out, it did bring my from back to life :) However it breaks the update function :S.
When i now click on the edit button, the form does no longer get filled with the data for the post, and "save" creates a new post in stead of updating it.
I tried to make a function in the CodeIgniter framework so users can search products from my database and display (echo) them if a certain product is found.
The problem is if I try to search for a keyword I see a blank page and nothing happens.
Search controller:
<?php
class SearchController extends CI_Controller {
public function index(){
$data = array();
//load view
$this->load->view('result_view',$data);
}
function search_keyword()
{
$this->load->model('Search_model');
$keyword = $this->input->post('keyword');
$data['results'] = $this->Search_model->search($keyword);
}
}
My search model:
<?php
class Search_model extends CI_Model {
function search($keyword) {
$this->db->select('product_naam');
$this->db->like('product_naam',$keyword);
$query = $this->db->get('products');
return $query->result();
}
}
?>
My view file:
<h2>Zoek een cadeau</h2><br>
<form class="navbar-form" role="search" action="<?= base_url('SearchController/search_keyword')?>" method = "post">
<div class="input-group">
<input type="text" class="form-control" placeholder="Search" name = "keyword" size="30px; ">
<div class="input-group-btn">
<button class="btn btn-default " type="submit" value = "Search"><i class="glyphicon glyphicon-search"></i></button>
</div>
</div>
</form>
</center>
<div class="clearfix"></div>
Change
return $query->result();
to
return $query->result_array();
Also, you are not calling result page in function and passing data in view.
function search_keyword()
{
$this->load->model('Search_model');
$keyword = $this->input->post('keyword');
$data['results'] = $this->Search_model->search($keyword);
$this->load->view('result_view',$data);
}
I have a simple input form with a text input field and a submit button. I am trying to get the value from the input field to be displayed again on the same page after the submit button is clicked. So far laravel always throws an error that the variable is undefined.
Route:
Route::get('/find/names', "FindController#get_name")->name('names');
Controller
public function get_name(){
$name = Input::get('name_by_user');
return $name;
}
view
<form role="form" method="GET">
<div class="row">
<div class="form-group">
<div class="input-group">
<input type="text" name="name_by_user"/>
<span class="input-group-btn">
<button class="btn search-button" type="submit">
<span aria-hidden="true">
<span>Search</span>
</button>
</span>
</span>
</div>
</div>
</div>
</form>
display name after submitting: {{$name}}
I would do something like this
Route
Route::name('names')->get('/find/names', "FindController#get_name");
Controller
public function get_name(){
$collection = Input::all();
$name = $collection->pluck('name_by_user');
return view('view_file_in_resources', compact('name'));
}
Now you will have a $names collection in your view.
But if you only want to fetch result from one row, you controller should look like this:
public function get_name($name){
$name = Input::where('name_by_user', $name)->get();
return view('view_file_in_resources', compact('name'));
}
And your routes file
Route::name('names')->get('/find/names/{name}', "FindController#get_name");
When generating a view inside a controller for a route, you can do the following in a function to return a view with data depending on whether it exists.
public function showNameView() {
if(is_null(Input::get('name_by_user'))
{
return view('my.view')->with(['name' => Input::get('name_by_user')]);
}
else
{
return view('my.view')->with(['name' => Input::get('name_by_user')]);
}
}
You need to return the same view:
public function get_name(Request $request)
{
return view('same.view', ['name' => $request->name]);
}
Or you can redirect back:
return redirect()->back()->with('name', $request->name);
And display name like using session data:
#if (session()->has('name'))
{{ session('name') }}
#endif
I have tried everything I can think of but whenever I click submit the form passes on a null value, I dont know if it is the problem with the form or the controller or even the view. I changed this->input->post to posted data and i get an error of undefined variable posted data, please help.
Controller:
public function addmenu(){
$this->load->model('organizer_model');
$data = array(
'menu_name' => $this->input->post('menu name'),
'price' => $this->input->post('price'),
'email' => $this->session->userdata('email')
);
if($this->organizer_model->insertmenu($data)) {
$this->session->set_flashdata('message', 'Your menu has been added');
redirect('/menu/index', 'refresh');
} else {
$this->session->set_flashdata('message', 'Your menu was not added, please try again');
redirect('/menu/index', 'refresh');
}
View:
<form action="<?php echo site_url('Organizer/addmenu'); ?>" method="post" class="form-horizontal no-margin">
<div class="control-group">
<label class="control-label" for="menuname">
Menu Name
</label>
<div class="controls controls-row">
<input class="span3" name="data[menuname]" type="text" placeholder="Enter menu Name">
</div>
</div>
<div class="control-group">
<label class="control-label" for="price">
Price
</label>
<div class="controls controls-row">
<input class="span3" name="data[price]" type="text" placeholder="">
</div>
</div>
<div class="form-actions no-margin">
<button type="submit" name="submit" class="btn btn-info pull-right">
Add menu
</button>
<div class="clearfix">
</div>
</div>
</form>
Model:
public function insertmenu($data) {
$condition = "email = '" . $data['email'] . "'";
$this->db->select('organizer_id');
$this->db->from('organizer');
$this->db->where($condition);
$this->db->limit(1);
$query = $this->db->get();
if ($query->num_rows() > 0){
array_pop($data); //will remove email from data
$row = $query->row();
$data['organizer_id'] = $row->organizer_id;
$this->db->insert('menu', $data);
if ($this->db->affected_rows() > 0) {
return true;
} else {
return false;
}
} else {
return false;
}
}
I notice same question here codeigniter- insert data into db not working
Checks
Make sure you load your form helper and url helper.
Make sure you use form validation when submitting form in codeigniter on controller.
From this php user guide here http://php.net/manual/en/reserved.variables.post.php
Example on your input would be like person[0][first_name]
<form action="" method="">
<input type="text" name="data_posts[0][menu_name]" placeholder="Enter menu Name">
<input type="text" name="data_posts[0][price]" placeholder="">
</form>
Model
<?php
class Model_something extends CI_Model {
public function add_menu() {
$data_posts = $this->input->post('data_posts');
foreach ($data_posts as $data_post) {
$data = array(
'email' => $this->session->userdata('email'),
'menu_name' => $data_post['menu_name'],
'price' => $data_post['price']
);
$this->db->insert('tablename', $data);
}
}
}
Controller
<?php
class Add_menu extends CI_Controller {
public function index() {
$this->load->helper('form');
$this->load->helper('url');
$this->load->library('form_validation');
$data_posts = $this->input->post('data_posts');
foreach ($data_posts as $data_post) {
$this->form_validation->set_rules('data_posts['.$data_post.'][menu_name]', 'Menu Name', 'required');
$this->form_validation->set_rules('data_posts['.$data_post.'][price]', 'Price', 'required');
}
if ($this->form_validation->run() == FALSE) {
$this->load->view('some_view');
} else {
$this->load->model('model_something');
$this->model_something->add_menu();
redirect('to_success_page');
}
}
}
You could also check if has been inserted by using callback function
Codeigniter 3 user guide form validation http://www.codeigniter.com/user_guide/libraries/form_validation.html
Codeigniter 2 user guide form validation http://www.codeigniter.com/userguide2/libraries/form_validation.html
Also you should upgrade to the new bootstrap I see your using old version.