dqndc26628 2014-05-19 16:29
浏览 1287
已采纳

带有SQL语句的Foreach循环

I'm creating a system that allows you to add a message with multiple images uploaded. The uploading of all the images goes well, however, when I try to insert all the image locations into a table that has a foreign key from the table 'messages' (so that every image knows to which message it belongs), I get the following error:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`scrapll`.`scrapimage`, CONSTRAINT `scrapimage_ibfk_1` FOREIGN KEY (`scrap_id`) REFERENCES `scraps` (`scrap_id`))' in C:\xampp\htdocs\scrapll_m_nonstatic\process
ew\scrap_process.php:132 Stack trace: #0 C:\xampp\htdocs\scrapll_m_nonstatic\process
ew\scrap_process.php(132): PDOStatement->execute(Array) #1 {main} thrown in C:\xampp\htdocs\scrapll_m_nonstatic\process
ew\scrap_process.php on line 132

This only happens when I try to upload multiple images. When I only upload one image, no issues at all.

Here's the code:

foreach($_FILES['scrapPhotos']['tmp_name'] as $key => $tmp_name ){
                $fileName = $_FILES['scrapPhotos']['name'][$key];
                $fileSize = $_FILES['scrapPhotos']['size'][$key];
                $fileTmp = $_FILES['scrapPhotos']['tmp_name'][$key];
                $fileType= $_FILES['scrapPhotos']['type'][$key];

                if($_FILES['scrapPhotos']['name'][$key]){
                    // Get file extension of uploaded file
                    $imgFileExtension = strrchr($fileName, ".");

                    // Check if file extension of uploaded file is valid
                    if(!in_array($imgFileExtension, $validFileExtensions)) {
                        echo $scrapErrors[0];
                    }
                    // Check if file size is valid (!> 10000000)
                    elseif($fileSize > MAXFILESIZE) {
                        echo $scrapErrors[1];
                    }
                    else {                  
                        // The path to Scrap image
                        $imgLoc = "../../../scrapll_m/static/img/user/scrap/orig/";

                        // Move files to appropiate location
                        $imgFile = sha1(uniqid($_FILES['scrapPhotos']['name'][$key]));
                        $imgFilePath = $imgLoc . $imgFile . $imgFileExtension;

                        // Store image(s) on the server
                        move_uploaded_file($_FILES['scrapPhotos']['tmp_name'][$key], $imgFilePath);

                        $insertImgQuery = 'INSERT INTO scrapimage (scrap_id, image_original_size, image_scrap_size) 
                                                    VALUES (LAST_INSERT_ID(), ?, ?)';
                        $prepInsertImg = $conn->prepare($insertImgQuery);
                        $prepInsertImg->execute(array($imgFilePath, $imgFilePath));
                    }
                }
            }

So, why can't I make the SQL query at the bottom of my code to be executed multiple times and add one row with the next image locations to the database? I've put it in a foreach to do that, but it doesn't seem to work.

  • 写回答

3条回答 默认 最新

  • dongshu7162 2014-05-19 16:40
    关注

    Yeah, like lealhugui said, you are inserting LAST_INSERT_ID() as the value for scrap_id in scrapimage. Your constraint is requiring that there be a row in scraps with that same id.

    Somewhere, probably above this loop, you should have figured out what the scrap_id is for this row in scraps. Then use that same id for every scrapimage.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥30 求解达问题(有红包)