mysql查询

xwbd5t1u  于 2021-06-19  发布在  Mysql
关注(0)|答案(1)|浏览(185)

我面临着php插入多个图像和数据库中唯一post id的问题。在数据库中我有图像表和post表,当我上传多个图像时,图像将以当前post id存储在图像表中。
问题是insert语法错误,因为
$insertvaluessql插入值
是从为每个使用 .= 用于上载图像的串联分配。
问题是我要添加额外的post id,这样我就可以很容易地从哪个post id得到图像,所以我必须插入两个 $insertValuesSQL 以及 $last_id 从post id。
有人能帮我纠正语法,并能够上传与post-id的图像吗?谢谢你。
错误位置:

  1. $insert = $conn->query("INSERT INTO test (file_name, post_id) VALUES $insertValuesSQL,$last_id");

php完整代码:

  1. $targetDir = "uploads/";
  2. $allowTypes = array('jpg','png','jpeg','gif');
  3. $statusMsg = $errorMsg = $insertValuesSQL = $errorUpload = $errorUploadType = '';
  4. if(!empty(array_filter($_FILES['submit-image']['name']))){
  5. foreach($_FILES['submit-image']['name'] as $key=>$val){
  6. // File upload path
  7. $fileName = basename($_FILES['submit-image']['name'][$key]);
  8. $targetFilePath = $targetDir . $fileName;
  9. // Check whether file type is valid
  10. $fileType = pathinfo($targetFilePath,PATHINFO_EXTENSION);
  11. if(in_array($fileType, $allowTypes)){
  12. // Upload file to server
  13. if(move_uploaded_file($_FILES["submit-image"]["tmp_name"][$key], $targetFilePath)){
  14. // Image db insert sql
  15. $insertValuesSQL .= "('".$fileName."'),";
  16. }else{
  17. $errorUpload .= $_FILES['submit-image']['name'][$key].', ';
  18. }
  19. }else{
  20. $errorUploadType .= $_FILES['submit-image']['name'][$key].', ';
  21. }
  22. }
  23. if (mysqli_query($conn, $sql)) {
  24. $last_id = mysqli_insert_id($conn);
  25. echo "New record created successfully. Last inserted ID is: " . $last_id;
  26. if(!empty($insertValuesSQL)){
  27. $insertValuesSQL = trim($insertValuesSQL,',');
  28. // Insert image file name into database
  29. $insert = $conn->query("INSERT INTO test (file_name, post_id) VALUES $insertValuesSQL,$last_id");
  30. if($insert){
  31. $errorUpload = !empty($errorUpload)?'Upload Error: '.$errorUpload:'';
  32. $errorUploadType = !empty($errorUploadType)?'File Type Error: '.$errorUploadType:'';
  33. $errorMsg = !empty($errorUpload)?'<br/>'.$errorUpload.'<br/>'.$errorUploadType:'<br/>'.$errorUploadType;
  34. $statusMsg = "Files are uploaded successfully.".$errorMsg;
  35. }else{
  36. $statusMsg = "Sorry, there was an error uploading your file.";
  37. }
  38. }
  39. } else {
  40. echo "Error: " . $sql . "<br>" . mysqli_error($conn);
  41. }
  42. }else{
  43. $statusMsg = 'Please select a file to upload.';
  44. }

再加一个
如果我使用以下代码:

  1. $insert = $conn->query("INSERT INTO test (file_name) VALUES $insertValuesSQL");

上载多个图像成功,但没有图像的post id
参考来源:https://www.codexworld.com/upload-multiple-images-store-in-database-php-mysql/

cmssoen2

cmssoen21#

你需要把 $last_id 插入到每个值列表中,而不是作为单独的参数。但你不能这么做因为你在创造 $insertValuesSQL 在你出发之前 $last_id .
可以移动创建 $insertValuesSQL 在你准备好之后 $last_id ,但另一种方法是使用mysql的内置函数 LAST_INSERT_ID() :

  1. $insertValuesSQL .= "('".$fileName."', LAST_INSERT_ID()),";

然后你就可以 $last_id 从后面出来 INSERT 查询:

  1. $insert = $conn->query("INSERT INTO test (file_name, post_id) VALUES $insertValuesSQL");

相关问题