How to input multiple value with checkbox in CodeIgniter? - codeigniter

I have prepared the form to be inputted to the database, but specifically for multiple checkboxes. I've found a similar case with the solutionbut not with the algorithm that I use
Here it is my controller
class Ruang extends CI_Controller {
public function __construct(){
parent::__construct();
$this->load->model("m_ruang");
$this->load->library('form_validation');
if($this->session->userdata('status') != "login"){
redirect(base_url("login"));
}
}
public function index()
{
$data["ruang"] = $this->m_ruang->getAll();
$this->load->view('admin/ruang/index.php', $data);
}
public function add()
{
$ruang = $this->m_ruang;
$validation = $this->form_validation;
$validation->set_rules($ruang->rules());
if ($validation->run()) {
$ruang->save();
$this->session->set_flashdata('success', 'Berhasil ditambahkan');
}
$this->load->view("admin/ruang/add_ruang");
}
Here it is my models
class M_ruang extends CI_Model
{
private $_table = "ruang";
public $id_ruang;
public $ruang;
public $kapasitas_kuliah;
public $kapasitas_ujian;
public $layout;
public $fasilitas;
public function getAll()
{
return $this->db->get($this->_table)->result();
}
public function getById($id)
{
return $this->db->get_where($this->_table, ["id_ruang" => $id])->row();
}
public function save()
{
$post = $this->input->post();
$this->id_ruang = uniqid();
$this->ruang = $post["ruang"];
$this->kapasitas_kuliah = $post["kapasitas_kuliah"];
$this->kapasitas_ujian = $post["kapasitas_ujian"];
$this->layout = $post["layout"];
$this->fasilitas = $post["fasilitas"];
$this->db->insert($this->_table, $this);
}
and here part of form view
<form action="<?php base_url('ruang/add') ?>" method="post" enctype="multipart/form-data" >
<div class="form-group">
<label for="ruang">Nama Ruang</label>
<input class="form-control <?php echo form_error('ruang') ? 'is-invalid':'' ?>"
type="text" name="ruang" placeholder="Masukkan nama ruangan" />
<div class="invalid-feedback">
<?php echo form_error('ruang') ?>
</div>
</div>
<div class="form-group">
<label for="kapasitas_kuliah">Kapasitas Kuliah</label>
<input class="form-control <?php echo form_error('kapasitas_kuliah') ? 'is-invalid':'' ?>"
type="number" name="kapasitas_kuliah" min="0" placeholder="Tentukan kapasitas kuliah" />
<div class="invalid-feedback">
<?php echo form_error('kapasitas_kuliah') ?>
</div>
</div>
<div class="form-group">
<label for="kapasitas_ujian">Kapasitas Kuliah</label>
<input class="form-control <?php echo form_error('kapasitas_ujian') ? 'is-invalid':'' ?>"
type="number" name="kapasitas_ujian" min="0" placeholder="Tentukan kapasitas ujian" />
<div class="invalid-feedback">
<?php echo form_error('kapasitas_ujian') ?>
</div>
</div>
<div class="form-group">
<label for="layout">Layout</label>
<input class="form-control"
data-inputmask="'mask': ['99 x 99']" data-mask
type="text" name="layout" placeholder="Tentukan layout ruangan" />
</div>
<div class="form-group">
<label for="fasilitas">Fasilitas Tersedia</label> <br>
<input type="checkbox" name="fasilitas[]" value="Proyektor"> Proyektor
<br>
<input type="checkbox" name="fasilitas[]" value="Papan Tulis"> Papan Tulis
<br>
<input type="checkbox" name="fasilitas[]" value="Jam Dinding"> Jam Dinding
<br>
<input type="checkbox" name="fasilitas[]" value="AC"> AC
<br>
<input type="checkbox" name="fasilitas[]" value="Kipas Angin"> Kipas Angin
<br>
<input type="checkbox" name="fasilitas[]" value="Tong Sampah"> Tong Sampah
<div class="invalid-feedback">
<?php echo form_error('fasilitas') ?>
</div>
</div>
<input class="btn btn-success" type="submit" name="btn" value="Save" />
</form>
This really hinders my project, I hope someone can help

You can use the following line too :
$fasilitas = implode(',', $this->input->post( 'fasilitas' , TRUE ) );

If you can save fasilitas in your database as string. Then you can implode fasilitas array with comma separated as shown below:
$this->fasilitas = implode(',',$post["fasilitas"]);
it will stored in back-end side(Database) something like that.
Proyektor,Papan Tulis
I hope this will works for you.

You Can Use This to Get fasilitas as array :
$fasilitas = $this->input->post('fasilitas'); // Like array('AC','Proyektor','Kipas Angin');

In order for you to get all the checked boxes store in database, write this code.
$values = $post['fasilitas'];
$fasilitas = "";
foreach($values as $val)
{
$fasilitas .= $val . ", ";
}
Then store $fasilitas to db.
$data = array(
'fasilitas' => $fasilitas,
);
$this->db->insert('table_name', $data);
Hope that helps :)

Related

POST method is not working in Codeigniter v4

I'm using form_open() for open form but form method is always showing get.
echo $this->request->getMethod() showing always get.
generated HTML is:
<form action="http://darkadmin.com/admin" method="post" accept-charset="utf-8">
<input type="hidden" name="_csrf" value="45435e3abd2d067883dacd6f62280fa7" />
<label for="fname">First name:</label>
<input type="text" id="fname" name="fname"><br><br>
<label for="lname">Last name:</label>
<input type="text" id="lname" name="lname"><br><br>
<input type="submit" value="Submit">
</form>
View:
<?php if(isset($validation)) { ?>
<?php echo $validation->listErrors(); ?>
<?php } ?>
<?php echo form_open(current_url().'admin'); ?>
<label for="fname">First name:</label>
<input type="text" id="fname" name="fname"><br><br>
<label for="lname">Last name:</label>
<input type="text" id="lname" name="lname"><br><br>
<input type="submit" value="Submit">
<?php echo form_close(); ?>
Controller:
<?php
namespace App\Controllers;
class Login extends BaseController
{
public function __construct(){
helper(['form', 'url']);
}
public function index()
{
$rules = [
'fname' => 'required',
'lname' => 'required'
];
echo $this->request->getMethod();
if($this->request->getMethod() == 'post' && $this->validate($rules)){
echo 'Success';
} else {
$this->data['validation'] = $this->validator;
}
return view($this->data['config']->theme.'login_view',$this->data);
}
}
How can I solve this problem? Please help me

Update on "user" and "technicien" with one to one connection

I have two tables user and technician with one to one connection, technician inherits from user. After editing technician information through edit form and saving no update happens on tables user and technician. and no errors as well.
Here is my code:
Controllers
public function edit($id)
{
$technicien=technicien::find($id);
$user = $technicien->user;
return view('technicien.edit',['technicien'=>$technicien])->with('user',$user);
}
public function update(Request $request, $id)
{
// do some request validation
$technicien=technicien::find($id);
$technicien->update($request->all());
$technicien->user->update($request->get('user'));
$user->nom = $request->update('nom');
return redirect('/technicien');
}
View
#extends('Layouts/app')
#extends('Layouts.master')
#section('content')
<div class="container">
<div class="row">
<div class="col-md-10">
<h1>Modifier Technicien</h1>
<form action="{{ route('technicien.update', $technicien->technicien ) }}" method="update">
{{csrf_field()}}
{{ method_field('PATCH') }}
<div class="form-group">
<label for="nom">Nom</label>
<input id="nom" type="text" class="form-control" name="user[nom]" value="{{$user->nom}}" >
</div>
<div class="form-group">
<label for="prenom">Prenom</label>
<input id="prenom" type="text" class="form-control" name="user[prenom]" value="{{$user->prenom}}" >
</div>
<div class="form-group">
<label for="prenom">Email</label>
<input id="prenom" type="text" class="form- control" name="user[email]" value="{{$user->email}}" >
</div>
<div class="form-group">
<label for="">moyenne Avis</label>
<input type="text" name="moyenne_avis" class="form-control" value ="{{$technicien->moyenne_avis}}" >
</div>
<div class="form-group">
<label for="">Etat Technicien</label>
<input type="text" name="actif" class="form-control" value ="{{$technicien->actif}}" >
</div>
<div class="form-group">
<input type="submit" value="enregistrer" class="form-control btn btn-primary">
</div>
</div>
</form>
</div>
</div>
#endsection
Route.php
Route::get('/technicien/{id}/edit', 'TechnicienController#edit');
Route::patch('/technicien/{id}', 'TechnicienController#update')->name('technicien.update');
Model1
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class technicien extends Model
{
protected $fillable = [
'moyenne_avis', 'actif',
];
use SoftDeletes;
protected $guarded = [];
protected $dates = ['deleted_at'];
public function zoneintervention()
{
return $this->belongsToMany('App\zoneintervention','technicien_zone','technicien_id','zoneintervention_id');
}
public function metier()
{
return $this->belongsToMany('App\metier','technicien_metier','technicien_id','metier_id');
}
public function user()
{
return $this->belongsTo('App\User');
}
public function tarificationtache()
{
return $this->belongsToMany('App\tarificationtache','technicien_tarificationtache','technicien_id','tarificationtache_id');
}
}
Model2
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
public function technicien()
{
return $this->hasOne('App\technicien');
}
use Notifiable;
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'email', 'password','nom','prenom','tel','mobil','role',
];
/**
* The attributes that should be hidden for arrays.
*
* #var array
*/
protected $hidden = [
'password', 'remember_token',
];
}
controler
public function edit($id)
{
// better to use findOrFail (it will throw an exception about missing
objects)
$technicien = technicien::findOrFail($id);
return view('technicien.edit', compact('technicien'));
}
public function update(Request $request, $id)
{
$technicien=technicien::findOrFail($id);
$technicien->user->update($request->get('user'));
$technicien->update($request->get('technicien'));
return redirect('/technicien');
}
and the view
#extends('Layouts/app')
#extends('Layouts.master')
#section('content')
<div class="container">
<div class="row">
<div class="col-md-10">
<h1>Modifier Technicien</h1>
<form action="{{ route('technicien.update', $technicien ) }}"
method="post">
{{csrf_field()}}
{{ method_field('patch') }}
<div class="form-group">
<label for="nom">Nom</label>
<input id="nom" type="text" class="form-control"
name="user[nom]" value="{{$technicien->user->nom}}" >
</div>
<div class="form-group">
<label for="prenom">Prenom</label>
<input id="prenom" type="text" class="form-control"
name="user[prenom]" value="{{$technicien->user->prenom}}" >
</div>
<div class="form-group">
<label for="prenom">Prenom</label>
<input id="prenom" type="text" class="form-control"
name="user[tel]" value="{{$technicien->user->tel}}" >
</div>
<div class="form-group">
<label for="prenom">Prenom</label>
<input id="prenom" type="text" class="form-control"
name="user[mobil]" value="{{$technicien->user->mobil}}" >
</div>
<div class="form-group">
<label for="prenom">Prenom</label>
<input id="prenom" type="text" class="form-control"
name="user[role]" value="{{$technicien->user->role}}" >
</div>
<div class="form-group">
<label for="prenom">Email</label>
<input id="prenom" type="text" class="form-control"
name="user[email]" value="{{$technicien->user->email}}" >
</div>
<div class="form-group">
<label for="prenom">Email</label>
<input id="prenom" type="text" class="form-control"
name="user[password]" value="{{$technicien->user->password}}" >
</div>
<div class="form-group">
<label for="">moyenne Avis</label>
<input type="text" name="technicien[moyenne_avis]"
class="form-control" value="{{$technicien->moyenne_avis}}" >
</div>
<div class="form-group">
<label for="">Etat Technicien</label>
<input type="text" name="technicien[actif]"
class="form-control" value="{{$technicien->actif}}" >
</div>
<div class="form-group">
<input type="submit" value="enregistrer" class="form-
control btn btn-primary">
</div>
</form>
</div>
</div>
</div>
#endsection
Try to do with One to One relation.
Make relation for user table and technician table, then try to do update.
Try this in controller
$user = User::with('technicien')->find($id);
$data['id'] = $id;
$data = $this->validate($request, [
'moyenne_avis' => 'required',
]);
$user->technicien()->whereUserId($data['id'])->update(['moyenne_avis' => $data['moyenne_avis']
]);
return redirect('/technicien')->with('Success', 'Records updated');
Also change form method as below in ur view.blade
<form action="{{ action('TechnicienController#update', $user->id) }}" method="post">
also, instead of {{ method_field('PATCH') }} use this <input name="_method" type="hidden" value="PATCH">
Note: Table name should be plural for controller and model name.
eg: Table name: users
Controller name: UserController
Model name: User
Make sure this same in urs too.

Data retrieval in CodeIgniter Form

I have successfully created a registration and login system in Code Igniter.
After registration all the user data is saved in database.
I have used email and password in the login form and I want to display all the details related to that logged user.
I have tried a few things but not getting the exact one. How should I proceed?
//Controller file
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class User extends CI_Controller{
function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
$this->load->library(array('session', 'form_validation', 'email', 'upload'));
$this->load->database();
$this->load->model('user_model');
}
public function index()
{
$this->load->view('registration');
}
public function registration()
{
//validate input value with form validation class of codeigniter
$this->form_validation->set_rules('fname', 'First Name', 'required|regex_match[/^[A-Za-z]{2,15}+$/]');
$this->form_validation->set_rules('lname', 'Last Name', 'required|regex_match[/^[A-Za-z]{2,15}+$/]');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email|is_unique[user.email]');
$this->form_validation->set_rules('phone', 'Phone Number', 'required|regex_match[/^[789]\d{9}$/]');
$this->form_validation->set_rules('city', 'City', 'required|regex_match[/^[A-Za-z]{3,15}+$/]');
$this->form_validation->set_rules('zip', 'Zip Code', 'required|regex_match[/^\d{6}$/]');
$this->form_validation->set_rules('gender', 'Gender', 'required');
$this->form_validation->set_rules('password', 'Password', 'required|min_length[6]|max_length[15]');
$this->form_validation->set_rules('confirmpswd', 'Password Confirmation', 'required|matches[password]');
$this->form_validation->set_rules('image', 'Image', 'callback_do_upload','required');
//$this->form_validation->set_message('is_unique', 'This %s is already exits');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('registration', array('error' => ' ' ));
}
else
{
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$city = $_POST['city'];
$zip = $_POST['zip'];
$gender = $_POST['gender'];
$password = $_POST['password'];
$passhash = hash('md5', $password);
$image = $_FILES['image']['name'];
//call method for uploading image
$upload = $this->do_upload('image');
//md5 hashing algorithm to decode and encode input password
//$salt = uniqid(rand(10,10000000),true);
$saltid = md5($email);
$data = array('fname' => $fname,
'lname' => $lname,
'email' => $email,
'phone' => $phone,
'city' => $city,
'zip' => $zip,
'gender' => $gender,
'password' => $passhash,
'image' => $image);
if($this->user_model->insertuser($data))
{
$this->session->set_flashdata('msg','<div class="alert alert-danger text-center"> Registration Successful</div>');
redirect(base_url().'user/login');
}
else
{
$this->session->set_flashdata('msg','<div class="alert alert-danger text-center">
Something Wrong. Please try again ...</div>');
redirect(base_url());
}
}
}
public function do_upload($data)
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$this->load->library('upload', $config);
/*$config['max_size'] = 1000000;
$config['max_width'] = 1024;
$config['max_height'] = 768;*/
//base_url('uploads/')
$this->upload->initialize($config);
if ( ! $this->upload->do_upload('image'))
{
$error = array('error' => $this->upload->display_errors());
}
else
{
$data = array('upload_data' => $this->upload->data());
}
return $data;
}
public function login()
{
$this->load->view('login');
}
public function check_login()
{
$email = $_POST['email'];
$pass = hash('md5', $_POST['password']);
$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
$this->form_validation->set_rules('password', 'Password', 'required|min_length[6]|max_length[15]');
if($this->form_validation->run() == FALSE)
{
$this->load->view('login');
}
else
{
$res = $this->user_model->check_user($email , $pass);
if(!empty($res))
{
echo "you are registered";
echo "<br>";
echo "<br>";
echo "your details are as follows: ";
echo "<br>";
echo "<br>";
echo "email:".$email;
echo "<br>";
}
else
{
$this->session->set_flashdata('msg','<div class="alert alert-danger text-center">email/password not found</div>');
redirect(base_url().'user/login');
}
}
}
function setSession($userId,$userName) {
$userSession = array('userId'=>$userId,
'userName'=>$userName,
'loggedIn'=>TRUE );
$this->session->set_userdata($userSession);
}
public function logout()
{
$this->session->sess_destroy();
redirect(base_url().'user/login', 'refresh');
}
}
//View File
<!DOCTYPE html>
<html lang="en">
<head>
<title>Registration</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<style type="text/css">
.form-box {
max-width: 500px;
position: relative;
margin: 5% auto;
}
</style>
</head>
<body>
<div class="wrapper">
<div class="container">
<div class="row">
<div class="form-box">
<div class="panel panel-primary">
<div class="panel-heading text-center">
<h3>Register</h3>
</div>
<?php
echo $this->session->flashdata('msg');
?>
<div class="panel-body">
<div class="row">
<div class="col-sm-12">
</div>
</div>
<form action="<?php echo base_url('user/registration');?>" method="post" enctype="multipart/form-data">
<div class="row">
<div class="col-sm-6">
<div class="form-group">
<label class="control-label" for="fname">First Name</label>
<div>
<input type="text" class="form-control" id="fname" name="fname" placeholder="First Name" required="">
<span class="text-danger"><?php echo form_error('fname'); ?></span>
</div>
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label class="control-label" for="fname">Last Name</label>
<div>
<input type="text" class="form-control" id="lname" name="lname" placeholder="Last Name" required="">
<span class="text-danger"><?php echo form_error('lname'); ?></span>
</div>
</div>
</div>
<div class="col-sm-12">
<div class="form-group">
<label class="control-label" for="email"> Email</label>
<div>
<input type="email" class="form-control" id="email" name="email" placeholder="Email" required="">
<span class="text-danger"><?php echo form_error('email'); ?></span>
</div>
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label class="control-label" for="phone">Phone Number</label>
<div>
<input type="text" class="form-control" id="phone" name="phone" maxlength="10" placeholder="Phone Number" required="">
<span class="text-danger"><?php echo form_error('phone'); ?></span>
</div>
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label class="control-label" for="city">City</label>
<div>
<input type="text" class="form-control" id="city" name="city" placeholder="City" required="">
<span class="text-danger"><?php echo form_error('city'); ?></span>
</div>
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label class="control-label" for="zip">Zip Code</label>
<div>
<input type="text" class="form-control" id="zip" name="zip" placeholder="Zip Code" required="">
<span class="text-danger"><?php echo form_error('zip'); ?></span>
</div>
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label class="control-label" for="gender">Gender</label>
<div>
<input type="radio" name="gender" value="male" id="gender" checked="true"> Male
<input type="radio" name="gender" value="female" id="gender"> Female
<input type="radio" name="gender" value="transgender" id="gender"> Transgender
<span class="text-danger"><?php echo form_error('gender'); ?></span>
</div>
</div>
</div>
<br>
<div class="col-sm-12">
<div class="form-group">
<label class="control-label" for="pswd">Password</label>
<div>
<input type="password" class="form-control" id="pswd" name="password" placeholder="Password" required="">
<span class="text-danger"><?php echo form_error('password'); ?></span>
</div>
</div>
</div>
<div class="col-sm-12">
<div class="form-group">
<label class="control-label" for="cn-pswd">Confirm Password</label>
<div>
<input type="password" class="form-control" id="cn-pswd" name="confirmpswd" placeholder="Confirm Password" required="">
<span class="text-danger"><?php echo form_error('confirmpswd'); ?></span>
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-sm-offset-5 col-sm-3 btn-submit">
<button type="submit" class="btn btn-success">Register</button>
</div>
</div>
</div>
<div class="col-sm-12">
<div class="form-group">
Select image to upload:<br>
<input type="file" name="image" id="image">
<br/><br/>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</body>
</html>
// Model file
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class User_model extends CI_Model
{
public function insertuser($data)
{
return $this->db->insert('candidates', $data);
}
public function verifyemail($key)
{
$this->db->where('md5(email)', $key);
return $this->db->update('candidates', $data);
}
public function check_user($email,$pass)
{
$sql = "SELECT id , fname FROM candidates where email = ? and password = ?";
$data = $this->db->query($sql, array($email,$pass));
return ($data->result_array()) ;
}
// Function To Fetch Selected Record
public function show_user_id($data){
$this->db->select('*');
$this->db->from('candidates');
$this->db->where('email', $data);
$q=$this->db->get('candidates');
return $q->row_array();
}
// Update Query For Selected Student
public function update_user_id1($id,$data){
$this->db->where('email', $id);
$this->db->update('candidates', $data);
}
}
?>
// While registering time your are using md5 encryption for password but while
// retrieving time you are passing password without encryption formate
$data = $this->db->query($sql, array($email,$pass));
// Use this in your model
$data = $this->db->query($sql, array($email,hash('md5', $pass)));

move() function not working in laravel 5.2 while editing/updating pictures

I have a founder page where the user can upload name, information and image of the founder.
In my view when I edit pictures and upload a new one, the name of the pictures gets stored in the database but the image is not being uploaded. There must be some problem in my controller but i can't seem to find out what. Any help would be appreciated.
Below are my model, controller and view.
Founder Model
class Founder extends Model
{
protected $fillable = ['name','information', 'image'];
public function getImageAttribute()
{
if (! $this->attributes['image']) {
return 'noimage.jpg';
}
return $this->attributes['image'];
}
public function getCreatedAtAttribute($date)
{
return Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $date)->format('Y-m-d');
}
public function getUpdatedAtAttribute($date)
{
return Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $date)->format('Y-m-d');
}
Founder Controller
public function update(Request $request, $id)
{
$founder = Founder::find($id);
$input = $request->all();
if($file=$request->file('image'))
{
$name = $file->getClientOriginalName();
$file->move('/images/', $name);
$input['image']= $name;
}
$founder->update($request->all());
return redirect()->route('founder.view');
}
Founder View Edit Form
<form class="form-horizontal" action="/founders/{{$founder->id}}" method="POST">
{{csrf_field()}}
<input type="hidden" name="_method" value="PUT">
<div class="form-group">
<label class="col-md-12">Name</label>
<div class="col-md-12">
<input type="text" name="name" class="form-control" value="{{$founder->name}}">
</div>
</div>
<div class="form-group">
<label class="col-md-12">Information</label>
<div class="col-md-12">
<textarea class="form-control" name="information" rows="3" value="{{$founder->information}}">{{$founder->information}}</textarea>
</div>
</div>
<div class="form-group">
<label class="col-md-12">Change Image</label>
<div class="col-md-12">
<input type="file" name="image" class="form-control" value="{{$founder->image}}">
</div>
</div>
<button type="submit" name="submit" class="btn btn-success waves-effect waves-light m-r-10">Submit</button>
</form>
Try to use rename:
rename ('current/path/to/foo', 'new/path/to/foo');
Documentation: http://php.net/rename
UPDATE:
Or use variable $destinationPath:
$file->move($destinationPath, $chooseYourFileName);
Use This Method This Will Work 100%
if($request->hasFile('filename')){
$file = $request->filename;
$file_new_name = $file->move("upload/posts/", $file->getClientOriginalName());
$post->filename = $file_new_names;
}
Remember filename is your < img name="filename" >

how to compare current and old password both are same or not before updation in DB using Codeigniter

I used this HTML for developing Form
<form method="post" action="<?php echo site_url('patients/change_patient_password'); ?>/<?php echo $this->session->userdata('patientDiseasesId'); ?>" class="form-horizontal">
<div class="form-body">
<div class="form-group">
<label class="col-md-3 control-label">Current Password</label>
<div class="col-md-4">
<input type="text" required class="form-control" name="password" placeholder="Current Password">
<?php $pass = form_error('password'); if(isset($pass)): ?><span class="help-block"><?php echo form_error('password'); ?></span><?php endif; ?>
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label">New Password </label>
<div class="col-md-4">
<input type="text" required class="form-control" name="password_confirm" placeholder="New Password">
<?php $pass_confirm = form_error('password_confirm'); if(isset($pass_confirm)): ?><span class="help-block"><?php echo form_error('password_confirm'); ?></span><?php endif; ?>
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label">ReType New Password</label>
<div class="col-md-4">
<input type="text" required class="form-control" name="password_confirm2" placeholder="ReType New Password">
<?php $pass_confirm2 = form_error('password_confirm2'); if(isset($pass_confirm2)): ?><span class="help-block"><?php echo form_error('password_confirm2'); ?></span><?php endif; ?>
</div>
</div>
</div>
<div class="form-actions">
<div class="row">
<div class="col-md-offset-3 col-md-9">
<button type="submit" class="btn green">Update Password</button>
<button type="button" class="btn default">Cancel</button>
</div>
</div>
</div>
</form>
also please have a look my code controller.
public function change_patient_password($patientId){
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
$this->form_validation->set_rules('password', 'Password', 'required|is_unique[patients.password]');
$this->form_validation->set_rules('password_confirm', 'New Password', 'required|matches[password_confirm2]');
$this->form_validation->set_rules('password_confirm2', 'ReType New Password', 'required');
if ($this->form_validation->run() == FALSE)
{
$this->change_patient_password_form();
}
else
{
$this->load->model('patients_model');
$data['password_updated'] = $this->patients_model->change_patient_password_model($patientId);
if($data['password_updated']==true){
$data['success']=true;
$data['main_content']= 'change_patient_password_form';
$this->load->view('includes/patient_template',$data);
}else{
$data['error']=true;
$data['main_content']= 'change_patient_password_form';
$this->load->view('includes/patient_template',$data);
}
}
here is the final model code
public function change_patient_password_model($patientId){
$data = array(
'password' => md5($this->input->post('password_confirm'))
);
$this->db->where('patientDiseasesId', $patientId);
return $this->db->update('patients', $data);
}
I see you're using "is_unique" in your form validation. Will each password be unique?
How I would do this, is to query the DB before we update it, and check if the passwords match. Like so.
public function change_patient_password_model($patientId)
{
// Get this users info
$query = $this->db->get_where('patients', array('patientDiseasesId' => $patientId));
if($query->num_rows() > 0)
{
// We have the patient, so check if the passwords match
$current_password = $query->row('password');
$inputted_password = md5($this->input->post('password_confirm'));
if( $current_password != $inputted_password)
{
// They are not the same, so update it...
$data['password'] = $inputted_password;
$this->db->where('patientDiseasesId', $patientId);
return $this->db->update('patients', $data);
}
else
{
// They are the same...
return false;
}
}
else
{
// No patients with this ID
return false;
}
}
I hope this helps.

Resources