我正在使用Blue-imp文件上传来上传文件,一切看起来效果都很好。我将用户数据基于userId保存到用户目录,但是当我尝试从数据库中删除数据时,它会删除$ name变量,即文件名。
这意味着具有相同文件名的文件的所有用户都将从数据库中删除。如何设置uploadHander.php文件中的删除按名称和userId删除?或者通过插入ID?
类CustomUploadHandler扩展了UploadHandler {
protected function handle_form_data($file, $index) {
$file->description = $_REQUEST['description'][$index];
$file->userId = $_REQUEST['userId'][$index];
$file->location = $_REQUEST['location'][$index];
$file->customId = $_REQUEST['customId'][$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`, `userId`, `description`,`location`,`customId`,`url`)'
.' VALUES (?, ?, ?, ?, ?, ?,?,?)';
$query = $this->db->prepare($sql);
$query->bind_param(
'sissssss',
$file->name,
$file->size,
$file->type,
$file->userId,
$file->description,
$file->location,
$file->customId,
$file->url
);
$query->execute();
$file->id = $this->db->insert_id;
}
return $file;
}
protected function set_additional_file_properties($file) {
parent::set_additional_file_properties($file);
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
$sql = 'SELECT `type`, `description`,`location`,`userId`,`customId`,`url` FROM `'
.$this->options['db_table'].'` WHERE `name`=? and `id`=?';
$query = $this->db->prepare($sql);
$query->bind_param('ss', $file->name,$file->id);
$query->execute();
$query->bind_result(
$type,
$description,
$location,
$userId,
$customId,
$url
);
while ($query->fetch()) {
$file->type = $type;
$file->description = $description;
$file->location = $location;
$file->userId = $userId;
$file->customId=$customId;
$file->url=$url;
}
}
}
public function delete($print_response = true) {
$response = parent::delete(false);
foreach ($response as $name => $deleted) {
if ($deleted) {
$sql = 'DELETE FROM `'.$this->options['db_table'].'` WHERE `name`=? AND `id`=?';
$query = $this->db->prepare($sql);
$query->bind_param('ss',$name,$id);
$query->execute();
}
}
return $this->generate_response($response, $print_response);
}
}
$ upload_handler = new CustomUploadHandler($ options);
答案 0 :(得分:0)
我在您的代码中发现了一个错误
$query->bind_param('ss',$name,$id);
使用
query->bind_param('si',$name,$id);
$ id是一个整数,你用字符串定义它。
希望这对你有用。
使用此代码==>
public function delete($print_response = true) {
$response = parent::delete(false);
foreach ($response as $name => $deleted) {
if ($deleted) {
$sql = 'DELETE FROM `'.$this->options['db_table'].'` WHERE `name`=? AND `id`=?';
$query = $this->db->prepare($sql);
$query->bind_param('si',$name,$id);
$query->execute();
}
}
return $this->generate_response($response, $print_response);
}