代码点火器CRUD应用程序:缩略图图像无法上传。接收数据库错误,因为NULL

时间:2012-10-07 16:10:57

标签: php codeigniter codeigniter-2

制作一个可接受大量文字数据和图片上传的表单。

在向两个表添加新项目时继续收到此错误:

  

错误号码:1048

     

列'id_path'不能为空

     

INSERT INTO thumbnailsid_pathid_data_row)VALUES(NULL,17)

     

文件名:C:\ wamp \ www \ project \ system \ database \ DB_driver.php

     

行号:330

我要做的是将文本数据(标题,文字,价格等内容)添加到一个表格中(数据),然后上传图片并添加完整的上传路径(作为缩略图表中的主键)和插入的文本数据行的ID(将其与缩略图表中的缩略图链接)到缩略图表。

由于某种原因,它不断传递NULL作为缩略图表的完整上传路径。

crud.php (控制器):

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 (模特):

//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();
        }

编辑(html表单代码):

crud_edit.php 添加新项目或更新现有项目的html表单代码):

<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>&nbsp;</td>
                    <td><input type="submit" value="Save" /></td>
                </tr>
            </table>
        </div>
    </form>
    <br />
    <?php echo $link_back; ?>
</div>

2 个答案:

答案 0 :(得分:1)

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;
                $thumbnail_row = array(
                'id_path' => $full_file_path,
                'id_data_row' => $id
            );
                $this->crud_model->save_thumbnail($thumbnail_row);
            }

我的猜测是上传失败,因为无论上传失败,你都试图进行保存,而full_file_path的值将为空。顺便说一下,只保存文件名并将路径放在显示代码中。保存路径会使将这些文件移动到另一个位置非常头疼

答案 1 :(得分:1)

更改php代码如下

$image1=$this->input->post('thumb');
if (!$this->upload->do_upload($image1)){