首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >数据库表代码的目录更新

问数据库表代码的目录更新
EN

Stack Overflow用户
提问于 2013-12-07 20:43:03
回答 1查看 78关注 0票数 1

我的代码应该扫描我的数据库表,如果没有找到匹配项,它应该添加新条目,所以它应该只是添加新条目,而不是复制任何条目。

无论运行多少次,它在运行的第一天就工作得很好,但是第二天它会添加任何新的内容,并复制以前的所有条目,因为日期不再匹配它认为它们都是新条目的假定新条目。

我对代码的研究时间太长了,我想不出我能添加什么作为防止这种情况发生的条件。

建议将是最感谢的!

代码语言:javascript
复制
<?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);

?>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-12-07 20:52:36

因为您正在使用if (mysql_num_rows($a_result) >= 0){检查现有记录,这将对TRUE评估该记录是否存在。这就是你想要的吗?如果只想在记录不存在时写入DB,请使用:

代码语言:javascript
复制
if (mysql_num_rows($a_result) < 1){

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20446386

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档