mysql UPDATE查询无法通过php上传图片

时间:2016-04-06 07:41:59

标签: php mysql pdo sql-update

尝试通过php将图像上传到mysql,试图使用用户ID来更新行。
但是,不断给出这个错误:

  

SQLSTATE [42000]:语法错误或访问冲突:1064 SQL语法中有错误;查看与您的MySQL服务器版本对应的手册,以便使用接近'(image_type,image,image_size,image_name)VALUES(' images / 1459926006.png',' png& #39;'第1行

无法查看语法错误是什么。

PHP:

   function upload(){
/*** check if a file was uploaded ***/
if(is_uploaded_file($_FILES['userfile']['tmp_name']) && getimagesize($_FILES['userfile']['tmp_name']) != false)
    {
    /***  get the image info. ***/
    $size = getimagesize($_FILES['userfile']['tmp_name']);
    /*** assign our variables ***/
    $type = $size['mime'];
    $imgfp = fopen($_FILES['userfile']['tmp_name'], 'rb');
    $size = $size[3];
    $name = $_FILES['userfile']['name'];
    $maxsize = 99999999;


    /***  check the file is less than the maximum file size ***/
    if($_FILES['userfile']['size'] < $maxsize )
        {
        /*** connect to db ***/
        $dbh = new PDO("mysql:host=localhost;dbname=table", 'username', 'password');

                /*** set the error mode ***/
                $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

            /*** our sql query ***/
        $stmt = $dbh->prepare("UPDATE users (image_type,image, image_size, image_name) VALUES (?,?,?,?) WHERE user_id=?"); 

        /*** bind the params ***/
        $stmt->bindParam(1, $type);
        $stmt->bindParam(2, $imgfp, PDO::PARAM_LOB);
        $stmt->bindParam(3, $size);
        $stmt->bindParam(4, $name);
        $stmt->bindParam(4, $name);
        $stmt->bindParam(5, $_SESSION['user_id']);

        /*** execute the query ***/
        $stmt->execute();
        }
    else
        {
        /*** throw an exception is image is not of type ***/
        throw new Exception("File Size Error");
        }
    }
else
    {
    // if the file is not less than the maximum allowed, print an error
    throw new Exception("Unsupported Image Format!");
    }
}
?>

2 个答案:

答案 0 :(得分:2)

不要使用update query作为insert query语法两者都不同

更改

$stmt = $dbh->prepare("UPDATE users (image_type,image, image_size, image_name) VALUES (?,?,?,?) WHERE user_id=?"); 

$stmt = $dbh->prepare("UPDATE users SET image_type=?, image=?, image_size=1 ,image_name=?  WHERE user_id=?"); 

你绑定$stmt->bindParam(4, $name);两次

 $stmt->bindParam(1, $type);
        $stmt->bindParam(2, $imgfp, PDO::PARAM_LOB);
        $stmt->bindParam(3, $size);
        $stmt->bindParam(4, $name);
       // $stmt->bindParam(4, $name);// comment it
        $stmt->bindParam(5, $_SESSION['user_id']);

检查Update query syntax

答案 1 :(得分:1)

您的更新SQL查询语法错误。如下更改您的查询:

来自:

$stmt = $dbh->prepare("UPDATE users (image_type,image, image_size, image_name) VALUES (?,?,?,?) WHERE user_id=?");

更改为:

UPDATE `users`   
   SET `image_type` = ?,
       `image` = ?,
       `image_size` = ?,
       `image_name` = ? 
 WHERE `user_id` = ?

立即尝试!!!!!