I have a problem when I want to update the image.
the update process went well, but the image cannot update.
Please help. Thanks.
Here is View :
<?php echo form_open('update'); ?>
<input type="hidden" name="id" value="<?php echo $data->id; ?>">
<input type="hidden" id="old" name="old" value="<?php echo $data->image;?>">
<div class="form-group">
<label>Name</label>
<input type="text" class="form-control" name="name" value="<?php echo $data->name; ?>">
</div>
<div class="form-group">
<label>Change Image</label>
<input class="form-control" type="file" name="image">
</div>
<button type="submit" class="btn btn-default">Submit</button>
Here is Controller :
public function Update(){
$id = $this->input->post('id');
$name = $this->input->post('name');
if($_FILES['image']['name']!=""){
$config['upload_path'] = './image/';
$config['allowed_types'] ='gif|jpg|png|jpeg|';
$this->load->library('upload', $config);
if(!$this->upload->do_upload('image')){
$error = array('error' => $this->upload->display_errors());
}else{
$upload_data=$this->upload->data();
$image_name =$upload_data['file_name'];
}
}else{
$image_name=$this->input->post('old');
}
$data = array( 'name' => $name, 'image' => $image_name, );
$this->model_user->update_user($data,$id);
}
Here is Model :
public function update_user($data, $id)
{
$this->db->where('id', $id);
$this->db->update('tabel_user', $data);
return TRUE;
}
you code dose not have any mistake. but you use some unwanted input.
try to debug your code.
in model
before return
public function update_user($data, $id)
{
$this->db->where('id', $id);
$this->db->update('tabel_user', $data);
echo $this->db->last_query();
echo "</br> id : ".$id;
print_r($data); EXIT;
}
if you get get query printed. check what you have missed. if you don't
at controller
after line $error = array('error' => $this->upload->display_errors());
put print_r($error); exit;
and may be you have forgotten to mention multi part at your view.
do these three things hope you will find what is wrong.
First, check your column names are same as array key values
Try These,
public function update_user($data, $id)
{
echo'<pre>';print_r($data);
$this->db->where('id', $id);
$flag = $this->db->update('tabel_user', $data);
echo $flag;die;
}
$flag will tell you True or False
Related
I put php script in my html (I use codeigniter), but when I show up in localhost, my php script eliminate all tag.
I want to put input text box for edit/update something and to show the previous text in my text box. But when I use php script, the element (input form and button) in tag php is missing in localhost.
<form method="post" action="<?php echo base_url('dashboard/categories_update'); ?>">
<div class="box-body">
<div class="form-group">
<label>Categories Name</label>
<?php foreach ($kategori as $k) { ?>
<input
type="hidden" name="id"
value="<?php echo base_url().$k->kategori_id; ?>"
>
<input
type="text" name="categories"
value="<?php echo base_url().$k->kategori_nama; ?>" placeholder="Type here. . . "
>
<?php } ?>
</div>
</div>
<div class="box-footer">
<input type="submit" class="btn btn-success" value="update">
</div>
</form>
This my controller code :
public function categories()
{
$this->load->model('m_data');
$data['kategori'] = $this->m_data->get_data('kategori')->result();
$this->load->view('dashboard/v_header');
$this->load->view('dashboard/v_categories',$data);
$this->load->view('dashboard/v_footer');
}
public function add_categories()
{
$this->load->view('dashboard/v_header');
$this->load->view('dashboard/v_categories_add');
$this->load->view('dashboard/v_footer');
}
public function categories_action()
{
$this->form_validation->set_rules('categories','Categories','required');
if ($this->form_validation->run() !=false) {
$categories = $this->input->post('categories');
$data = array(
'kategori_nama' => $categories,
'kategori_slug' => strtolower(url_title($categories))
);
$this->load->model('m_data');
$this->m_data->insert_data($data,'kategori');
redirect(base_url().'dashboard/categories');
} else {
$this->load->view('dashboard/v_header');
$this->load->view('dashboard/v_categories_add');
$this->load->view('dashboard/v_footer');
}
}
public function categories_edit()
{
$id = $this->input->post('id');
$where = array(
'kategori_id' => $id
);
$this->load->model('m_data');
$data['kategori']= $this->m_data->edit_data($where,'kategori')->result();
$this->load->view('dashboard/v_header');
$this->load->view('dashboard/v_categories_edit',$data);
$this->load->view('dashboard/v_footer');
}
public function categories_update()
{
$this->form_validation->set_rules('categories','Categories','required');
if ($this->form_validation->run() != false) {
$id = $this->input->post('id');
$kategori = $this->input->post('categories');
$where = array(
'kategori_id' => $id
);
$data = array (
'kategori_nama' => $kategori,
'kategori_slug' => strtolower(url_title($kategori))
);
$this->load->model('m_data');
$this->m_data->update_data($where,$data,'kategori');
redirect(base_url().'dashboard/categories');
}
}
there's incorrect script in my controller,
before :
public function categories_edit() {
$id = $this->input->post('id');
it should be :
public function categories_edit($id)
and now all my script is working ! thankyou guys, especially bro #RajendraSingh !
I'm trying create an edit profile function for a user. When the user submits the form, it updates the data in the database. But when I try to access the edit profile page I get this error:
Type: ArgumentCountError
Message: Too few arguments to function Profile::editProfile(), 0 passed in /Applications/MAMP/htdocs/Capstone/system/core/CodeIgniter.php on line 532 and exactly 1 expected
I'm passing $id in the function, so I'm not sure why this is happening.
Model:
public function updateUser($id)
{
$data = array(
'age' => $this->input->post('age'),
'location' => $this->input->post('location'),
'favouritebeer' => $this->input->post('favouritebeer'),
'website' => $this->input->post('website'),
'bio' => $this->input->post('bio')
);
$this->db->where('id', $id);
$this->db->update('users', $data);
return $id;
}
public function getUser($id)
{
$this->db->select('*');
$this->db->where('id', $id);
$this->db->from('users');
$query = $this->db->get();
return $query-row();
}
Controller:
public function editProfile($id)
{
$this->load->model('user_model');
$this->load->view('templates/header');
$this->load->view('pages/editprofile');
$this->load->view('templates/footer');
if(isset($_POST['updatechanges'])) {
if($this->user_model->getUser($id)) {
$this->session->set_flashdata('success', 'Profile updated!');
redirect('profile', 'refresh');
}
}
}
View:
<div class ="container">
<?php
$con=mysqli_connect("localhost","root","admin","Mydb");
$sql = mysqli_query($con, "SELECT id, username, age, location, favouritebeer, website, bio FROM users WHERE username = '" . $_SESSION['username'] . "'");
$row = mysqli_fetch_array($sql);
?>
<h2><?php echo $_SESSION['username'];?>'s Profile</h2>
Logout
</br>
Change password
</br>
Change security question
</br>
</br>
<body>
<form method ="POST">
<h4>Age:</h4>
<input type="age" class="form-control" name="age" id="ageinput" value="<?php echo $row['age']; ?>">
<h4>Location:</h4>
<input type="location" class="form-control" name="location" id="locationinput" value="<?php echo $row['location']; ?>">
<h4>Favourite beer:</h4>
<input type="beer" class="form-control" name="favouritebeer" id="beerinput" value="<?php echo $row['favouritebeer']; ?>">
<h4>Website:</h4>
<input type="website" class="form-control" name="website" id="websiteinput" value="<?php echo $row['website']; ?>">
<h3>Bio: </h3>
<input type="bio" class="form-control" name="bio" id="bioinput" value="<?php echo $row['bio']; ?>">
Save Changes
</form>
</div>
if you are calling this editProfile() function from view try the following code :
in view :
edit
and in controller :
function editProfile() {
$id = $this->uri->segment(3);
// do more
}
and if you are calling this function from another function in the same controller try this :
$this->editProfile(1);
Try this code. The below mentioned function have one errors in return place.
public function getUser($id)
{
$this->db->select('*');
$this->db->where('id', $id);
$this->db->from('users');
$query = $this->db->get();
return $query->row();
}
I have this edit page that has gets its id and status from uri segments. The problem is that if the user doesn't select an image or complete a part of the form, the page is supposed to reload and show the validation errors for the form. However that page reload causes the data in the form to go missing. Is there a way to solve this issue? Any help would be greatly appreciated. I attached my view, and controller
View
<?php if($edit == "false"){
echo form_open_multipart('Control/Products/ProductDetail/addProduct','class="productdetail"');
}else{
echo form_open_multipart('Control/Products/ProductDetail/editProduct','class="productdetail"');
}?>
<label for="inputproductname">Product Name</label>
<input type="text" class="form-control" id="inputproductname" name="inputproductname" placeholder="Name" value="<?php echo $name; ?>">
<label for="inputproductdescription">Product Description</label>
<textarea class="form-control" id="inputproductdescription" name="inputproductdescription" placeholder="Description" rows="7"
><?php echo $description; ?></textarea>
<label for="inputproductprice">Product Price</label>
<input type="price" class="form-control" id="inputproductprice" name="inputproductprice" placeholder="Price" value="<?php echo $price; ?>">
<label for="inputproductimage">Product Image</label>
<p><input type="file" class="form-control-file" name="upload" id="upload" aria-describedby="fileHelp"></p>
<input type="hidden" class="form-control" id="inputcurrentid" name="inputcurrentid" value="<?php echo $currentid; ?>">
<input type="hidden" class="form-control" id="inputcurrentstatus" name="inputcurrentstatus" value="<?php echo $currentstatus; ?>">
<button type="submit" class="btn btn-primary">
<?php if($edit == "false"){
echo "Add";
}else{
echo "Edit";
}?>
</button>
Cancel
<?php echo form_close(); ?>
<?php echo validation_errors(); ?>
<p><?php echo $this->session->flashdata('Form'); ?></p>
Controller
public function index(){
$productid = $this->uri->segment(5);
$editstatus = $this->uri->segment(6);
if($editstatus == "false"){
$data['name'] = '';
$data['description'] = '';
$data['price'] = '';
$data['edit'] = "false";
$data['message']='';
$data['currentid'] = '';
$data['currentstatus'] = '';
}else{
$product = $this->ProductsModel->getProduct($productid);
foreach ($product as $productdetail){
$data['name'] = $productdetail->name;
$data['description'] = $productdetail->description;
$data['price'] = $productdetail->price;
}
$data['edit'] = "true";
$data['message']='';
$data['currentid'] = $productid;
$data['currentstatus'] = $editstatus;
}
$this->load->view('control/controlMenu/navigationLink');
$this->load->view('control/controlProducts/productDetail',$data);
$this->load->view('control/controlMenu/navigationJquery');
}
public function editProduct(){
$this->form_validation->set_error_delimiters('<p class="error">', '</p>');
$this->form_validation->set_rules('inputproductname', 'Name', 'trim|required');
$this->form_validation->set_rules('inputproductdescription', 'Description', 'trim|required');
$this->form_validation->set_rules('inputproductprice', 'Price', 'trim|required');
if (empty($_FILES['userfile']['name']))
{
$this->form_validation->set_rules('upload', 'Image', 'required');
}
$inputproductname = $this->input->post('inputproductname');
$inputproductdescription = $this->input->post('inputproductdescription');
$inputproductprice = $this->input->post('inputproductprice');
$inputdateadded = date('Y-m-d');
$inputcurrentid = $this->input->post('inputcurrentid');
$inputcurrentstatus = $this->input->post('inputcurrentstatus');
$config['upload_path'] = $this->getProductImageFolderPath();
$config['allowed_types'] = 'jpg|jpeg|png';
$config['max_size'] = 3000;
$config['remove_spaces'] = TRUE;
$config['overwrite'] = TRUE;
$config['file_name'] = $inputproductname;
$this->load->library('upload', $config);
if($this->form_validation->run()==false){
redirect('/Control/Products/ProductDetail/index/'.$inputcurrentid.'/'.$inputcurrentstatus);
}else{
if(!$this->upload->do_upload('upload')){
$this->session->set_flashdata('Form',$this->upload->display_errors());
redirect('Control/'.$this->getCurrentModule().'/'.$this->getClassName());
}else{
$extension = $this->upload->data('file_ext');
$productdetails = array(
'name'=>$inputproductname,
'description'=>$inputproductdescription,
'price'=>$inputproductprice,
'imagePath'=>$config['upload_path'].$config['file_name'].$extension,
'dateAdded'=>$inputdateadded
);
$this->db->trans_start();
$this->ProductsModel->editProduct($productid,$productdetails);
$this->db->trans_complete();
if($this->db->trans_status()===false){
}else{
$this->session->set_flashdata('Form', $inputproductname . ' has been altered on the database');
redirect('/Control/Products/Products');
}
}
}
}
if($this->form_validation->run()==false){
redirect('/Control/Products/ProductDetail/index/'.$inputcurrentid.'/'.$inputcurrentstatus);
I think the problem with this redirect stmt don't use it instead you need to use load view so that it will preserves the errors array.
just do ths in your controller
if ($this->form_validation->run() == FALSE) {
$this->session->set_flashdata('field', form_error('field', '<span class="text-danger pl-3">', '</span>'));
redirect("url");
} else {
# code...
}
and in your redirect page use
<?= $this->session->flashdata('field'); ?>
I have a data table that I want to edit. I currently pass the id via the url to an edit page and provide a form that loads details from the database. However if there is any validation error, the form will reset and the details will disappear. I am thinking that if each row were a form, I could post the data and even with a refresh, the id would still remain. However I am afraid how slow it would make my table.
Does anyone have any other method that I may use? Thank you.
This is my code
View
<?php if($edit == "false"){
echo form_open_multipart('Control/Products/ProductDetail/addProduct','class="productdetail"');
}else{
echo form_open_multipart('Control/Products/ProductDetail/editProduct','class="productdetail"');
}?>
<label for="inputproductname">Product Name</label>
<input type="text" class="form-control" id="inputproductname" name="inputproductname" placeholder="Name" value="<?php echo $name; ?>">
<label for="inputproductdescription">Product Description</label>
<textarea class="form-control" id="inputproductdescription" name="inputproductdescription" placeholder="Description" rows="7"
><?php echo $description; ?></textarea>
<label for="inputproductprice">Product Price</label>
<input type="price" class="form-control" id="inputproductprice" name="inputproductprice" placeholder="Price" value="<?php echo $price; ?>">
<label for="inputproductimage">Product Image</label>
<p><input type="file" class="form-control-file" name="upload" id="upload" aria-describedby="fileHelp"></p>
<input type="hidden" class="form-control" id="inputcurrentid" name="inputcurrentid" value="<?php echo $currentid; ?>">
<input type="hidden" class="form-control" id="inputcurrentstatus" name="inputcurrentstatus" value="<?php echo $currentstatus; ?>">
<button type="submit" class="btn btn-primary">
<?php if($edit == "false"){
echo "Add";
}else{
echo "Edit";
}?>
</button>
Cancel
<?php echo form_close(); ?>
<?php echo validation_errors(); ?>
<p><?php echo $this->session->flashdata('Form'); ?></p>
Controller
public function index(){
$productid = $this->uri->segment(5);
$editstatus = $this->uri->segment(6);
if($editstatus == "false"){
$data['name'] = '';
$data['description'] = '';
$data['price'] = '';
$data['edit'] = "false";
$data['message']='';
$data['currentid'] = '';
$data['currentstatus'] = '';
}else{
$product = $this->ProductsModel->getProduct($productid);
foreach ($product as $productdetail){
$data['name'] = $productdetail->name;
$data['description'] = $productdetail->description;
$data['price'] = $productdetail->price;
}
$data['edit'] = "true";
$data['message']='';
$data['currentid'] = $productid;
$data['currentstatus'] = $editstatus;
}
$this->load->view('control/controlMenu/navigationLink');
$this->load->view('control/controlProducts/productDetail',$data);
$this->load->view('control/controlMenu/navigationJquery');
}
public function editProduct(){
$this->form_validation->set_error_delimiters('<p class="error">', '</p>');
$this->form_validation->set_rules('inputproductname', 'Name', 'trim|required');
$this->form_validation->set_rules('inputproductdescription', 'Description', 'trim|required');
$this->form_validation->set_rules('inputproductprice', 'Price', 'trim|required');
if (empty($_FILES['userfile']['name']))
{
$this->form_validation->set_rules('upload', 'Image', 'required');
}
$inputproductname = $this->input->post('inputproductname');
$inputproductdescription = $this->input->post('inputproductdescription');
$inputproductprice = $this->input->post('inputproductprice');
$inputdateadded = date('Y-m-d');
$inputcurrentid = $this->input->post('inputcurrentid');
$inputcurrentstatus = $this->input->post('inputcurrentstatus');
$config['upload_path'] = $this->getProductImageFolderPath();
$config['allowed_types'] = 'jpg|jpeg|png';
$config['max_size'] = 3000;
$config['remove_spaces'] = TRUE;
$config['overwrite'] = TRUE;
$config['file_name'] = $inputproductname;
$this->load->library('upload', $config);
if($this->form_validation->run()==false){
redirect('/Control/Products/ProductDetail/index/'.$inputcurrentid.'/'.$inputcurrentstatus);
}else{
if(!$this->upload->do_upload('upload')){
$this->session->set_flashdata('Form',$this->upload->display_errors());
redirect('Control/'.$this->getCurrentModule().'/'.$this->getClassName());
}else{
$extension = $this->upload->data('file_ext');
$productdetails = array(
'name'=>$inputproductname,
'description'=>$inputproductdescription,
'price'=>$inputproductprice,
'imagePath'=>$config['upload_path'].$config['file_name'].$extension,
'dateAdded'=>$inputdateadded
);
$this->db->trans_start();
$this->ProductsModel->editProduct($productid,$productdetails);
$this->db->trans_complete();
if($this->db->trans_status()===false){
}else{
$this->session->set_flashdata('Form', $inputproductname . ' has been altered on the database');
redirect('/Control/Products/Products');
}
}
}
}
my suggestion is, you make different method to add and edit or you can use ajax to call data and setInterval function to manage time you get the data
My Controller Codes
Looking forward for your help! thank you :
public function index()
{
$registration = $this->Registration_model->get_all();
$data = array(
'registration_data' => $registration
);
$this->load->view('registration/registration_list', $data);
}
public function do_upload()
{
$config['upload_path'] = './uploads/pictures/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 10000000;
$config['max_width'] = 10240000;
$config['max_height'] = 76800000;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('Image'))
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('registration_form', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
return $data['upload_data']['file_name'];
}
}
public function read($id)
{
$row = $this->Registration_model->get_by_id($id);
if ($row) {
$data = array(
'id' => $row->id,
'firstName' => $row->firstName,
'lastName' => $row->lastName,
'gender' => $row->gender,
'address' => $row->address,
'dob' => $row->dob,
'Image' => $row->Image,
);
$this->load->view('registration/registration_read', $data);
} else {
$this->session->set_flashdata('message', 'Record Not Found');
redirect(site_url('registration'));
}
}
public function create()
{
$data = array(
'button' => 'Create',
'action' => site_url('registration/create_action'),
'id' => set_value('id'),
'firstName' => set_value('firstName'),
'lastName' => set_value('lastName'),
'gender' => set_value('gender'),
'address' => set_value('address'),
'dob' => set_value('dob'),
'Image' =>set_value('image'),
);
$this->load->view('registration/registration_form', $data);
}
public function create_action()
{
$this->_rules();
if ($this->form_validation->run() == FALSE) {
$this->create();
} else {
$data = array(
'firstName' => $this->input->post('firstName',TRUE),
'lastName' => $this->input->post('lastName',TRUE),
'gender' => $this->input->post('gender',TRUE),
'address' => $this->input->post('address',TRUE),
'dob' => $this->input->post('dob',TRUE),
'Image' => $this->input->post('Image',TRUE),
);
$this->Registration_model->insert($data);
$this->session->set_flashdata('message', 'The New Record was Successfuly Added');
redirect(site_url('registration'));
}
}
My Views Codes
<div class="form-group">
<label for="date">Dob <?php echo form_error('dob') ?></label>
<input type="text" class="form-control" name="dob" id="dob" placeholder="Dob" value="<?php echo $dob; ?>" />
</div>
<div class="form-group">
<label for="varchar">Image <?php echo form_open_multipart('upload/do_upload');?></label>
<input type="file" class="form-control" name="Image" id="Image" height="50" />
<br/>
</div>
<br/>
<input type="hidden" name="id" value="<?php echo $id; ?>" />
<button type="submit" class="btn btn-primary"name="userSubmit"><?php echo $button ?></button>
My Model
class Registration_model extends CI_Model
{
public $table = 'registration';
public $id = 'id';
public $order = 'DESC';
function __construct()
{
parent::__construct();
}
// get all
function get_all()
{
$this->db->order_by($this->id, $this->order);
return $this->db->get($this->table)->result();
}
// get data by id
function get_by_id($id)
{
$this->db->where($this->id, $id);
return $this->db->get($this->table)->row();
}
// get total rows
function total_rows($q = NULL) {
$this->db->like('id', $q);
$this->db->or_like('firstName', $q);
$this->db->or_like('lastName', $q);
$this->db->or_like('gender', $q);
$this->db->or_like('address', $q);
$this->db->or_like('dob', $q);
$this->db->or_like('Image', $q);
$this->db->from($this->table);
return $this->db->count_all_results();
}
// get data with limit and search
function get_limit_data($limit, $start = 0, $q = NULL) {
$this->db->order_by($this->id, $this->order);
$this->db->like('id', $q);
$this->db->or_like('firstName', $q);
$this->db->or_like('lastName', $q);
$this->db->or_like('gender', $q);
$this->db->or_like('address', $q);
$this->db->or_like('dob', $q);
$this->db->or_like('Image', $q);
$this->db->limit($limit, $start);
return $this->db->get($this->table)->result();
}
// insert data
function insert($data)
{
$this->db->insert($this->table, $data);
}
// update data
function update($id, $data)
{
$this->db->where($this->id, $id);
$this->db->update($this->table, $data);
}
// delete data
function delete($id)
{
$this->db->where($this->id, $id);
$this->db->delete($this->table);
}
}
Hello You can try this
$config['upload_path'] = 'ADD HERE YOUR SITE PATH TO PICTURE'; // Like SITE_PATH .'/uploads/pictures/';
and print $config['upload_path'] and check it's correct your upload picture path
I've had the same issue before. Depending on your server, you may want to remove the ./ from your upload path.
public function do_upload()
{
$config['upload_path'] = 'uploads/pictures/';
Change your view as below. You are not closing form
<?php echo form_open_multipart('upload/do_upload');?>
<div class="form-group">
<label for="date">Dob <?php echo form_error('dob') ?></label>
input type="text" class="form-control" name="dob" id="dob" placeholder="Dob" value="<?php echo $dob; ?>" />
</div>
<div class="form-group">
<label for="varchar">Image </label>
<input type="file" class="form-control" name="Image" id="Image" height="50" />
<br/>
</div>
<br/>
<input type="hidden" name="id" value="<?php echo $id; ?>" />
<button type="submit" class="btn btn-primary"name="userSubmit"><?php echo $button ?></button>
<?= form_close()?>