在blueimp / jquery-file-upload上为mysql insert添加更多自定义变量

时间:2014-03-19 20:46:03

标签: php jquery mysql blueimp

我目前正在通过mysql在blueimp / jquery-file-upload脚本中插入标题和描述。我使用this教程将我带到那里,但是,我需要添加另一个变量。该变量是当前登录用户ID $_SESSION["userid"]的会话,我想将其插入到我添加的名为uid的列中。通常很容易将另一列插入到插入中,但是这个脚本非常敏感,任何时候我都搞乱它,即使是最轻微的一点,我得到“SyntaxError: Unexpected token <”。任何帮助将不胜感激。

/server/php/index.php

$options = array(
    'delete_type' => 'POST',
    'db_host' => 'localhost',
    'db_user' => 'fpform_fanuser',
    'db_pass' => '*****',
    'db_name' => 'fpform_fandata',
    '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);
    }

}


$upload_handler = new CustomUploadHandler($options);

4 个答案:

答案 0 :(得分:0)

假设您要更改INSERT查询(您发布的代码中只有一个INSERT查询),这就是您需要更改的内容:

if (empty($file->error)) {
    $sql = 'INSERT INTO `'.$this->options['db_table']
            .'` (`name`, `size`, `type`, `title`, `description`, `uid`)'
            .' VALUES (?, ?, ?, ?, ?, ?)';
    $query = $this->db->prepare($sql);
    $query->bind_param(
            'sisss',
            $file->name,
            $file->size,
            $file->type,
            $file->title,
            $file->description,
            $_SESSION['userid']
        );
    $query->execute();
    $file->id = $this->db->insert_id;
}

答案 1 :(得分:0)

protected function handle_file_upload($uploaded_file, $name, $size, $type, $error,
            $index = null, $content_range = null, $uid) {
        $file = parent::handle_file_upload(
            $uploaded_file, $name, $size, $type, $error, $index, $content_range, $uid
        );
        $uid=$_SESSION['userid'];
        if (empty($file->error)) {
            $sql = 'INSERT INTO `'.$this->options['db_table']
                .'` (`name`, `size`, `type`, `title`, `description`,`uid`)'
                .' VALUES (?, ?, ?, ?, ?,?)';
            $query = $this->db->prepare($sql);
            $query->bind_param(
                'sisssi',
                $file->name,
                $file->size,
                $file->type,
                $file->title,
                $file->description,
                $file->uid
            );
            $query->execute();
            $file->id = $this->db->insert_id;
        }
        return $file;
    }`

答案 2 :(得分:0)

我认为你必须在bind_param的第一个参数中添加另一个条目:

$query->bind_param(
        **'sisss',**
        $file->name,
        $file->size,
        $file->type,
        $file->title,
        $file->description,
        $_SESSION['userid']

应该是

$query->bind_param(
        **'sissss',**
        $file->name,
        $file->size,
        $file->type,
        $file->title,
        $file->description,
        $_SESSION['userid']

当然,减去星号或任何正确的数据类型(请参阅http://us.php.net/manual/en/mysqli-stmt.bind-param.php处的参数类型部分)。

有一个奇怪的是,我尝试将一个文字字符串作为最后一个参数传递,并且它抛出了这个错误:

&#34; PHP致命错误:无法在第66和34行的/var/www/html/jqfileuploadertest/server/php/index.php中通过引用传递参数7;

但是当我改变了#mys;&#39;到$ file-&gt;名称它工作正常。这将需要一些摔跤,因为我想给这些时间戳...我有一种感觉只是添加&#34; NOW()&#34;没有去上班......


更新:我想为每个文件添加两个字段,上传者的ID(我认为OP正在寻找的内容)以及时间戳。我在表格中添加了隐藏的输入:

<input name="contributor[]" type="hidden" value="myuserid" />
<input name="uploadedOn[]" type="hidden" value="2014-03-28 10:00:00" />

如果您的表单是php脚本,则可以动态生成两者。

然后将其添加到index.php第44行:

    $file->contributor = @$_REQUEST['contributor'][$index];
    $file->uploadedOn = @$_REQUEST['uploadedOn'][$index];

并修改了index.php的这一部分,其中准备了sql语句:

if (empty($file->error)) {
        $sql = 'INSERT INTO `'.$this->options['db_table']
            .'` (`name`, `size`, `type`, `title`, `description`,`contributor`, `dateUploaded`)'
            .' VALUES (?, ?, ?, ?, ?, ?, ?)';
        $query = $this->db->prepare($sql);
        $query->bind_param(
            'sisssss',
            $file->name,
            $file->size,
            $file->type,
            $file->title,
            $file->description,
            $file->contributor,
            $file->uploadedOn
        );

它有效。

答案 3 :(得分:0)

当您正确上传文件时,您在javascript控制台中收到的错误是什么?在我看来,正在加载的文件之一是404找不到,并且它试图将收到的404页面解析为脚本文件,而实际上它是一个以&lt; ...开头的html文件。在网络下检查并查看发生时正在加载的所有资源。可能没有返回正确的404标头,这就是为什么它会尝试解析它。