【问题标题】:Copying into Intermediary table not working复制到中间表不起作用
【发布时间】:2017-01-25 19:11:10
【问题描述】:

我正在尝试将 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 的表中。

如您所见,每个人的数据都是重复的。

【问题讨论】:

    标签: php mysql insert


    【解决方案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 注入。

    【讨论】:

      猜你喜欢
      • 2017-08-12
      • 2019-10-30
      • 2012-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-04
      相关资源
      最近更新 更多