[英]Blueimp File upload— insert form data into multiple database tables
我有两个数据库表,即photos
, albums
。 我可以上传照片并将表单数据立即插入两个表中,这是可以的。
我的问题是
当我上传5张照片时,每张桌子都会创建5行-每张照片的每一行。
我想要的是
在photos
表中应创建5行, 但在albums
表中仅应创建一行 。 photos
表具有albums
表的foreign key
。
这是我的代码如下:
扩展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);
?>
在上面的代码中,我将代码块注释为
//LABEL: PROBLEM BLOCK BEGINS
...
//LABEL: PROBLEM BLOCK ENDS.
上面的代码用于在photos
表和albums
表中插入相同数量的行。 我需要有关PROBLEM BLOCK
帮助,才能为我在照片表中上传的每批photos
在albums
表中仅创建一行。
我自己想出了解决方案。 我在album
表中添加了unique id
列,并在上传表单上将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
);
而已; 我们完了。 希望这可以帮助某人
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.