• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Can't get information to insert into database

#1
[eluser]mikegeorgeff[/eluser]
I have a small CMS where my customer can add products to their site. When the data gets submitted it goes to a blank screen and the information does not insert.

Model Code:
Code:
function addProduct()
    {
        $now = date("Y-m-d H:i:s");
        $data = array(
            'name' => $this->input->post('name'),
            'desc' => $this->input->post('desc'),
            'category_id' => $this->input->post('category_id'),
            'featured' => $this->input->post('featured'),
            'date' => $now
        );
        
        if($_FILES)
        {
            // Config the upload
            $config['upload_path'] = './uploads/';
            $config['allowed_types'] = 'gif|jpg|jpeg|png';
            $config['max_size'] = '2048';
            $config['remove_spaces'] = TRUE;
            $config['overwrite'] = FALSE;
            
            // Do the upload
            $this->load->library('upload', $config);
            if(!$this->upload->do_upload('image'))
            {
                $this->upload->display_errors();
                exit();
            }
            
            // Create file name array for database entry
            $image_data = $this->upload->data();
            
            $this->load->library('image_lib');
            // Config image size for Prod details page
            $config['source_image'] = $image_data['full_path'];
            $config['new_image'] = './uploads/img/';
            $config['maintain_ratio'] = TRUE;
            $config['height'] = 157;
            $config['width'] = 204;
            
            $this->image_lib->initialize($config);
            if(!$this->image_lib->resize())
            {
                $this->image_lib->display_errors();
                exit();
            }
            
            // data for database insert
            if($image_data['file_name'])
            {
                $data['image'] = 'img/'.$image_data['file_name'];
            }
            $this->image_lib->clear();
            
            // config thumbnail
            $configThumb['source_image'] = $image_data['full_path'];
            $configThumb['new_image'] = './uploads/thumbs/';
            $configThumb['maintain_ratio'] = TRUE;
            $configThumb['height'] = 97;
            $configThumb['width'] = 144;
            
            $this->image_lib->initialize($configThumb);
            if(!$this->image_lib->resize())
            {
                $this->image_lib->display_errors();
                exit();
            }
            
            // thumbnail data for database insert
            if($image_data['file_name'])
            {
                $data['thumbnail'] = 'thumbs/'.$image_data['file_name'];
            }    
        }    
        // insert data
        $this->db->insert('product', $data);
    }
View Code:
Code:
<?php
echo form_open_multipart('admin/create_equipment');

echo "<p><label>Name</label><br />";
$data = array('name' => 'name', 'id' => 'name', 'size' => 25);
echo form_input($data) ."</p>";

echo "<p><label>Category</label><br />";
echo form_dropdown('category_id', $categories) ."</p>";

echo "<p><label>Description</label><br />";
$data = array('name' => 'desc', 'id' => 'desc', 'rows' => 5, 'cols' => '40');
echo form_textarea($data) ."</p>";

echo "<p><label>Image</label><br />";
$data = array('name' => 'image', 'id' => 'image');
echo form_upload($data) ."</p>";

echo "<p><label>Featured?</label><br />";
$options = array('false' => 'false', 'true' => 'true');
echo form_dropdown('featured', $options) ."</p>";

echo form_submit('submit', 'Add Product');
echo form_close();

?&gt;
Controller Code:
Code:
function create_equipment()
    {
        if ($this->input->post('name'))
        {
            $this->MProducts->addProduct();
            $this->session->set_flashdata('message', 'Product Added');
            redirect('admin/equipment', 'refresh');
        }else{
            $data['title'] = 'Add Product';
            $data['main'] = 'admin/new_product';
            $data['categories'] = $this->MCats->getCategoriesDropDown();
            $this->load->view('admin/template', $data);
        }
    }

I can't see what the issue is.

#2
[eluser]darrentaytay[/eluser]
First things to test would be what's in your array before you do the Insert - make sure all the correct data is there.

Next thing would be to check the last query using:

Code:
$this->db->last_query();

Also, do you have php error reporting on?

#3
[eluser]d1a8lo24[/eluser]
You can use the profiler to narrow down where the problem is, if you get an output from the profiler then you will know that you have an sql error or something wrong with the upload function.

If you don't get any output then your controller has a problem which i don't think so but you never know.

Also I see that you're using an SQL time stamp which is 2011-05-10 02:02:40 am or something you might be getting an error there SQL hates it when the time stamp is not in the right format.

That is why more experience programmers use time to keep track of time meaning when was it inserted, updated and so on... Best thing of all when using operators it makes it much easier to work with and you can have as many time tracker fields as you like, like and expire feature date instead of SQLs that only allows you to have 1 time stamp field per table. By the way when you use time you use an int field and when you want to return a date you can use the date() function just add your format and then the the return unix time stamp. SO keep that in mind even if that is not the error.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.