簡體   English   中英

在blueimp / jquery-file-upload上向mysql插入中添加更多自定義變量

[英]Add more custom variables to mysql insert on blueimp/jquery-file-upload

我目前正在通過mysql在blueimp / jquery-file-upload腳本中插入標題和描述。 我使用教程可以到達那里,但是,我需要添加另一個變量。 該變量是當前已登錄用戶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);

假設您要更改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;
}
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;
    }`

我認為您必須在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/zh-cn/mysqli-stmt.bind-param.php中的參數類型部分)。

奇怪的是,我嘗試傳遞文字字符串作為最后一個參數,這引發了此錯誤:

“ PHP致命錯誤:在第66行的/var/www/html/jqfileuploadertest/server/php/index.php中無法通過引用傳遞參數7”

但是,當我將“ mystring”更改為$ file-> name時,它可以正常工作。 這將需要一些努力,因為我想為這些時間戳記...我感覺只是添加“ NOW()”是行不通的...


更新:我想為每個文件添加兩個字段,即上載文件的人的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];

並修改了准備SQL語句的index.php的這一部分:

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
        );

而且有效。

上傳文件時,您在JavaScript控制台中遇到的錯誤是對的嗎? 在我看來,正在加載的文件之一未找到404,並且它實際上嘗試將接收到的404頁解析為腳本文件,而實際上它是一個以<...開頭的html文件,我以前曾經發生過這種情況。 在網絡下檢查並查看發生時正在加載的所有資源。 也可能沒有返回正確的404標頭,這就是為什么它將嘗試解析它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM