My user profile picture registration is not working on CodeIgniter - php

I made a user registration form on the CodeIgniter framework so users can registrate to my website. Now the only thing that doesn't work is that I cant upload a profile picture. When I click on the register button I'm getting 2 errors. I want the profile picture to be uploaded in the product_foto column.
This is my view file: (register.php) :
<form action="" method="POST" enctype='multipart/form-data'>
<div class="form-group">
<label for="voornaam">Voornaam</label>
<input class="form-control" name="voornaam" id="voornaam" type="text">
</div>
<div class="form-group">
<label for="achternaam">Achternaam</label>
<input class="form-control" name="achternaam" id="achternaam" type="text">
</div>
<div class="form-group">
<label for="achternaam">Straat en huisnummer</label>
<input class="form-control" name="straat" id="straat" type="text">
</div>
<div class="form-group">
<input class="form-control" name="huisnummer" id="huisnummer" type="text">
</div>
<div class="form-group">
<label for="huisnummer">Huisnummer</label>
<input class="form-control" name="huisnummer" id="huisnummer">
</div>
<div class="form-group">
<label for="postcode">Postcode</label>
<input class="form-control" name="postcode" id="postcode">
</div>
<div class="form-group">
<label for="woonplaats">Woonplaats</label>
<input class="form-control" name="woonplaats" id="woonplaats">
</div>
<div class="form-group">
<label for="email">Email adres</label>
<input class="form-control" name="email" id="email" type="emai">
</div>
<div class="form-group">
<label for="wachtwoord">Wachtwoord</label>
<input class="form-control" name="wachtwoord" id="wachtwoord" type="password">
</div>
<div class="form-group">
<label for="wachtwoord">Herhaal wachtwoord</label>
<input class="form-control" name="wachtwoord2" id="wachtwoord" type="password">
</div>
<div class="form-group">
<label for="profiel_foto">Profiel foto</label>
<input class="form-control" type="file" name="profiel_foto" id="profiel_foto">
</div>
<div class="form-group">
<label for="beschrijving">Beschrijving</label>
<input class="form-control" name="beschrijving" id="beschrijving">
</div>
<div class="form-group">
<label for="geboortedatum">Geboortedatum</label>
<input class="form-control" name="geboortedatum" id="geboortedatum" type="date">
</div>
<div class="form-group">
<label for="geslacht">Geslacht</label>
<select class="form-control" id="geslacht" name="geslacht">
<option value="Man">Man</option>
<option value="Vrouw">Vrouw</option>
</select>
</div>
<div>
<button class="btn btn-primary" name="register">Registreren</button>
</div>
</form>
This is the register code in the controller:
public function register() {
$config['upload_path'] = './upload/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$this->load->library('upload', $config);
$this->input->post('profiel_foto');
$data_upload_files = $this->upload->data();
$image = $data_upload_files['./upload/'];
//voeg gebruiker toe aan database
$data = array(
'voornaam' => $_POST['voornaam'],
'achternaam' => $_POST['achternaam'],
'email' => $_POST['email'],
'wachtwoord' => ($_POST['wachtwoord']),
'startdatum' => date('Y-m-d'),
'postcode' => $_POST['postcode'],
'huisnummer' => $_POST['huisnummer'],
'woonplaats' => $_POST['woonplaats'],
'beschrijving' => $_POST['beschrijving'],
'geboortedatum' => $_POST['geboortedatum'],
'geslacht' => $_POST['geslacht'],
'profiel_foto' => $image
);
$this->db->insert('users', $data);
$this->session->set_flashdata("success", "Uw account is nu geregistreerd, u kunt nu inloggen");
redirect("auth/register", "refresh");
}
And these are the 2 errors I'm getting when I'm trying to registrate:
PHP Error was encountered
Severity: Notice
Message: Undefined index: ./upload/
Filename: controllers/Auth.php
Line Number: 131
Backtrace:
File: /home/ubuntu/workspace/application/controllers/Auth.php
Line: 131
Function: _error_handler
File: /home/ubuntu/workspace/index.php
Line: 315
Function: require_once
A Database Error Occurred
Error Number: 1048
Column 'profiel_foto' cannot be null
INSERT INTO `users` (`voornaam`, `achternaam`, `email`, `wachtwoord`, `startdatum`, `postcode`, `huisnummer`, `woonplaats`, `beschrijving`, `geboortedatum`, `geslacht`, `profiel_foto`) VALUES ('hallo', 'hallo', 'hallo#gmail.com', 'hallo', '2017-06-28', 'hallo', 'hallo', 'hallo', 'hallo', '2017-06-10', 'Man', NULL)
Filename: controllers/Auth.php
Line Number: 149

Your First error is being caused by:
$image = $data_upload_files['./upload/'];
As stated './upload/' is not a valid index nor is an existing one in that array. It's not clear where you have defined that.
Next: What does this line do?
$this->input->post('profiel_foto');
Next: Debug: Inspect your array $data prior to the insert and check it.
// Snip of other code
$data = array(
'voornaam' => $_POST['voornaam'],
'achternaam' => $_POST['achternaam'],
'email' => $_POST['email'],
'wachtwoord' => ($_POST['wachtwoord']),
'startdatum' => date('Y-m-d'),
'postcode' => $_POST['postcode'],
'huisnummer' => $_POST['huisnummer'],
'woonplaats' => $_POST['woonplaats'],
'beschrijving' => $_POST['beschrijving'],
'geboortedatum' => $_POST['geboortedatum'],
'geslacht' => $_POST['geslacht'],
'profiel_foto' => $image
);
$this->db->insert('users', $data);
var_dump($data);
exit();
// Snip of other code
And read the user guide as I stated above in my comments... Cause I aint writing that out here for you... That should get you started.

Related

upload more files to a folder with laravel

I try to upload multiple files to an specific folder using laravel. I was able to do this with just one file. But after I tried to upload multiple files it didn't work. First of all I tried to make an foreach. But it didn't work I tried to find solutions on the internet but unfortunately I didn't find one. Can anybody help me?
public function store(Request $request) {
// dd(request()->all());
$this->validate(request(), [
'name' => 'required|unique:lessons|max:20',
'type' => 'required',
'description' => 'required',
'price' => 'required',
'main_picture' => 'required',
'pictures' => 'required'
]);
$input = $request->all();
$images= array();
if($files=$request->file('images')){
foreach($files as $file){
$name=$file->getClientOriginalName();
$file->move('lesson images',$name);
$images[]=$name;
}
}
$lesson = Lesson::create( [
'name' =>$input['name'],
'type' => $input['type'],
'description' =>$input['description'],
'price' => $input['price'],
'main_picture' =>$input['main_picture'],
'pictures'=> implode("|",$images),
]);
$image_name = $request->file('main_picture')->getClientOriginalName();
$id = $lesson->id;
if($request->hasFile('main_picture')) {
$file = $request->file('main_picture');
$file->move('lesson images', $id . "-main-" . $image_name);
}
return redirect('/lessenoverzicht');
}
HTML:
<form method="post" enctype="multipart/form-data" action="/lessenoverzicht/toevoegen">
{{ csrf_field() }}
<div class="form-group">
<label for="name"><span class='required'>*</span>Naam les:</label>
<input type="text" class="form-control" id="name" name="name" placeholder="Voer naam les in..">
</div>
<div class="form-group">
<label for="exampleFormControlSelect1"><span class='required'>*</span>Type</label>
<select name='type' class="form-control" id="type">
<option value="" disabled selected>Select your option</option>
<option value='les'>Les</option>
<option value='workshop'>Workshop</option>
</select>
</div>
<div class="form-group">
<label for="description"><span class='required'>*</span>Omschrijving:</label>
<textarea class="form-control" id="description" name="description" rows="10" placeholder="Voer omschrijving in..."></textarea>
</div>
<label for="description"><span class='required'>*</span>Prijs:</label>
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text" id="basic-addon1">€</span>
</div>
<input type="number" value="0.00" min="0" step="1.00" class="form-control" id='price' name='price'>
</div>
<div class="form-group">
<label for="main_picture"><span class='required'>*</span>Main Image:</label>
<input type="file" class='form-control' id='main_picture' name="main_picture">
</div>
<div class="form-group">
<label for="picture">Afbeelding(en) omschrijving:</label>
<input type="file" class='form-control' id='picture' name="pictures" multiple>
</div>
<div class="form-group">
#include('layouts.errors')
</div>
<button type="submit" class="btn btn-primary">Toevoegen</button>
</form>
Can you try the below change in your code? I have only added Array type name property name="pictures[]"
<input type="file" class='form-control' id='picture' name="pictures[]" multiple>
Sample Code:
<form class="form-horizontal" enctype="multipart/form-data" method="post" action="/details">
and this for multiple selection:
<input required type="file" class="form-control" name="images[]" placeholder="address" multiple>
PHP logic
public function store(request $request) {
$input=$request->all();
$images=array();
if($files=$request->file('images')){
foreach($files as $file){
$name=$file->getClientOriginalName();
$file->move('image',$name);
$images[]=$name;
}
}
/*Insert your data*/
Detail::insert( [
'images'=> implode("|",$images),
'description' =>$input['description'],
//you can put other insertion here
]);
return redirect('redirecting page');
}

How can a user upload a profile picture on my codeigniter registration form

I made a user registration form on the CodeIgniter framework so users can registrate to my website. Now the only thing that doesn't work is that I cant upload a profile picture. When I click on the register button I'm getting 2 errors. I want the profile picture to be uploaded in the product_foto column.
This is my view file: (register.php) :
<form action="" method="POST" enctype='multipart/form-data'>
<div class="form-group">
<label for="voornaam" >Voornaam</label>
<input class="form-control" name="voornaam" id="voornaam" type="text">
</div>
<div class="form-group">
<label for="achternaam">Achternaam</label>
<input class="form-control" name="achternaam" id="achternaam" type="text">
</div>
<div class="form-group">
<label for="achternaam">Straat en huisnummer</label>
<input class="form-control" name="straat" id="straat" type="text">
</div>
<div class="form-group">
<input class="form-control" name="huisnummer" id="huisnummer" type="text">
</div>
<div class="form-group">
<label for="huisnummer">Huisnummer</label>
<input class="form-control" name="huisnummer" id="huisnummer">
</div>
<div class="form-group">
<label for="postcode" >Postcode</label>
<input class="form-control" name="postcode" id="postcode">
</div>
<div class="form-group">
<label for="woonplaats" >Woonplaats</label>
<input class="form-control" name="woonplaats" id="woonplaats">
</div>
<div class="form-group">
<label for="email" >Email adres</label>
<input class="form-control" name="email" id="email" type="emai">
</div>
<div class="form-group">
<label for="wachtwoord" >Wachtwoord</label>
<input class="form-control" name="wachtwoord" id="wachtwoord" type="password">
</div>
<div class="form-group">
<label for="wachtwoord">Herhaal wachtwoord</label>
<input class="form-control" name="wachtwoord2" id="wachtwoord" type="password">
</div>
<div class="form-group">
<label for="profiel_foto">Profiel foto</label>
<input class="form-control" type="file" name="profiel_foto" id="profiel_foto">
</div>
<div class="form-group">
<label for="beschrijving">Beschrijving</label>
<input class="form-control" name="beschrijving" id="beschrijving">
</div>
<div class="form-group">
<label for="geboortedatum" >Geboortedatum</label>
<input class="form-control" name="geboortedatum" id="geboortedatum" type="date">
</div>
<div class="form-group">
<label for="geslacht" >Geslacht</label>
<select class="form-control" id="geslacht" name="geslacht">
<option value="Man">Man</option>
<option value="Vrouw">Vrouw</option>
</select>
</div>
<div>
<button class="btn btn-primary" name="register" >Registreren</button>
</div>
</form>
This is the register code in the controller:
public function register()
{
$config['upload_path'] = './upload/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$this->load->library('upload', $config);
$this->input->post('profiel_foto');
$data_upload_files = $this->upload->data();
$image = $data_upload_files['./upload/'];
//voeg gebruiker toe aan database
$data = array (
'voornaam'=>$_POST['voornaam'],
'achternaam'=>$_POST['achternaam'],
'email'=>$_POST['email'],
'wachtwoord'=> ($_POST['wachtwoord']),
'startdatum'=>date('Y-m-d'),
'postcode'=>$_POST['postcode'],
'huisnummer'=>$_POST['huisnummer'],
'woonplaats'=>$_POST['woonplaats'],
'beschrijving'=>$_POST['beschrijving'],
'geboortedatum'=>$_POST['geboortedatum'],
'geslacht'=>$_POST['geslacht'],
'profiel_foto'=>$image
);
$this->db->insert('users',$data);
$this->session->set_flashdata("success", "Uw account is nu geregistreerd, u kunt nu inloggen");
redirect("auth/register", "refresh");
}
}
And these are the 2 errors I'm getting when I'm trying to registrate:
A PHP Error was encountered
Severity: Notice
Message: Undefined index: ./upload/
Filename: controllers/Auth.php
Line Number: 131
Backtrace:
File: /home/ubuntu/workspace/application/controllers/Auth.php
Line: 131
Function: _error_handler
File: /home/ubuntu/workspace/index.php
Line: 315
Function: require_once
A Database Error Occurred
Error Number: 1048
Column 'profiel_foto' cannot be null
INSERT INTO `users` (`voornaam`, `achternaam`, `email`, `wachtwoord`, `startdatum`, `postcode`, `huisnummer`, `woonplaats`, `beschrijving`, `geboortedatum`, `geslacht`, `profiel_foto`) VALUES ('hallo', 'hallo', 'hallo#gmail.com', 'hallo', '2017-06-28', 'hallo', 'hallo', 'hallo', 'hallo', '2017-06-10', 'Man', NULL)
Filename: controllers/Auth.php
Line Number: 149
I modified your code. Try this
public function register() {
$data = array();
$config = array(
'upload_path' => 'upload',
'allowed_types' => 'gif|jpg|png|jpeg',
);
$this->load->library('upload', $config);
if (!$this->upload->do_upload('profiel_foto')) {
$error = array('error' => $this->upload->display_errors());
// var_dump( $error); die; check errors
} else {
$fileName = $this->upload->data();
$data['profiel_foto'] = $fileName['file_name'];
}
// voeg gebruiker toe aan database
$data = array (
'voornaam'=>$_POST['voornaam'],
'achternaam'=>$_POST['achternaam'],
'email'=>$_POST['email'],
'wachtwoord'=> ($_POST['wachtwoord']),
'startdatum'=>date('Y-m-d'),
'postcode'=>$_POST['postcode'],
'huisnummer'=>$_POST['huisnummer'],
'woonplaats'=>$_POST['woonplaats'],
'beschrijving'=>$_POST['beschrijving'],
'geboortedatum'=>$_POST['geboortedatum'],
'geslacht'=>$_POST['geslacht'],
);
$this->db->insert('users', $data);
$this->session->set_flashdata("success", "Uw account is nu geregistreerd, u kunt nu inloggen");
redirect("auth/register", "refresh");
}
replace
$config['upload_path'] = './upload/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$this->load->library('upload', $config);
$this->input->post('profiel_foto');
$data_upload_files = $this->upload->data();
$image = $data_upload_files['./upload/'];
With
$target_dir = "upload/";
$target_file = $target_dir . time().basename($_FILES["profiel_foto"]["name"]);
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
$imgName = time().basename($_FILES["profiel_foto"]["name"]);
move_uploaded_file($_FILES["profiel_foto"]["tmp_name"], $target_file);
Your insert function
$data = array (
'voornaam'=>$_POST['voornaam'],
'achternaam'=>$_POST['achternaam'],
'email'=>$_POST['email'],
'wachtwoord'=> ($_POST['wachtwoord']),
'startdatum'=>date('Y-m-d'),
'postcode'=>$_POST['postcode'],
'huisnummer'=>$_POST['huisnummer'],
'woonplaats'=>$_POST['woonplaats'],
'beschrijving'=>$_POST['beschrijving'],
'geboortedatum'=>$_POST['geboortedatum'],
'geslacht'=>$_POST['geslacht'],
'profiel_foto'=>$imgName
);
$this->db->insert('users',$data);

codeigniter database error number 1064 on update data

I want to update user data in the Code-igniter (user details)
below is my code for more checking . please check it out why I get error when i push "update" button.
note: I used some of these information in my view that I didn't copied theme for now (no need in-fact )
controller:
public function edit(){
//validation form
$this->form_validation->set_rules('first_name', 'First Name', 'trim|required|min_length[4]|xss_clean');
$this->form_validation->set_rules('last_name', 'Last Name', 'trim|required|min_length[4]|xss_clean');
$this->form_validation->set_rules('email','Email','trim|required|valid_email|is_unique[users.email]');
$this->form_validation->set_rules('username','Username','trim|required|min_length[3]|max_length[16]');
$this->form_validation->set_rules('password','Password','trim|min_length[3]|max_length[50]');
$this->form_validation->set_rules('password2','Confirm Password','trim|matches[password]');
$this->form_validation->set_rules('cellphone','Cellphone Number','trim|required');
$this->form_validation->set_rules('activation_code','Activation Code','trim');
//$data['groups'] = $this->User_model->get_groups();
//$data['user'] = $this->User_model->get_user($uid);
if($this->form_validation->run() == FALSE) {
//view
$data = array(
'userid' => $this->session->userdata('user_id')->id,
'first_name' => $this->session->userdata('user_id')->first_name,
'last_name' => $this->session->userdata('user_id')->last_name,
'fathers_name' => $this->session->userdata('user_id')->fathers_name,
'id_card_number' => $this->session->userdata('user_id')->id_card_number,
'national_number' => $this->session->userdata('user_id')->national_code,
'national_code' => $this->session->userdata('user_id')->national_code,
'age' => $this->session->userdata('user_id')->age,
'gender' => $this->session->userdata('user_id')->gender,
'is_addict' => $this->session->userdata('user_id')->is_addict,
'hiv_status' => $this->session->userdata('user_id')->hiv_status,
'hepatitis_status' => $this->session->userdata('user_id')->hepatitis_status,
'email' => $this->session->userdata('user_id')->email,
'needed_services' => $this->session->userdata('user_id')->needed_services,
'username' => $this->session->userdata('user_id')->username,
);
//$data['last_name'] = $this->session->userdata('user_id')->last_name;
//$data['data'] = $this->Profile_Model->get_user_profile();
//$this->base->set_message($msg,$type);
redirect(site_url('admin/profile'),'refresh');
$data['courses'] = $this->Profile_Model->get_user_profile();
//load view
$data['main_content'] = 'profile/index';
$this->load->view('profile/layouts/main', $data);
}else{
//create articles data array
$data = array(
'first_name' => $this->input->post('first_name'),
'last_name' => $this->input->post('last_name'),
'username' => $this->input->post('username'),
'email' => $this->input->post('email'),
'cellphone' => $this->input->post('cellphone'),
'group_id' => 2,
);
if($this->input->post('user_id') == $this->session->userdata('user_id')->id){
$uid = $this->input->post('user_id');
}
$password = $this->input->post('password');
$password2 = $this->input->post('password2');
if($password != '' && $password = $password2 ){
$data['password'] = md5($this->input->post('password'));
}
if($this->input->post('activation_code') !=''){
$data['activation_code'] = $this->input->post('activation_code');
}
$this->Profile_Model->update($data, $uid);
//Create Message
$this->session->set_flashdata('User_saved','You have successfully registered');
//redirect to page
redirect('admin/profile');
}
}
also below is my model snippet code:
model:
public function update($data, $uid)
{
$this->db->where('id', $uid);
$result = $this->db->update($this->table_users, $data);
return $result;
}
and below is my view form:
view:
<?php echo form_open(base_url('admin/profile/edit'),'class="form-horizontal"');?>
<?php $uid = $this->session->userdata('user_id')->id;
$uid = intval($uid);
?>
<div class="form-group">
<label class="col-sm-1 control-label" for="first_name">نام</label>
<input type="hidden" name="user_id" value="<?php echo $uid;?>">
<div class="col-sm-5">
<input type="text" id="first_name" name="first_name" class="form-control" value="<?php echo set_value('first_name',$first_name);?>">
</div>
<label class="col-sm-1 control-label" for="last_name">نام خانوادگی</label>
<div class="col-sm-5">
<input type="text" id="last_name" name="last_name" class="form-control" value="<?php echo set_value('last_name',$last_name);?>">
</div>
</div>
<div class="form-group">
<label class="col-sm-1 control-label" for="email">ایمیل</label>
<div class="col-sm-5">
<input type="email" id="email" name="email" class="form-control" value="<?php /** #var dbObject $email */
echo set_value('email',$email);?>">
</div>
<label class="col-sm-1 control-label" for="username">نام کاربری</label>
<div class="col-sm-5">
<input type="text" class="form-control" value="<?php echo $username;?>" disabled aria-disabled="true" title="You cant't change your username" />
</div>
</div>
<div class="form-group">
<label class="col-sm-1 control-label" for="password">رمز ورود</label>
<div class="col-sm-5">
<input type="password" id="password" name="password" class="form-control">
</div>
<label class="col-sm-1 control-label" for="password2">تایید رمز</label>
<div class="col-sm-5">
<input type="password" id="password2" name="password2" class="form-control">
</div>
</div>
<div class="form-group">
<label class="col-sm-1 control-label" for="cellphone">شماره تلفن همراه</label>
<div class="col-sm-5">
<input type="tel" id="cellphone" name="cellphone" class="form-control">
</div>
<label class="col-sm-1 control-label" for="cellphone_activation">کد تایید پیامکی</label>
<div class="col-sm-5">
<input type="text" id="cellphone_activation" name="cellphone_activation" class="form-control">
</div>
</div>
<div class="form-group">
<div class="col-sm-5 col-sm-offset-1">
<p>
<button class="btn mybtn btn-custom" type="submit">ویرایش</button>
</p>
</div>
</div>
<?php echo form_close();?>
Model Should be like this write table name instead of $this->table_users
public function update($data, $uid)
{
$this->db->where('id', $uid);
$result = $this->db->update(user, $data);
return $result;
}

Can not upload image in the folder using Codeigniter

I am trying to upload image to the folder on the server and store another data in the databse from the same form simultaneously. But it is showing error You did not select a file to upload.
Below is my view and controller for the same:
View:
<form role="form" method="post" action="job_insert" enctype="multipart/form-data">
<div class="box-body">
<div class="form-group">
<label>Job Name*</label>
<input type="text" class="form-control" name="jname" placeholder="Enter Job Name" required>
</div>
<div class="form-group">
<label>Description</label>
<textarea rows="4" cols="10" class="form-control" name="description" required></textarea>
</div>
<div class="form-group">
<label>File input</label>
<input type="file" name="userimage">
</div>
<div class="form-group">
<label>Paper Size*</label>
<div class="form-group">
<label>Paper Cutting Size</label>
<div>
<input id="cutting_size" name="cutting_size" type="text" placeholder="Cutting Size" class="form-control input-md" required>
</div>
</div>
<div class="form-group">
<label>Sheet</label>
<div>
<input id="sheet" name="sheet" type="text" placeholder="No. of Sheet" class="form-control input-md" required>
</div>
</div>
<div class="form-group">
<label>Tin No.</label>
<div>
<input id="sheet" name="tin" type="text" placeholder="Tin No." class="form-control input-md" required>
</div>
</div>
</div>
<!-- /.box-body -->
<div style="margin-left: 600px" class="box-footer">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</form>
Controller:
public function job_insert()
{
$this->form_validation->set_rules('jname', 'job_name', 'trim');
if($this->form_validation->run()== FALSE)
{
$this->load->view('invalid_jobname');
}
else
{
$this->do_upload();
$data = $_SESSION['email'];
$id = $this->user_model->get_client_id($data);
$data = array('cid' => $id,
'job_name' => $this->input->post("jname"),
'job_id' => $this->input->post("job_id"),
'description' => $this->input->post("description"),
'img_name' => $img_name,
'adate' => Date('Y-m-d'),
'status' => 'NEW',
'paper_size' => $this->input->post("paper_size"),
'paper_type' => $this->input->post("paper_type"),
'cutting_size'=> $this->input->post("cutting_size"),
'sheet'=> $this->input->post("sheet"),
'tin' => $this->input->post("tin"),
'lamination' => $this->input->post("checkboxes"),
'print_type' => $this->input->post("checkboxes1"),
'binding' => $this->input->post("checkboxes2"),
);
$query = $this->user_model->job_insert($data);
if ($query == TRUE)
{
$this->load->view('job_submitted');
}
else
{
$this->load->view('error_page');
}
}
}
Here's my do_upload function:
public function do_upload()
{
$config = array(
'upload_path' => "./uploads/",
'allowed_types' => "gif|jpg|png|jpeg|pdf",
'overwrite' => FALSE,
'max_size' => "2048000", // Can be set to particular file size , here it is 2 MB(2048 Kb)
'max_height' => "768",
'max_width' => "1024"
);
$this->load->library('upload', $config);
$image = $this->input->post("image");
echo $image;
if(!$this->upload->do_upload('userimage'))
{
$error = $this->upload->display_errors();
echo $error;
}
}
Change this to
<input type="file" name="image">
to
<input type="file" name="userfile"/>
and in do_upload
if(!$this->upload->do_upload())
to
if ( ! $this->upload->do_upload('userfile'))
Provide enctype="multipart/form-data" in your form tag. Without it you cannot upload image.
update form tag with below code
<form role="form" method="post" action="job_insert" enctype="multipart/form-data">

Update table row using codeigniter and save it in to database

Clicking on edit icon filled form should open , and proced to update
Controller code
public function update_menus( $id ) {
$data = array(
'menu_title' => $this->input->post('menu_title'),
'filepath' => $this->input->post('filepath'),
'description' => $this->input->post('description'),
'left_content' => $this->input->post('left_content'),
'right_content' => $this->input->post('right_content'),
'url_path' => $this->input->post('url_path'),
'created_date' => date('Y-m-d H:i:s')
);
if ($this->login_database->update($id, $data) == TRUE) {
$this->session->set_flashdata('flash_message', 'updated');
} else {
$this->session->set_flashdata('flash_message', 'not_updated');
}
$this->load->view('update_menus');
$data = array();
$data['menus'] = $this->login_database->get_menus_by_id($id);
$this->load->view('menu_details', $data);
}
Model code
public function update($id)
{
$data = array(
'menu_title'=> $this->input->post('menu_title'),
'filepath' => $this->input->post('filepath'),
'description' => $this->input->post('description'),
'left_content' => $this->input->post('left_content'),
'right_content' => $this->input->post('right_content'),
'url_path' => $this->input->post('url_path'),
'created_date' =>date('Y-m-d H:i:s')
);
$this->db->where('id',$id);
$menus = $this->db->update('menu_details', $data);
return $menus->result();
}
view code
<?php
echo form_open('user_authentication/update_menu_submit');
?><?php foreach ($menus->result() as $row) {
?>
<div class="box-body">
<div class="form-group">
<label for="exampleInputEmail1">Menu Title</label>
<input type="text" name="menu_title" class="form-control" id="exampleInputTitle" placeholder="Enter Menu Title">
</div>
<div class="form-group">
<label for="exampleInputPassword1">Menu Url</label>
<input type="text" name="url_path" class="form-control" id="exampleInputurl" placeholder="Enter File Path">
</div>
<div class="form-group">
<label for="exampleInputFile">Upload Image</label>
<input type="file" name="filepath" id="exampleInputFile">
</div>
<div class="form-group">
<label for="exampleInputDesc">Description</label>
<textarea id="editor4" name="description" rows="10" cols="80" placeholder="Enter Description"></textarea>
</div>
<div class="form-group">
<label for="exampleInputcontl">Left Content</label>
<textarea id="editor5" name="left_content" rows="10" cols="80" placeholder="Enter Left Content"></textarea>
</div>
<div class="form-group">
<label for="exampleInputcontr">Right Content</label>
<textarea id="editor3" name="right_content" rows="10" cols="80" placeholder="Enter Right Content"></textarea>
</div>
<div class="box-footer">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
<?php
}
echo form_close();
?>
</div>
Once click on edit action it should redirect to view page and should open filled form, Its showing error like
Fatal error: Call to a member function result() on boolean in
C:\xampp\htdocs\codecms\application\models\login_database.php on line
205 in model code,
can you just check the generated query using :
$this- db- last_query();
$data = array(
'menu_title'=> $this->input->post('menu_title'),
'filepath' => $this->input->post('filepath'),
'description' => $this->input->post('description'),
'left_content' => $this->input->post('left_content'),
'right_content' => $this->input->post('right_content'),
'url_path' => $this->input->post('url_path'),
'created_date' =>date('Y-m-d H:i:s')
);
$this->db->where('id',$id);
$menus = $this->db->update('menu_details', $data);
$this->db->last_query();
Check the generated query in your php admin .
is return $menus->result(); required ?

Categories