Blue-imp удаление файла из базы данных с дополнительной переменной

Я использую Blue-imp file-upload для загрузки файлов, все, кажется, работает отлично с одним исключением. Я использую данные, которые сохраняются в каталогах пользователей на основе их userId, но когда я пытаюсь удалить данные из базы данных, они удаляются с помощью переменной $ name, которая является именем файла.

Это означает, что все пользователи с файлом с одинаковым именем файла удаляются из базы данных. Как я могу установить удаление в моем файле uploadHander.php для удаления по имени и идентификатору пользователя? Или по идентификатору вставки?

класс CustomUploadHandler extends 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);

1

Решение

Я нашел одну ошибку в вашем коде

$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);
}
0

Другие решения

Других решений пока нет …