Php 尝试将映像存储到数据库时发生sql错误(已更新)
Php 尝试将映像存储到数据库时发生sql错误(已更新),php,sql,database,upload,Php,Sql,Database,Upload,UPDATED:根据评论部分的建议,我已经更新了我的代码,现在正在使用PDO。我仍然有同样的错误。 我正在尝试将多个图像存储到数据库(phpmyadmin)。当我试图上传时,它给出了一个错误error:SQLSTATE[42S22]:未找到列:1054“field list”中的未知列“file.png” 我的数据库: 我有一个名为image_upload的数据库,其中有一个名为car_details的表,该表有一个id(int11)、car_名称(varchar 255)和imageOfcar
UPDATED:
根据评论部分的建议,我已经更新了我的代码,现在正在使用PDO。我仍然有同样的错误。
我正在尝试将多个图像存储到数据库(phpmyadmin)。当我试图上传时,它给出了一个错误error:SQLSTATE[42S22]:未找到列:1054“field list”中的未知列“file.png”
我的数据库:
我有一个名为image_upload的数据库,其中有一个名为car_details的表,该表有一个id(int11)、car_名称(varchar 255)和imageOfcar(longblob)
这是我的图像详细信息:
Array
(
[0] => Array
(
[name] => file.png
[type] => image/png
[tmp_name] => /opt/lampp/temp/phpJYyrQn
[error] => 0
[size] => 77776
)
[1] => Array
(
[name] => files.png
[type] => image/png
[tmp_name] => /opt/lampp/temp/phpXOLvzL
[error] => 0
[size] => 84710
)
)
正如评论部分所建议的,我现在正在使用PDO
这是我的代码:(更新)
试验
如何修复我提到的显示在顶部的错误?如评论部分所述。我首先将代码更改为PDO,并没有在没有任何反勾号或“”的情况下插入数据库,而是按照注释部分中的建议,在将图像插入数据库时添加了反勾号和“”
$conn->exec(“插入到
car\u details(
car\u name,
imageOfcar)值(“$carname',“$imagename”)代码>
现在我可以将图像插入到我的数据库中。我也在这里更新了我的代码
我的代码:
试验
您没有将相应的查询插入car\u details…
。但是,我希望您不要在生产中使用roor访问(没有密码)system@B001ᛦ 抱歉,我在代码中编辑了一些内容,但在将其发布到此处之前忘记将其更改回原处。我再次编辑了代码。现在我正在使用无密码(只是为了练习)当我在现实生活中的某个地方实际使用代码时,我会添加一个密码。multi_query
与您的SQL注入问题有关,这是一个大问题。另外,修复SQL注入将修复当前的问题。这是否回答了您的问题。。。或者一个重复,但这只是把问题推向了更进一步的道路。
<!doctype html>
<html>
<head>
<title>Test</title>
</head>
<body>
<form method="post" enctype="multipart/form-data">
<input type="file" name="userfile[]" multiple="" />
<input type="submit" name="submit" value="upload" />
</form>
<?php
$servername="localhost";
$username="root";
$password = "";
$dbname="image_upload";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname",$username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// begin the transaction
$conn->beginTransaction();
$phpFileUploadErrors = array(
0 => "There is no error, the file uploaded with success",
1 => "The uploaded file exceeds the upload_max_filesize directive in php.ini",
2 => "The upload file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form",
3 => "The upload file was only partially uploaded",
4 => "No file was uploaded",
6 => "Missing a temporary folder",
7 => "Failed to write file to disk.",
8 => "A php extension stopped the file upload"
);
if(isset($_FILES['userfile'])) {
$file_array = reArrayFiles($_FILES['userfile']);
pre_r($file_array);
for($i=0;$i<count($file_array);$i++) {
$carname = $file_array[$i]["tmp_name"];
$tablename = "car_detailss";
$imagename = $file_array[$i]["name"];
pre_r($carname);
pre_r($imagename);
$conn->exec("INSERT INTO `car_detailss`(`car_name`, `imageOfcar`) VALUES ( $carname,$imagename)");
}
}
$conn->commit();
echo "new records created succesfully";
} catch(PDOException $e) {
// roll back the transaction if something failed
$conn->rollback();
echo "Error: " . $e->getMessage();
}
$conn = null;
function reArrayFiles($file_post) {
$file_ary = array();
$file_count = count($file_post['name']);
$file_keys = array_keys($file_post);
for ($i = 0; $i < $file_count; $i++){
foreach($file_keys as $key) {
$file_ary[$i][$key] = $file_post[$key][$i];
}
}
return $file_ary;
}
function pre_r($array) {
echo '<pre>';
print_r($array);
echo '</pre>';
}
?>
</body>
</html>
<!doctype html>
<html>
<head>
<title>Test</title>
</head>
<body>
<form method="post" enctype="multipart/form-data">
<input type="file" name="userfile[]" multiple="" />
<input type="submit" name="submit" value="upload" />
</form>
<?php
$servername="localhost";
$username="root";
$password = "";
$dbname="image_upload";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname",$username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// begin the transaction
$conn->beginTransaction();
$phpFileUploadErrors = array(
0 => "There is no error, the file uploaded with success",
1 => "The uploaded file exceeds the upload_max_filesize directive in php.ini",
2 => "The upload file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form",
3 => "The upload file was only partially uploaded",
4 => "No file was uploaded",
6 => "Missing a temporary folder",
7 => "Failed to write file to disk.",
8 => "A php extension stopped the file upload"
);
if(isset($_FILES['userfile'])) {
$file_array = reArrayFiles($_FILES['userfile']);
pre_r($file_array);
for($i=0;$i<count($file_array);$i++) {
$carname = $file_array[$i]["size"];
$tablename = "car_detailss";
$imagename = $file_array[$i]["name"];
$conn->exec( "INSERT INTO `car_detailss` (`car_name`, `imageOfcar`) VALUES ('$carname', '$imagename')");
}
}
$conn->commit();
echo "new records created succesfully";
} catch(PDOException $e) {
// roll back the transaction if something failed
$conn->rollback();
echo "Error: " . $e->getMessage();
}
$conn = null;
function reArrayFiles($file_post) {
$file_ary = array();
$file_count = count($file_post['name']);
$file_keys = array_keys($file_post);
for ($i = 0; $i < $file_count; $i++){
foreach($file_keys as $key) {
$file_ary[$i][$key] = $file_post[$key][$i];
}
}
return $file_ary;
}
function pre_r($array) {
echo '<pre>';
print_r($array);
echo '</pre>';
}
?>
</body>
</html>