gpt4 book ai didi

php - 插入错误

转载 作者:行者123 更新时间:2023-11-29 01:16:31 26 4
gpt4 key购买 nike

我正在编写一个将图像上传到服务器的脚本,以及到 MySQL 数据库的路径。当我提交它时出现了这个错误:

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

代码如下:

<?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");
}

}

?>

我的编辑器没有提出任何语法错误,但似乎表明存在该错误。

最佳答案

标识符引号是反引号而不是单引号:

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

你可以直接抛弃它们。

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

强制性说明:

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

这是一个 mysqli 用法的简短示例:

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

关于php - 插入错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27316735/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com