简体   繁体   English

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

[英]Blueimp File upload— insert form data into multiple database tables

I have two database tables namely, photos , albums . 我有两个数据库表,即photosalbums I am able to upload photos and insert form data into both tables at once which is okay. 我可以上传照片并将表单数据立即插入两个表中,这是可以的。

The problem I have is that, 我的问题是

when I upload eg, 5 photos, 5 rows are created into each table-- each row for each photo. 当我上传5张照片时,每张桌子都会创建5行-每张照片的每一行。

What I want is that, 我想要的是

5 rows should be created in the photos table but only one row should go into albums table. photos表中应创建5行, albums表中应创建一行 The photos table has foreign key to albums table. photos表具有albums表的foreign key

Here is my code below: 这是我的代码如下:

The index.php which extends the UploadHandler handler has the following code: 扩展UploadHandler处理程序的index.php具有以下代码:

<?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);

?>

In the code above, I have commented block of code as 在上面的代码中,我将代码块注释为

//LABEL: PROBLEM BLOCK BEGINS

...

//LABEL: PROBLEM BLOCK ENDS.

The code above works to insert same number of rows in both photos table and albums table. 上面的代码用于在photos表和albums表中插入相同数量的行。 I need help with the PROBLEM BLOCK in order to create only one row in albums table for each batch of photos I upload in photos table. 我需要有关PROBLEM BLOCK帮助,才能为我在照片表中上传的每批photosalbums表中仅创建一行。

I figured out the solution myself. 我自己想出了解决方案。 I added a unique id column to the album table and I added the value of the unique id as hidden field on the upload form so it became like this 我在album表中添加了unique id列,并在上传表单上将unique id的值添加为hidden字段,因此它变成了这样

the album table 专辑表

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)
)

In the upload form, I added 在上传表单中,我添加了

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

You will need to create your own function to get the variable $variable-data which will be different for each new photo upload. 您将需要创建自己的函数来获取变量$variable-data ,该变量对于每次上传的新照片都会有所不同。

With this, the CustomUploadHanler.php will send queries to insert multiple rows in the album table but the table will accept only one row for each batch of photos uploaded. 这样, CustomUploadHanler.php将发送查询以在album表中插入多行,但是对于每一批上载的照片,该表仅接受一行。 Perhaps there is a better solution out there but this worked for me for now. 也许那里有更好的解决方案,但这对我来说还是行得通的。

Also do the following changes in the code in the OP. 此外,还要在OP中的代码中进行以下更改。

change the: 更改:

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

to

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];
}

Then in the PROBLEM BLOCK, change: 然后在“问题块”中,更改:

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

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

to

$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

    );

That's it; 而已; we are done. 我们完了。 Hope this helps someone 希望这可以帮助某人

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM