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