douque8861 2014-12-05 13:14
浏览 158
已采纳

INSERT错误

I am working on a script that uploads an image to a server, and the path to a MySQL Database. When I submit this it comes up with this error:

error in INSERT into 'images_tbl' ('images_path') VALUES ('images/05-12-2014-1417785023.png') == ----> You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''images_tbl' ('images_path') VALUES ('images/05-12-2014-1417785023.png')' at line 1

Here is the code:

<?php
 include("mysqlconnect.php");

     function GetImageExtension($imagetype)
         {
        if(empty($imagetype)) return false;
        switch($imagetype)
        {
            case 'image/bmp': return '.bmp';
            case 'image/gif': return '.gif';
            case 'image/jpeg': return '.jpg';
            case 'image/png': return '.png';
            default: return false;
        }
      }



 if (!empty($_FILES["uploadedimage"]["name"])) {

     $file_name=$_FILES["uploadedimage"]["name"];
     $temp_name=$_FILES["uploadedimage"]["tmp_name"];
     $imgtype=$_FILES["uploadedimage"]["type"];
     $ext= GetImageExtension($imgtype);
     $imagename=date("d-m-Y")."-".time().$ext;
     $target_path = "images/".$imagename;


 if(move_uploaded_file($temp_name, $target_path)) {

      $query_upload="INSERT into 'images_tbl' ('images_path') VALUES ('".$target_path."')";
     mysql_query($query_upload) or die("error in $query_upload == ----> ".mysql_error());  

 }else{

    exit("Error While uploading image on the server");
 } 

 }

?>

My editor is not bringing up any syntax errors, but it seems to suggest there is in that error.

  • 写回答

4条回答 默认 最新

  • dsc862009 2014-12-05 13:17
    关注

    Identifier quotes are backticks not single quotes:

    INSERT into 'images_tbl' ('images_path')
                ^           ^             ^
    

    You could just ditch them instead.

    INSERT into images_tbl (images_path)
    // or
    INSERT into `images_tbl` (`images_path`)
    

    Obligatory Note:

    Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

    Ref: https://stackoverflow.com/a/12860140/3859027

    Here's a short example of a mysqli usage:

    $db = new mysqli('localhost', 'username', 'password', 'database');
    $query_upload = 'INSERT INTO images_tbl (images_path) VALUES (?)';
    $insert = $db->prepare($query_upload);
    $insert->bind_param('s', $target_path);
    $insert->execute();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试