2

I am currently inserting a title and description, via mysql, inside of the blueimp/jquery-file-upload script. I used this tutorial to get me there, however, i need to add another variable. The variable is the session of the current logged in user's ID $_SESSION["userid"], and i want to insert it into a column i added called uid. Usually it's simple to impliment another column into the insert, however this script is very touchy and anytime i mess with it, even the slightest bit, i get "SyntaxError: Unexpected token <". Any help would be greatly appreciated.

/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);
Alex Sarnowski
  • 749
  • 1
  • 11
  • 21
  • what is the specific error message that you are getting and in what line of the code ? – Maximus2012 Mar 19 '14 at 20:52
  • `SyntaxError: Unexpected token <` – Alex Sarnowski Mar 19 '14 at 20:54
  • which line of the code ? This error message by itself is not very helpful. It could be anywhere. – Maximus2012 Mar 19 '14 at 20:55
  • I am getting it after i attempt to upload the image (when i attempt edit the index "insert into" sql in any way), however when i leave it the way the tutorial instructed, no error. – Alex Sarnowski Mar 19 '14 at 21:01
  • I think you posted a lot of code. Maybe if you could only post the code that is having issues along with possibly the line number then that would be helpful. Do you have error reporting turned on for your application: http://stackoverflow.com/questions/6575482/how-do-i-enable-error-reporting-in-php – Maximus2012 Mar 19 '14 at 21:03
  • okay, my question is simple. I would like to add a `$_SESSION["userid"]` as a value to a uid column in the FILES table...i need to make the code do so, simply add another column to the insert. – Alex Sarnowski Mar 19 '14 at 21:11
  • do you already have a column for user_id in your $this->options['db_table'] table ? That is the first thing that you need to do. – Maximus2012 Mar 19 '14 at 21:13
  • look at the answer that I posted. See if that helps. – Maximus2012 Mar 19 '14 at 21:18

4 Answers4

0

Assuming you want to change your INSERT query (there is only one INSERT query in the code that you posted), this is what you need to change:

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;
}
Maximus2012
  • 1,799
  • 2
  • 12
  • 15
  • again, i am recieveing the `SyntaxError: Unexpected token <` error. – Alex Sarnowski Mar 19 '14 at 21:20
  • are you familiar with blueimp? – Alex Sarnowski Mar 19 '14 at 21:21
  • no but since it is PHP it would be helpful if you could indicate the line of the code that is having issues. Is it possible that the error is coming from entirely different part of the code and it has nothing to do with the changes that you made or the changes that I suggested ? – Maximus2012 Mar 19 '14 at 21:22
  • it's echoing on add.php.., because anytime you edit anything on blueimp it seems as if this error is displayed via ajax, under the image you try to upload. It's a generic error...it comes up anytime anything is incorrect. – Alex Sarnowski Mar 19 '14 at 21:24
  • Then it is possible that you might be getting the error elsewhere in the code. Without further information, neither me nor anyone else would be able to help you much. – Maximus2012 Mar 19 '14 at 21:25
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;
    }`
Phrixus
  • 1,209
  • 2
  • 19
  • 36
issa
  • 1
  • 1
  • I did this and i am still recieving the error. I added `session_start();` and changed the line to `$query->bind_param('sissss',` and added a `$_SESSION['userid']`....and still not working. – Alex Sarnowski Mar 23 '14 at 21:03
0

I think you have to add another entry into the first parameter of bind_param:

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

should be

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

Minus the asterisks, of course, or whatever the correct data type is (see the parameter types section at http://us.php.net/manual/en/mysqli-stmt.bind-param.php).

One oddity, I tried passing a literal string as the last param, and it threw this error:

"PHP Fatal error: Cannot pass parameter 7 by reference in /var/www/html/jqfileuploadertest/server/php/index.php on line 66"

But when I changed 'mystring' to $file->name it worked fine. This will take some wrestling because I want to timestamp these...I have a feeling just adding "NOW()" isn't going to work...


Updated: I wanted to add two fields for each file, the ID of who uploaded it (which I think is what the OP is looking to do) as well as a timestamp. I added those as hidden inputs in the form:

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

If your form is a php script you can dynamically generate both.

Then added this to index.php line ~44:

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

And modified this part of index.php where the sql statement is prepared:

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

And it worked.

Tom McGee
  • 76
  • 7
0

The error you're getting is in the javascript console when you upload the file right? I seems to me that one of the files thats being loaded is 404 not found, and its trying to parse the received 404 page as a script file when really its an html file that starts with <... I had this happen once before. Check under network and view all the resources that are being loaded when it happens. Its likely that the correct 404 headers are not being returned either which is why it would be attempting to parse it.

Rick Kukiela
  • 1,135
  • 1
  • 15
  • 34