2013-12-07 17 views
1

我有代碼應該掃描我的數據庫表,並添加新的條目,如果它沒有找到匹配的東西已經存在,所以它應該只是添加新的條目,而不是重複任何..目錄更新到數據庫表代碼

無論多少次它運行的第一天,但​​它運行的第二天,它添加了任何新的和重複所有以前的條目作爲日期不再匹配假設的新條目似乎是完美的認爲他們都是新的條目。

我已經在代碼上出現太久了,我無法弄清楚我可以添加哪些條件來防止這種情況發生。

建議將不勝感激!

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

?> 

回答

0

因爲你正在檢查與if (mysql_num_rows($a_result) >= 0){現有記錄,那將評估爲TRUE的記錄是否存在與否。那真的是你想要的嗎?如果你只是想寫信給DB時,記錄不存在,那麼使用:

if (mysql_num_rows($a_result) < 1){ 

你也有你的第一個$ a_query一個語法錯誤 - $a_query = "SELECT * from albums where name = '".$file."' and "; - 在and末不應該那裏。

+0

我在提交這段代碼後就發現了語法錯誤,這是我試圖弄清楚如何讓它起作用的一部分,我嘗試了將0更改爲1的建議,當我更改日期到我的相冊的前一個日期..舊的仍然被添加,就好像它認爲它的日期的一個新的原因.. 它不會在同一天添加重複的條目,它必須是以前的日期郵票再次進入相同的專輯.. 不知道我能做些什麼來防止這種情況發生,但是如果我無法解決這個問題,它會使客戶端與專輯鏈接變得困難 – user2970595

+0

您是否還將'> ='更改爲'<'? –

+0

噢,天啊,我沒有..我想這可能已經做到了! 我仍然是這個東西的嚴重新手,所以排除故障對我來說真的很難,但我真的很感謝這個幫助! – user2970595