dongyu9894 2016-10-29 21:40
浏览 46

jQuery文件上传PHP MySQL集成无法正常工作

I would like to use jQuery-File-Upload with what I'm working on. I was able to use it without issues but I'm stucked when I try to save the data of the image I upload on my database even if I followed the instructions stated here.

Here are the files that I've changed from the original:

  • index.html (I added this on <script id="template-upload" type="text/x-tmpl">)
<label class="title">
<span>Title:</span><br>
<input name="title[]" class="form-control">
</label>
<label class="description">
<span>Description:</span><br>
<input name="description[]" class="form-control">
</label>

(I added this on <script id="template-download" type="text/x-tmpl">)

<p class="title"><strong>{%=file.title||''%}</strong></p>
<p class="description">{%=file.description||''%}</p>
  • main.js
$(function () {
    'use strict';
    $('#fileupload').fileupload({
        url: 'server/php/'
    }).on('fileuploadsubmit', function (e, data) {
        data.formData = data.context.find(':input').serializeArray();
    });
});
  • index.php
$options = array(
    'delete_type' => 'POST',
    'db_host' => 'localhost',
    'db_user' => 'root',
    'db_pass' => 'eL!m@5%ie',
    'db_name' => 'u922477615_pmp',
    'db_table' => 'files'
);

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;
    }
    return $file;
}

protected function set_additional_file_properties($file) {
    parent::set_additional_file_properties($file);
    if ($_SERVER['REQUEST_METHOD'] === 'GET') {
        $sql = 'SELECT `id`, `type`, `title`, `description` FROM `'
            .$this->options['db_table'].'` WHERE `name`=?';
        $query = $this->db->prepare($sql);
        $query->bind_param('s', $file->name);
        $query->execute();
        $query->bind_result(
            $id,
            $type,
            $title,
            $description
        );
        while ($query->fetch()) {
            $file->id = $id;
            $file->type = $type;
            $file->title = $title;
            $file->description = $description;
        }
    }
}

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`=?';
            $query = $this->db->prepare($sql);
            $query->bind_param('s', $name);
            $query->execute();
        }
    } 
    return $this->generate_response($response, $print_response);
 }
}

I also added this in my database:

CREATE TABLE `files` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`size` int(11) DEFAULT NULL,
`type` varchar(255) DEFAULT NULL,
`url` varchar(255) DEFAULT NULL,
`title` varchar(255) DEFAULT NULL,
`description` text,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

But I get this:

enter image description here

I would really like to try and experiment more but I'm lack on time so I'd appreciate anyone's help.

Also, I have read other discussions but it did not work for me.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 gg加速器加速游戏时,提示不是x86架构
    • ¥15 python按要求编写程序
    • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
    • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
    • ¥15 opencv图像处理,需要四个处理结果图
    • ¥15 无线移动边缘计算系统中的系统模型
    • ¥15 深度学习中的画图问题
    • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条
    • ¥15 Python报错怎么解决
    • ¥15 simulink如何调用DLL文件