title: Code Igniter CRUD app: thumbnail image doesn't upload. Receive database error because NULL
body: <p>Making a form that will accept a bunch of textual data and an image upload.</p>
  
  <p>Keep getting this error when adding new item to both tables:</p>
  
  <blockquote>
    <p>Error Number: 1048</p>
    
    <p>Column 'id_path' cannot be null</p>
    
    <p>INSERT INTO <code>thumbnails</code> (<code>id_path</code>, <code>id_data_row</code>) VALUES (NULL, 17)</p>
    
    <p>Filename: C:\wamp\www\project\system\database\DB_driver.php</p>
    
    <p>Line Number: 330</p>
  </blockquote>
  
  <p>What I'm trying to do is add text data(stuff like title, text, price, etc.) into one table(<strong>data</strong>) and then upload an image and add its full upload path(as primary key in <strong>thumbnails</strong> table) and the id of the inserted text data row(to link it with the thumbnail in <strong>thumbnails</strong> table) into <strong>thumbnails</strong> table.</p>
  
  <p>For some reason it keeps passing NULL as the full upload path to thumbnails table.</p>
  
  <p><strong>crud.php</strong>(the controller):</p>
  
  <pre><code>function add()
          {
              //Set validation properties
              $this-&gt;_set_fields();
  
              //Set common properties
              $data['title'] = 'Add new data row';
              $data['message'] = '';
              $data['action'] = site_url('crud/addDataRow');
              $data['link_back'] = anchor('crud/index', 'Back to list', array('class' =&gt; 'back'));
  
              //Load the view
              $this-&gt;load-&gt;view('templates/header', $data);
              $this-&gt;load-&gt;view('pages/crud_edit', $data);
              $this-&gt;load-&gt;view('templates/footer');
          }
  
          function addDataRow()
          {
              //Set common properties
              $data['title'] = 'Add new data row';
              $data['action'] = site_url('crud/addDataRow');
              $data['link_back'] = anchor('crud/index/', 'Back to list', array('class' =&gt; 'back'));
  
              //Set validation properties
              $this-&gt;_set_fields();
              $this-&gt;_set_rules();
  
              //Run validation
              if($this-&gt;form_validation-&gt;run() == FALSE)
              {
                  $data['message'] = '';
              }
              else
              {
                  //Save the data
                  $full_file_path = null;
                  $config['allowed_types'] = 'gif|jpg|png';
                  $config['max_size'] = '100';
                  $config['max_width']  = '1024';
                  $config['max_height']  = '768';
                  $path_to_uploads='./uploads';
                  $config['upload_path'] = $path_to_uploads;
                  $this-&gt;load-&gt;library('upload', $config);
                  //add this
                  $this-&gt;upload-&gt;initialize($config);
                  if (!$this-&gt;upload-&gt;do_upload()){
                      $error = $this-&gt;upload-&gt;display_errors();
                      echo "&lt;script&gt;alert($error);&lt;/script&gt;";
                  }else{
                      $upload_data=$this-&gt;upload-&gt;data();
                      $file_name=$upload_data['file_name'];
                      $full_file_path = $path_to_uploads.'/'.$file_name;
                  }
  
  
                  $data_row = array(
                      'title' =&gt; $this-&gt;input-&gt;post('title'),
                      'text' =&gt; $this-&gt;input-&gt;post('text'),
                      'price' =&gt; $this-&gt;input-&gt;post('price'),
                      'status' =&gt; $this-&gt;input-&gt;post('status'),
                      'type' =&gt; $this-&gt;input-&gt;post('type')
                  );
  
                  $id = $this-&gt;crud_model-&gt;save($data_row);
  
                  $thumbnail_row = array(
                      'id_path' =&gt; $full_file_path,
                      'id_data_row' =&gt; $id
                  );
  
                  $this-&gt;crud_model-&gt;save_thumbnail($thumbnail_row);
  
                  //Set form input name="id"
                  $this-&gt;form_validation-&gt;id = $id;
  
                  //Set user message
                  $data['message'] = '&lt;div class="success"&gt;New data row added!&lt;/div&gt;';
              }
  
              $this-&gt;load-&gt;view('templates/header', $data);
              $this-&gt;load-&gt;view('pages/crud_edit', $data);
              $this-&gt;load-&gt;view('templates/footer');
          }
  </code></pre>
  
  <p><strong>crud_model.php</strong>(the model):</p>
  
  <pre><code>//Add new data row
          function save($data)
          {
  
              $this-&gt;db-&gt;insert($this-&gt;tbl_data, $data);
              return $this-&gt;db-&gt;insert_id();
          }
  
          //Add the thumbnail upload path and id of the row in data table to link them
          function save_thumbnail($data)
          {
              $this-&gt;db-&gt;insert($this-&gt;tbl_thumbnails, $data);
              return $this-&gt;db-&gt;insert_id();
          }
  </code></pre>
  
  <p><strong>EDIT(html form code):</strong></p>
  
  <p><strong>crud_edit.php</strong>(html form code for <strong>adding</strong> a new item or <strong>updating</strong> an existing one):</p>
  
  <pre><code>&lt;div id="contentColumn"&gt;
      &lt;h1&gt;&lt;?php echo $title; ?&gt;&lt;/h1&gt;
      &lt;?php echo $message; ?&gt;
      &lt;form method="post" action="&lt;?php echo $action; ?&gt;" multipart="multipart"&gt;
          &lt;div class="data"&gt;
              &lt;table&gt;
                  &lt;tr&gt;
                      &lt;td width="30%"&gt;ID&lt;/td&gt;
                      &lt;td&gt;
                          &lt;input type="text" name="id" disabled="disabled" class="text" value="&lt;?php echo set_value('id'); ?&gt;"/&gt;
                          &lt;input type="hidden" name="id" value="&lt;?php echo set_value('id',$this-&gt;form_data-&gt;id); ?&gt;"/&gt;
                      &lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;tr&gt;
                      &lt;td valign="top"&gt;Title&lt;span style="color:red;"&gt;*&lt;/span&gt;&lt;/td&gt;
                      &lt;td&gt;
                          &lt;input type="text" name="title" class="text" value="&lt;?php echo set_value('title',$this-&gt;form_data-&gt;title); ?&gt;"/&gt;
                          &lt;?php echo form_error('title'); ?&gt;
                      &lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;tr&gt;
                      &lt;td valign="top"&gt;Text&lt;span style="color:red;"&gt;*&lt;/span&gt;&lt;/td&gt;
                      &lt;td&gt;
                          &lt;textarea name="text" class="text"&gt;
                              &lt;?php echo set_value('text',$this-&gt;form_data-&gt;text); ?&gt;
                          &lt;/textarea&gt;
                          &lt;?php echo form_error('text'); ?&gt;
                      &lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;tr&gt;
                      &lt;td valign="top"&gt;Price&lt;span style="color:red;"&gt;*&lt;/span&gt;&lt;/td&gt;
                      &lt;td&gt;
                          &lt;input type="text" name="price" class="text" value="&lt;?php echo set_value('price',$this-&gt;form_data-&gt;price); ?&gt;"/&gt;
                          &lt;?php echo form_error('price'); ?&gt;
                      &lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;tr&gt;
                      &lt;td valign="top"&gt;Thumbnail&lt;span style="color:red;"&gt;*&lt;/span&gt;&lt;/td&gt;
                      &lt;td&gt;
                          &lt;?php echo form_upload(array('name'=&gt;'thumb', 'type'=&gt;'file', 'accept'=&gt;'image/*'))?&gt;
                          &lt;!--&lt;input type="file" name="thumb" size="20" /&gt;--&gt;
                          &lt;?php echo form_error('thumb'); ?&gt;
                      &lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;!--
                  &lt;tr&gt;
                      &lt;td valign="top"&gt;Images&lt;span style="color:red;"&gt;*&lt;/span&gt;&lt;/td&gt;
                      &lt;td&gt;
                          &lt;input type="text" name="images" class="text" value="&lt;?php echo set_value('images',$this-&gt;form_data-&gt;images); ?&gt;"/&gt;
                          &lt;?php echo form_upload(array('name'=&gt;'images', 'type'=&gt;'file', 'multiple'=&gt;'multiple', 'accept'=&gt;'image/*'))?&gt;
                          &lt;?php echo form_error('images'); ?&gt;
                      &lt;/td&gt;
                  &lt;/tr&gt;
                  --&gt;
                  &lt;tr&gt;
                      &lt;td valign="top"&gt;Status&lt;/td&gt;
                      &lt;td&gt;
                          &lt;input type="text" name="status" class="text" value="&lt;?php echo set_value('status',$this-&gt;form_data-&gt;status); ?&gt;"/&gt;
                          &lt;?php echo form_error('status'); ?&gt;
                      &lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;tr&gt;
                      &lt;td valign="top"&gt;Type&lt;span style="color:red;"&gt;*&lt;/span&gt;&lt;/td&gt;
                      &lt;td&gt;
                          &lt;input type="text" name="type" class="text" value="&lt;?php echo set_value('type',$this-&gt;form_data-&gt;type); ?&gt;"/&gt;
                          &lt;?php echo form_error('type'); ?&gt;
                      &lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;tr&gt;
                      &lt;td&gt;&amp;nbsp;&lt;/td&gt;
                      &lt;td&gt;&lt;input type="submit" value="Save" /&gt;&lt;/td&gt;
                  &lt;/tr&gt;
              &lt;/table&gt;
          &lt;/div&gt;
      &lt;/form&gt;
      &lt;br /&gt;
      &lt;?php echo $link_back; ?&gt;
  &lt;/div&gt;
  </code></pre>