1

制作一个将接受一堆文本数据和图像上传的表单。

将新项目添加到两个表时不断收到此错误:

错误号:1048

列“id_path”不能为空

插入thumbnails( id_path, id_data_row) 值 (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>
4

2 回答 2

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 的值将为空。顺便说一句,只保存文件名并将路径放在你的显示代码中。保存路径使得将这些文件移动到另一个位置变得非常头疼

于 2012-10-07T16:25:25.030 回答
1

更改php代码如下

$image1=$this->input->post('thumb');
if (!$this->upload->do_upload($image1)){
于 2012-10-07T17:20:28.267 回答