This question already has an answer here:
这个问题在这里已有答案:
- When to use single quotes, double quotes, and back ticks in MySQL 12 answers
何时在MySQL 12答案中使用单引号,双引号和反向标记
I am creating a separate page in my portal from where i can upload the images to my database. but while submitting the image i am getting the following Error:
我在门户网站中创建了一个单独的页面,我可以将图像上传到我的数据库。但在提交图像时,我收到以下错误:
"error in INSERT into 'images_tbl' ('images_path','submission_date') VALUES ('images/24-01-2016-1453612538.jpg','2016-01-24') 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','submission_date') VALUES ('images/24-01-2016-14' at line 1"
“插入'images_tbl'('images_path','submission_date')中的错误VALUES('images / 24-01-2016-1453612538.jpg','2016-01-24')你的SQL语法有错误;查看与您的MySQL服务器版本对应的手册,以便在''images_tbl'附近使用正确的语法('images_path','submission_date')VALUES('images / 24-01-2016-14'at line 1“
I am getting this error in my saveimage.php file. Why exactly this error is?
我在saveimage.php文件中收到此错误。为什么会出现这个错误?
Here is my HTML code:
这是我的HTML代码:
<html>
<head>
<title>Image Upload</title>
</head>
<body>
<form action="saveimage.php" enctype="multipart/form-data" method="post">
<table style="border-collapse: collapse; font: 12px Tahoma;" border="1" cellspacing="5" cellpadding="5">
<tbody>
<tr>
<td>
<input name="uploadedimage" type="file">
</td>
</tr>
<tr>
<td>
<input name="Upload Now" type="submit" value="Upload Image">
</td>
</tr>
</tbody>
</table>
</form>
</body>
</html>
PHP code of my saveimage.php file:
我的saveimage.php文件的PHP代码:
<?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','submission_date') VALUES ('".$target_path."','".date("Y-m-d")."')";
mysql_query($query_upload) or die("error in $query_upload ".mysql_error());
}else{
exit("Error While uploading image on the server");
}
}
?>
Code of my mysqlconnect.php file is:
我的mysqlconnect.php文件的代码是:
<?php
/**********MYSQL Settings****************/
$host="localhost";
$databasename="demo";
$user="root";
$pass="";
/**********MYSQL Settings****************/
$conn=mysql_connect($host,$user,$pass);
if($conn)
{
$db_selected = mysql_select_db($databasename, $conn);
if (!$db_selected) {
die ('Can\'t use foo : ' . mysql_error());
}
}
else
{
die('Not connected : ' . mysql_error());
}
?>
1 个解决方案
#1
1
Remove single quotes from your table name and column names, use backticks instead. So your query should be like this:
从表名和列名中删除单引号,改为使用反引号。所以你的查询应该是这样的:
$query_upload="INSERT into `images_tbl`(`images_path`,`submission_date`) VALUES ('".$target_path."','".date("Y-m-d")."')";
Sidenote: Don't use mysql_*
functions, they are deprecated as of PHP 5.5 and are removed altogether in PHP 7.0. Use mysqli
or PDO
instead. And this is why you shouldn't use mysql_*
functions.
旁注:不要使用mysql_ *函数,它们自PHP 5.5起就被弃用,并在PHP 7.0中被完全删除。请改用mysqli或PDO。这就是为什么你不应该使用mysql_ *函数。
#1
1
Remove single quotes from your table name and column names, use backticks instead. So your query should be like this:
从表名和列名中删除单引号,改为使用反引号。所以你的查询应该是这样的:
$query_upload="INSERT into `images_tbl`(`images_path`,`submission_date`) VALUES ('".$target_path."','".date("Y-m-d")."')";
Sidenote: Don't use mysql_*
functions, they are deprecated as of PHP 5.5 and are removed altogether in PHP 7.0. Use mysqli
or PDO
instead. And this is why you shouldn't use mysql_*
functions.
旁注:不要使用mysql_ *函数,它们自PHP 5.5起就被弃用,并在PHP 7.0中被完全删除。请改用mysqli或PDO。这就是为什么你不应该使用mysql_ *函数。