上传多个图像时发生错误,表示数据无法更新

nhn9ugyo  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(636)

我试图使多个图像上传与数据连接,所以图像名称应保存在数据库中,现在当我上传图像,它是上传,也显示在文件夹中,但没有在我的数据库中上传,并说对不起,数据无法更新!
请帮帮我,我已经试了一天了。这是我的上传代码

  1. <?php
  2. require_once 'dbconfig.php';
  3. require_once 'common_functions.php';
  4. if (isset($_POST["sub"])) {
  5. $msg = "";
  6. if (count($_FILES["user_files"]) > 0) {
  7. $folderName = "uploads/";
  8. $counter = 0;
  9. for ($i = 0; $i < count($_FILES["user_files"]["name"]); $i++) {
  10. $file_name=$_FILES["user_files"]["name"][$i];
  11. if ($_FILES["user_files"]["name"][$i] <> "") {
  12. $ext = strtolower(pathinfo($file_name,PATHINFO_EXTENSION)); // get image extension
  13. $file_name = $folderName . rand(10000, 990000) . '_' . time() . '.' . $ext;
  14. $valid_extensions = array('jpeg', 'jpg', 'png'); // valid extensions
  15. if(in_array($ext, $valid_extensions)){
  16. $filename=basename($file_name,$ext);
  17. $newFileName=$filename.$ext;
  18. if (!move_uploaded_file($_FILES["user_files"]["tmp_name"][$i],"Uploads/".$newFileName)) {
  19. $msg .= "Failed to upload" . $_FILES["user_files"]["name"][$i] . ". <br>";
  20. $counter++;
  21. }
  22. if(!isset($msg)) {
  23. $stmt = $DB_con->prepare('INSERT INTO products SET productname=:productname');
  24. $stmt->bindParam(':productname',$newFileName);
  25. if($stmt->execute()){
  26. $msg = "Your Post ' ".$newFileName." ' has been successfully uploaded , <a target='_blank' href='post.php?name=".$newFileName." '>View</a>.";
  27. // redirects video view page after 5 seconds.
  28. } else {
  29. $msg = "error while inserting....";
  30. }
  31. } else {
  32. $msg = "Sorry Data Could Not Updated !";
  33. }
  34. } else {
  35. $msg = "Sorry, only JPG, JPEG, PNG files are allowed.";
  36. }
  37. }
  38. }
  39. }
  40. } else {
  41. $msg = errorMessage("You must upload atleast one file");
  42. }
  43. ?>
wqsoz72f

wqsoz72f1#

“insert”语句中没有“set”。尝试。。。

  1. 'INSERT INTO products (productname) VALUES (:productname)'

将来,您可以通过触发mysqli错误并显示以下内容来节省一天左右的时间:

  1. $stmt->execute() or trigger_error($stmt->error, E_USER_ERROR);

相关问题