复制到中介表不起作用

时间:2017-01-25 19:11:10

标签: php mysql insert

我正在尝试将ID复制到中间表中。由于某种原因,它正在为每个人输入两次数据。

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






        ?>

addMiddle()函数应该获取图片ID,然后在循环中获取每个personID并将其与图像ID一起复制到名为image_person的表中。

enter image description here

正如您所看到的,每个人都会重复这些数据。

1 个答案:

答案 0 :(得分:1)

这是因为您运行了两次相同的查询......

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

        }   
    }
}
顺便说一句,我不知道你的输入来自哪里,但你应该真正考虑使用PDO来编写更清晰的代码并帮助防止SQL注入。

相关问题