Blueimp文件上传 - 将表单数据插入多个数据库表

时间:2018-01-04 11:44:43

标签: php mysql jquery-file-upload blueimp

我有两个数据库表,即photosalbums。我可以一次上传照片并将表格数据插入两个表格中,这是可以的。

我遇到的问题是,

当我上传例如5张照片时,每张桌子都会创建5行 - 每张照片的每一行。

我想要的是

应在photos中创建5行,但只有一行应该进入albums表。 photos表格有foreign keyalbums表格。

以下是我的代码:

扩展index.php处理程序的UploadHandler具有以下代码:

<?php
$options = array(
    'delete_type' => 'POST',
    'db_host' => 'localhost',
    'db_user' => 'username',
    'db_pass' => 'password',
    'db_name' => 'test',
    'db_table' => 'photos'

);

error_reporting(E_ALL | E_STRICT);
require('UploadHandler.php');

class CustomUploadHandler extends UploadHandler {

    protected function initialize() {
        $this->db = new mysqli(
            $this->options['db_host'],
            $this->options['db_user'],
            $this->options['db_pass'],
            $this->options['db_name']

        );
        parent::initialize();
        $this->db->close();
    }

protected function handle_form_data($file, $index) {
    $file->title = @$_REQUEST['title'][$index];
    $file->description = @$_REQUEST['description'][$index];
}

protected function handle_file_upload($uploaded_file, $name, $size, $type, $error,
    $index = null, $content_range = null) {
        $file = parent::handle_file_upload(
            $uploaded_file, $name, $size, $type, $error, $index, $content_range
        );

        if (empty($file->error)) {
            $sql = 'INSERT INTO `'.$this->options['db_table']
                .'` (`name`, `size`, `type`, `title`, `description`)'
                .' VALUES (?,?, ?, ?, ?)';
            $query = $this->db->prepare($sql);
            $query->bind_param(
                'sisss',

                $file->name,
                $file->size,
                $file->type,
                $file->title,
                $file->description,
            );
            $query->execute();
            $file->id = $this->db->insert_id;

            //LABEL: PROBLEM BLOCK BEGINS
            /*Here, I am attempting to insert only row in the albums table 
             for each batch of photos I upload. So even if I upload 5 photos 
             into the photos table, only one row should be created in the albums table*/


            $sql2 = 'INSERT INTO `albums` (`album_title`, `album_description`)'
                .' VALUES (?,?)';
            $query2 = $this->db->prepare($sql2);
            $query2->bind_param(
                'ss',

                $file->title,
                $file->description,
            );
            $query2->execute();
            $file->id = $this->db->insert_id; 

            //LABEL: PROBLEM BLOCK ENDS
        }

        return $file;
    }


}

$upload_handler = new CustomUploadHandler($options);

?>

在上面的代码中,我将代码块注释为

//LABEL: PROBLEM BLOCK BEGINS

...

//LABEL: PROBLEM BLOCK ENDS.

上面的代码适用于在photos表和albums表中插入相同数量的行。我需要PROBLEM BLOCK的帮助才能在albums表格中为我在照片表格中上传的每批photos只创建一行。

1 个答案:

答案 0 :(得分:0)

我自己想出了解决方案。我在unique id表格中添加了album列,并在上传表单中添加了unique id作为hidden字段的值,因此它就像这样

专辑表

CREATE TABLE album (
  id int(11) unsigned NOT NULL AUTO_INCREMENT,
  album_title varchar(255) DEFAULT NULL,
  album_description text,
  special_album_id varchar(255) NOT NULL,

  PRIMARY KEY (id),
  UNIQUE (special_album_id)
)

在上传表单中,我添加了

<input type = "hidden" name = "special_album_id[]" value= "<?php echo $variable-data; ?>">

您需要创建自己的函数来获取变量$variable-data,这对于每次新照片上传都会有所不同。

有了这个,CustomUploadHanler.php将发送查询以在album表中插入多行,但是对于每批上传的照片,该表只接受一行。也许有一个更好的解决方案,但现在这对我有用。

还要对OP中的代码进行以下更改。

更改:

protected function handle_form_data($file, $index) {
    $file->title = @$_REQUEST['title'][$index];
    $file->description = @$_REQUEST['description'][$index];
}

protected function handle_form_data($file, $index) {
    $file->title = @$_REQUEST['title'][$index];
    $file->description = @$_REQUEST['description'][$index];
    $file->special_album_id = @$_REQUEST['special_album_id'][$index];
}

然后在问题块中,更改:

$sql2 = 'INSERT INTO `albums` (`album_title`, `album_description`)'
            .' VALUES (?,?)';
        $query2 = $this->db->prepare($sql2);
        $query2->bind_param(
            'ss',

            $file->title,
            $file->description,
        );

$sql2 = 'INSERT INTO `albums` (`album_title`, `album_description`, `special_album_id`)'
            .' VALUES (?,?,?)';
    $query2 = $this->db->prepare($sql2);
    $query2->bind_param(
        'sss',

         $file->title,
         $file->description,
         $file->special_album_id

    );

就是这样;我们完了。希望这有助于某人