Welcome Guest, Not a member yet? Register   Sign In
how to upload image and create item to database in one button
#1

help me please  Angel

this is my controllers create item
PHP Code:
function create_item()
 
   {
 
       $this->is_admin();

 
       $this->load->helper('form');
 
       $this->load->library('form_validation');

 
       $this->form_validation->set_rules('name''Name''required');
 
       $this->form_validation->set_rules('description''Description');
 
       $this->form_validation->set_rules('price''Price''trim|required|numeric');
 
       $this->form_validation->set_rules('categoryID''Category''required');
 
       $this->form_validation->set_rules('brandID''Brand''required');

 
       if ($this->form_validation->run() === FALSE) {

 
           $data['main_content'] = 'admin/item_create';
 
           $this->load->view('admin/template'$data$this->globals);
 
       } else {
 
           $data = array(
 
               'name' => $this->input->post('name'),
 
               'price' => $this->input->post('price'),
 
               'image' => $this->input->post('image'),
 
               'description' => $this->input->post('description'),
 
               'categoryID' => $this->input->post('categoryID'),
 
               'brandID' => $this->input->post('brandID')
 
           );
            
 
           $insert_id $this->item_model->set_item($data);
 
           $this->session->set_flashdata('success''Item Added');
 
           $this->view($insert_id);
            
redirect('admin/show/items');
            
 
       }
 
   

and this my controller upload
PHP Code:
public function do_upload()
 
       {
 
               $config['upload_path'         './images/furniture';
 
               $config['allowed_types'       'gif|jpg|png';
 
               $config['max_size'            1000;
 
               $config['max_width'           1024;
 
               $config['max_height'          768;

 
               $this->load->library('upload'$config);

 
               if ( ! $this->upload->do_upload('image'))
 
               {
 
                       $error = array('error' => $this->upload->display_errors());

 
                       redirect($redirect_url);
 
               }
 
               else
                
{
 
                       $data = array('upload_data' => $this->upload->data());
                        
redirect('home');
 
                      
                
}
 
       

and this my view
PHP Code:
<?php echo validation_errors(); ?>

<?php

$attributes 
= array('class' => 'form');

echo 
form_open('item/create_item'$attributes); ?>

<div class="form-group">
    <label for="name">Name</label>
    <input type="input" name="name" class="form-control" placeholder="Name"  required />
</div>
<div class="form-group">
    <label for="price">Price</label>
    <input type="input" name="price" class="form-control" placeholder="Price"   required/>
</div>
<div class="form-group">
    <label for="categoryID">Category</label>
    <select name="categoryID" class="form-control">
        <?php
        foreach
($categories as $category){
 
           echo "<option value='"$category['id'] . "'>" $category['name'] . "</option>";
 
       }
 
       ?>
    </select>
</div>
<div class="form-group">
    <label for="brandID">Brand</label>
    <select name="brandID" class="form-control">
        <?php
        foreach
($brands as $brand){
 
           echo "<option value='"$brand['id'] . "'>" $brand['name'] . "</option>";
 
       }
 
       ?>
    </select>
</div>
<div class="form-group">
    <label for="image">Image</label>
    <input type="file" name="image" placeholder="Image" >
</div>
<div class="form-group">
    <label for="description">Description</label>
    <textarea name="description" class="form-control" placeholder="Description" ></textarea>
</div>


<div class="form-group registration_form">
    <input type="submit" name="submit" value="Create item" />
</div>


</form> 
Reply
#2

For one you do not want to save images to the database, save them to a directory and then save the path and filename to the database.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

(This post was last modified: 06-17-2016, 03:58 AM by Wouter60.)

Before redirect('home'), insert the code that inserts the item (title, path, size etc.) into the table in your database. That way, it's assured that the upload itself was successful and the image exists. If you insert the item into the table before do_upload(), then you never know...
Reply
#4

(06-17-2016, 03:57 AM)Wouter60 Wrote: Before redirect('home'), insert the code that inserts the item (title, path, size etc.) into the table in your database. That way, it's assured that the upload itself was successful and the image exists. If you insert the item into the table before do_upload(), then you never know...

can you give me example for my problem ?
Reply
#5

If you must save it to the database the image table field must be a BLOB type
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#6

(This post was last modified: 06-18-2016, 05:26 AM by fian rahardian.)

(06-18-2016, 02:45 AM)InsiteFX Wrote: If you must save it to the database the image table field must be a BLOB type

i try to post in my database with field name : name, price, image, category and brand.
but in my view form. i want do that in one submit buttom.
and my problem is in upload image in upload directory.
until now i cannot fix my problem.
can you give me emaple for that ?

and what is Blob type ?
please help me
sorry im newbe.
and sorry for my bad english
Reply
#7

fian rahardian

submit form with enctype="multipart/form-data"

upload image with CI library upload
and get upload data

and store your other data in DB with uploaded file name
Reply
#8

Usually on this board or on stackoverflow - you will get helpful responses if you have a specific error that you are trying to work through. Otherwise you are asking people to create an application for you and that usually does not happen.

So first - work through some tutorials and build. https://www.google.com/#q=codeigniter+im...d+tutorial
Reply
#9

thanks guys finally its works
Reply




Theme © iAndrew 2016 - Forum software by © MyBB