我正在使用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 =新的CustomUploadHandler($options);
发布于 2014-11-08 14:00:31
我在你的代码中发现了一个错误
$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);
}https://stackoverflow.com/questions/26764905
复制相似问题