尝试将图像存储到数据库时发生sql错误(已更新)

icomxhvb  于 2021-07-24  发布在  Java
关注(0)|答案(1)|浏览(446)

UPDATED: 正如在评论部分所建议的,我已经更新了我的代码并使用了pdo。我还是有同样的错误。我正在尝试将多个图像存储到数据库(phpmyadmin)。当我试图上传它给了一个错误 Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'file.png' in 'field list' 我的数据库:
我有一个名为image\u upload的数据库,里面有一个名为car\u details的表,其中有一个id(int11)、car\u name(varchar 255)和imageofcar(longblob)。
这是我的图像细节:

  1. Array
  2. (
  3. [0] => Array
  4. (
  5. [name] => file.png
  6. [type] => image/png
  7. [tmp_name] => /opt/lampp/temp/phpJYyrQn
  8. [error] => 0
  9. [size] => 77776
  10. )
  11. [1] => Array
  12. (
  13. [name] => files.png
  14. [type] => image/png
  15. [tmp_name] => /opt/lampp/temp/phpXOLvzL
  16. [error] => 0
  17. [size] => 84710
  18. )
  19. )

正如评论部分所建议的,我现在正在使用pdo,这是我的代码:(更新)

  1. <!doctype html>
  2. <html>
  3. <head>
  4. <title>Test</title>
  5. </head>
  6. <body>
  7. <form method="post" enctype="multipart/form-data">
  8. <input type="file" name="userfile[]" multiple="" />
  9. <input type="submit" name="submit" value="upload" />
  10. </form>
  11. <?php
  12. $servername="localhost";
  13. $username="root";
  14. $password = "";
  15. $dbname="image_upload";
  16. try {
  17. $conn = new PDO("mysql:host=$servername;dbname=$dbname",$username, $password);
  18. $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  19. // begin the transaction
  20. $conn->beginTransaction();
  21. $phpFileUploadErrors = array(
  22. 0 => "There is no error, the file uploaded with success",
  23. 1 => "The uploaded file exceeds the upload_max_filesize directive in php.ini",
  24. 2 => "The upload file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form",
  25. 3 => "The upload file was only partially uploaded",
  26. 4 => "No file was uploaded",
  27. 6 => "Missing a temporary folder",
  28. 7 => "Failed to write file to disk.",
  29. 8 => "A php extension stopped the file upload"
  30. );
  31. if(isset($_FILES['userfile'])) {
  32. $file_array = reArrayFiles($_FILES['userfile']);
  33. pre_r($file_array);
  34. for($i=0;$i<count($file_array);$i++) {
  35. $carname = $file_array[$i]["tmp_name"];
  36. $tablename = "car_detailss";
  37. $imagename = $file_array[$i]["name"];
  38. pre_r($carname);
  39. pre_r($imagename);
  40. $conn->exec("INSERT INTO `car_detailss`(`car_name`, `imageOfcar`) VALUES ( $carname,$imagename)");
  41. }
  42. }
  43. $conn->commit();
  44. echo "new records created succesfully";
  45. } catch(PDOException $e) {
  46. // roll back the transaction if something failed
  47. $conn->rollback();
  48. echo "Error: " . $e->getMessage();
  49. }
  50. $conn = null;
  51. function reArrayFiles($file_post) {
  52. $file_ary = array();
  53. $file_count = count($file_post['name']);
  54. $file_keys = array_keys($file_post);
  55. for ($i = 0; $i < $file_count; $i++){
  56. foreach($file_keys as $key) {
  57. $file_ary[$i][$key] = $file_post[$key][$i];
  58. }
  59. }
  60. return $file_ary;
  61. }
  62. function pre_r($array) {
  63. echo '<pre>';
  64. print_r($array);
  65. echo '</pre>';
  66. }
  67. ?>
  68. </body>
  69. </html>

如何修复我提到的显示在顶部的错误?

jm2pwxwz

jm2pwxwz1#

正如评论部分所建议的。我首先将代码更改为pdo,并没有在没有任何反勾号或“”的情况下插入数据库,而是按照注解部分的建议,在将图像插入数据库时添加了反勾号和“”。 $conn->exec( "INSERT INTO 车辆详细信息 ( 车辆名称 , 汽车影像 ) VALUES ('$carname', '$imagename')"); 现在我可以将图像插入数据库。我也更新了我的代码。
我的代码:

  1. <!doctype html>
  2. <html>
  3. <head>
  4. <title>Test</title>
  5. </head>
  6. <body>
  7. <form method="post" enctype="multipart/form-data">
  8. <input type="file" name="userfile[]" multiple="" />
  9. <input type="submit" name="submit" value="upload" />
  10. </form>
  11. <?php
  12. $servername="localhost";
  13. $username="root";
  14. $password = "";
  15. $dbname="image_upload";
  16. try {
  17. $conn = new PDO("mysql:host=$servername;dbname=$dbname",$username, $password);
  18. $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  19. // begin the transaction
  20. $conn->beginTransaction();
  21. $phpFileUploadErrors = array(
  22. 0 => "There is no error, the file uploaded with success",
  23. 1 => "The uploaded file exceeds the upload_max_filesize directive in php.ini",
  24. 2 => "The upload file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form",
  25. 3 => "The upload file was only partially uploaded",
  26. 4 => "No file was uploaded",
  27. 6 => "Missing a temporary folder",
  28. 7 => "Failed to write file to disk.",
  29. 8 => "A php extension stopped the file upload"
  30. );
  31. if(isset($_FILES['userfile'])) {
  32. $file_array = reArrayFiles($_FILES['userfile']);
  33. pre_r($file_array);
  34. for($i=0;$i<count($file_array);$i++) {
  35. $carname = $file_array[$i]["size"];
  36. $tablename = "car_detailss";
  37. $imagename = $file_array[$i]["name"];
  38. $conn->exec( "INSERT INTO `car_detailss` (`car_name`, `imageOfcar`) VALUES ('$carname', '$imagename')");
  39. }
  40. }
  41. $conn->commit();
  42. echo "new records created succesfully";
  43. } catch(PDOException $e) {
  44. // roll back the transaction if something failed
  45. $conn->rollback();
  46. echo "Error: " . $e->getMessage();
  47. }
  48. $conn = null;
  49. function reArrayFiles($file_post) {
  50. $file_ary = array();
  51. $file_count = count($file_post['name']);
  52. $file_keys = array_keys($file_post);
  53. for ($i = 0; $i < $file_count; $i++){
  54. foreach($file_keys as $key) {
  55. $file_ary[$i][$key] = $file_post[$key][$i];
  56. }
  57. }
  58. return $file_ary;
  59. }
  60. function pre_r($array) {
  61. echo '<pre>';
  62. print_r($array);
  63. echo '</pre>';
  64. }
  65. ?>
  66. </body>
  67. </html>
展开查看全部

相关问题