目录更新到数据库表代码

时间:2013-12-07 20:43:03

标签: php mysql database directory

我有代码应该扫描我的数据库表并添加新条目,如果它找不到已经存在的匹配,所以它应该只添加新条目而不是重复任何..

它运行的第一天无论多少次运行都很完美,但是第二天它运行它会添加任何新的东西并复制所有以前的条目,因为日期不再匹配所谓的新条目似乎认为它们都是新条目。

我已经对代码进行了太长时间的讨论,我无法弄清楚我可以添加什么作为阻止这种情况发生的条件。

建议非常感谢!

<?php

// define directory path
$dir = "album";

function getDir($path = '.', $album_id = 0){

    // Directories to ignore when listing output. Many hosts
    // will deny PHP access to the cgi-bin.
    $ignore = array( 'cgi-bin', '.', '..' );

     // Open the directory to the handle $dir_handle
    $dir_handle = @opendir( $path );

    // Loop through the directory
    while( false !== ( $file = readdir( $dir_handle ) ) ){
        // Check that this file is not to be ignored
        if( !in_array( $file, $ignore ) ){
            // Its a directory, so we need to keep reading down...
            if( is_dir( $path."/".$file ) ){
                //Check to see if we have this directory in the Album table
                $a_query = "SELECT * from albums where name = '".$file."' and ";
                $a_result = mysql_query($a_query);
                if (mysql_num_rows($a_result) >= 0){
                    //write this directory as a record
                    $a_insert_query = "INSERT INTO albums (`album_id` ,`name` ,`path` ,`image_id` ,`date`)
                                        VALUES (NULL ,'".$file."','".$path."/".$file."','0',NOW())";
                    $a_insert_result = mysql_query($a_insert_query);
                    //get the new album id
                    $album_id = mysql_insert_id();

                }else{
                    $a_row = mysql_fetch_array($a_result);
                    $album_id = $a_row["album_id"];
                }
                echo $file."<br/>";

                // Re-call this same function but on a new directory.
                // this is what makes function recursive.
                 getDir( $path."/".$file, $album_id );

            } else {//if( is_dir( "$path/$file" ) ){
                //this is a file  -- handle reading the file names
                $file_name_array = explode(".",$file);
                $file_type = strtolower($file_name_array[count($file_name_array)-1]);
                switch($file_type){
                    case "jpg":
                    case "jpeg":
                    case "gif":
                    case "png":
                            echo "-->".$file."<br/>";
                            $img_query = "SELECT `album_id`, `name`, `path`, `image_id` from images where path = '".$path."/".$file."'";
                            $img_result = mysql_query($img_query);
                            if (mysql_num_rows($img_result) >= 0){
                                $p_query = "INSERT INTO `images` (`image_id` ,`name` ,`path` ,`album_id` ,`date`)
                                            VALUES (NULL ,  '".$file."',  '".$path."/".$file."','".$album_id."',NOW())";
                                $p_result = mysql_query($p_query);
                            }
                    break;
                }
            }// else {//if( is_dir( "$path/$file" ) ){
        }//if( !in_array( $file, $ignore ) ){

    }//while( false !== ( $file = readdir( $dir_handle )

}

//this function call starts the process.  
//The function recursively calls itself to step down through the directories.
//the second argument is the album id

getDir($dir, 0);

?>

1 个答案:

答案 0 :(得分:0)

因为您正在检查包含if (mysql_num_rows($a_result) >= 0){的现有记录,所以会评估TRUE记录是否存在。这真的是你想要的吗?如果您只想在记录不存在时写入数据库,请使用:

if (mysql_num_rows($a_result) < 1){

您的第一个$ a_query也有语法错误 - $a_query = "SELECT * from albums where name = '".$file."' and "; - 最后的and不应该在那里。