简体   繁体   中英

Uploading form with image in database

After submitting the form, i insert the information in database.Everything is fine,except, the image is not inserted in databse,it shows 0 in image name field in db.Please guys, watch in the form_open() tag,and my img input,what is wrong here? It will be great help .

Thank you

The Controller:

<?php

class insert_ctrl extends Controller {

 function __construct() {
    parent::__construct();
    $this->load->model('insert_model'); 

} 
function index() {

             $this->load->model('category_model'); 
             $fata['all_categories'] = $this->category_model->all_categories();  
        //Setting values for tabel columns
        $data = array(
            'product_name' => $this->input->post('dname'),
            'product_desc' => $this->input->post('desc'),
            'product_price' => $this->input->post('price'),
            'image'=>  $this->input->post('img'),
            'product_options'=>  $this->input->post('options'),
           );
              $this->insert_model->form_insert($data);


        $product_id = $this->db->insert_id();

        $categories=  $this->input->post('cat');

         /*echo '<pre>';
          print_r($categories);
          * 
          */

        if(!empty($categories)){
        foreach($categories as $value){
        $res_arr=explode('_',$value);
        $cat_id=$res_arr[0];
        $cat_name=$res_arr[1];
        $this->insert_model->cate_insert($product_id,$cat_id);
    }

        }
        //$data['message'] = 'Data Inserted Successfully';
        //Loading View
        $this->load->view('insert_view',$fata);

}

}

?>

The view:

<html>
    <head>
        <title>Insert Data Into Database Using CodeIgniter Form</title>
        <link href='http://fonts.googleapis.com/css?family=Marcellus' rel='stylesheet' type='text/css'/>
        <link  rel="stylesheet" type="text/css" href="<?php echo base_url(); ?>include/insert_style.css" />
        <style>
            input[type=checkbox]{
                margin: 5px -167px 20px;
            }
        </style>
    </head>
    <body>

        <div id="container">
            <?php echo form_open_multipart('insert_ctrl'); ?>
            <h1>Insert Data Into Database Using CodeIgniter</h1><hr/> 
            <?php if (isset($message)) { ?>
                <CENTER><h3 style="color:green;">Data inserted successfully</h3></CENTER><br>
            <?php } ?>


             <?php echo form_label('Product Name :'); ?> <?php echo form_error('dname'); ?><br />
            <?php echo form_input(array('id' => 'dname', 'name' => 'dname')); ?><br />

            <?php echo form_label('Product Description :'); ?> <?php echo form_error('desc'); ?><br />
            <?php echo form_input(array('id' => 'desc', 'name' => 'desc')); ?><br />

            <?php echo form_label('Product Price :'); ?> <?php echo form_error('price'); ?><br />
            <?php echo form_input(array('id' => 'price', 'name' => 'price', 'placeholder' =>'' )); ?><br />

            <?php echo form_label('Product options :'); ?> <?php echo form_error('options'); ?><br />
            <?php echo form_input(array('id' => 'options', 'name' => 'options', 'placeholder' =>'' )); ?><br />


            <?php echo form_open_multipart('insert_ctrl');?> 
            <?php echo form_input(array('id'=>'img','name'=>'img','type'=>'file'));?>


              <

              <?php  foreach ($all_categories as $v_menu) { ?>
              <input type="checkbox" id="cat_<?php echo $v_menu->id; ?>" value="<?php echo $v_menu->id.'_'.$v_menu->name; ?>" name="cat[]"> 
              <?php echo $v_menu->name; ?>
               <?php echo '<br/>'; ?>
               <?php } ?> 


            <?php echo form_submit(array('id' => 'submit','name'=>'submit', 'value' => 'Add Product')); ?>
            <?php echo form_close(); ?><br/>
           <div id="fugo">

            </div>
        </div>
    </body>
</html>

This won't work.

$this->input->post('img')

$_FILES used for getting image in core php and in CI use codeigniter library for uploading image.

You are put form_open_multipart two times in a form.

For more Information - http://www.codeigniter.com/user_guide/libraries/file_uploading.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM