dongyun65343 2017-01-25 19:11
浏览 13
已采纳

复制到中介表不起作用

I am trying to copy ID's into an intermediary table. For some reason it is inputting the data for each person twice.

                function addPeople($tags){   
            include('connect.php');
            if($con==false){
            }else if ($con==true){
                foreach($tags as $key){     
                    $checkPerson="SELECT Name from person WHERE Name='".$key."';";
                    $insertPerson="INSERT INTO person (Name) VALUES ('$key')";
                    $resultCheckPerson=mysqli_query($con,$checkPerson) or die(mysqli_error($con));
                    if(mysqli_num_rows($resultCheckPerson)>0){
                        //Do not add to database
                    }elseif($con->query($insertPerson)=== TRUE){

                    }           
                }
            }
        }
        function add($image,$pathy,$array,$optional=null){
            include('connect.php');
            if($con==false){
            }else if ($con==true){
                $insertImage="INSERT INTO image (path) VALUES ('$pathy')";
                $checkImage="SELECT path from image WHERE path='".$pathy."';";  
                $resultCheckImage=mysqli_query($con,$checkImage) or die(mysqli_error($con));
                if(mysqli_num_rows($resultCheckImage)>0){

                }else if($con->query($insertImage) === TRUE){

                }
                foreach($array as $key){
                    $checkPerson="SELECT Name from person WHERE Name='".$key."';";
                    $insertPerson="INSERT INTO person (Name) VALUES ('$key')";
                    $resultCheckPerson=mysqli_query($con,$checkPerson) or die(mysqli_error($con));
                    if(mysqli_num_rows($resultCheckPerson)>0){
                        //Do not add to database
                    }else if($con->query($insertPerson) === TRUE){

                    }
                }
            }

        }
        function getImageID($path){
            include('connect.php');
            if($con==false){
            }else if ($con==true){
                $getID="SELECT image_ID from image WHERE path ='".$path."';";
                $resultGetID=mysqli_query($con,$getID)or die(mysqli_error($con));
                $idArray=mysqli_fetch_array($resultGetID);
                return $idArray[0];
            }
        }
        function getPersonID($name){
            include('connect.php');
            if($con==false){
            }else if ($con==true){
                $getName="SELECT person_ID FROM person WHERE Name ='".$name."';";
                $resultGetName=mysqli_query($con,$getName)or die(mysqli_error($con));
                $arrayName=mysqli_fetch_array($resultGetName);
                return $arrayName[0];
            }
        }
        function addMiddle($imageID,$personID){

            include('connect.php');
            if($con==false){
            }else if ($con==true){
                $addData="INSERT INTO image_person (image_ID,person_ID) VALUES ('$imageID','$personID')";
                $resultAddData=mysqli_query($con,$addData) or die(mysqli_error($con));
                if($con->query($addData) === TRUE){

                }   
            }
        }
        function huntExtract($x){

            foreach($x as $imageFile){
                echo "image".$imageFile."  start<br>";
                $withoutExt = preg_replace('/\\.[^.\\s]{3,4}$/', '', $imageFile);
                $path="img/".$withoutExt.".jpg";
                $tagsArray=getTags($imageFile);
                add($imageFile,$path,$tagsArray);
                $imageID=getImageID($path); 
                $image_ID=$imageID[0];

                foreach ($tagsArray as $key){
                    echo" person ".$key."<br>";
                    $person_ID=getPersonID($key);
                    echo "Their ID is ".$person_ID."<br>";
                    addMiddle($image_ID,$person_ID);
                }
                echo "image".$imageFile." end<br>";
            }

        }

        //phase 1: get all files in dir into a list or array
        $dir='img';
        $folder=scandir($dir);
        //sanatize the array, we don't want junk in our trunk
        if(($key = array_search('.', $folder)) !== false) {
            unset($folder[$key]);
            unset ($key);
        }
        if(($key = array_search('..', $folder)) !== false) {
            unset($folder[$key]);
            unset ($key);
        }
        if(($key = array_search('Thumbs.db', $folder)) !== false) {
            unset($folder[$key]);
        }


        huntExtract($folder);






        ?>

The addMiddle() function is supposed to go get the picture ID, then in a loop, get each personID and copy that along with the image ID into a table called image_person.

enter image description here

As you can see the data is repeated for each person.

  • 写回答

1条回答 默认 最新

  • dongzz4545 2017-01-25 20:02
    关注

    It's because you are running the same query twice...

    function addMiddle($imageID,$personID){
    
        include('connect.php');
        if($con==false){
        }else if ($con==true){
    
            // Create SQL statement 
            $addData="INSERT INTO image_person (image_ID,person_ID) VALUES ('$imageID','$personID')";
    
            // First time running the SQL statement $addData
            $resultAddData=mysqli_query($con,$addData) or die(mysqli_error($con));
    
            // Second time running the SQL statement $addData
            if($con->query($addData) === TRUE){
    
            }   
        }
    }
    

    BTW, I don't know where your input is coming from, but you should really look into using PDO instead in order to write cleaner code and to help prevent SQL injection.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度