Q:

Copying into Intermediary table not working

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. 

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

A:

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.

