2016-03-05 95 views
-1

之前存在我有一個表在數據庫:檢查行更新MySQL表

Time  Name 
11:23  Tom 
12:22  Jack 

而且.txt文件

12:22, Anna 

這是我的代碼

$connection = mysqli_connect($hostname, $username, $password, $dbname) or die("Error " . mysqli_error($connection)); 

// open txt file 
$fp = fopen($filename,"r"); 

//parse the csv file row by row 
while(($row = fgetcsv($fp,"500",",")) != FALSE) 
{ 
    //insert csv data into mysql table 
    $sql = "INSERT INTO project-1 (time, name) VALUES('" . implode("','",$row) . "')"; 
    if(!mysqli_query($connection, $sql)) 
    { 
     die('Error: ' . mysqli_error($connection)); 
    } 
} 

fclose($fp); 

//close the db connection 
mysqli_close($connection); 

如果數據庫中的時間值= .txt中的時間值,我想停止導入.txt文件到數據庫。

+2

聽起來像一個偉大的項目!如果您有任何問題,請告訴我們! – Shadow

+0

因此,向我們展示您正在使用的代碼插入到數據庫中 – RiggsFolly

+0

@RiggsFolly我編輯並顯示我的代碼 – Kalista

回答

0

我不知道我是否正確理解你,但這是我建議你做的。如果你有其他問題,請告訴我我會幫你。

$conn=mysqli_connect('host','username','password','dbname'); 

$file=fopen('a.txt','r'); 
if($file){ 
    while(($line=fgets($file))!==null){ 
     //read every line in txt and but before inserting into db check the time like this: 
     //first parse the time out of the line: 
     $time_array=explode(',',$line); 
     //now you have an array with to indexes. 0=time, 1=name. 
     $query="SELECT * FROM table WHERE time='$time_array[0]'"; 
     $result=mysqli_query($conn,$query); 
     if(mysqli_num_rows($result)==0){ 
      //code for inserting into db; 
     }else{ 
      //stop inserting code and do fclose() or anything else. 
     } 
    } 
} 
+0

感謝您的幫助<3 – Kalista