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条)

报告相同问题?

悬赏问题

  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作