2017-03-21 37 views
0

嗨,大家好我有一個很好的工作PHP腳本,它讀取服務器上目錄中的圖像文件並將圖像路徑插入到數據庫中。它工作正常,但我需要插入更多的值不僅圖像路徑也是類別和名稱,但我無法弄清楚如何使查詢以正確的方式工作,它只是圖像本身。 我想插入整個路徑,而不是隻有帶擴展名的圖像和其他2個值,如名稱和類別。如何在數據庫行中插入多個值從文件夾目錄中讀取圖像並插入它們

這裏是PHP腳本

<?php 





$server = 'localhost'; 
$dbuser = 'root'; 
$dbpass = 'password'; 
$dbname = 'dbname'; 

$connect = mysql_connect($server,$dbuser,$dbpass); 
mysql_select_db($dbname,$connect); 





$path = "folder/subfolder/"; 
$files = array_map('mysql_real_escape_string',array_map('basename',array_filter(glob("{$path}*.*"),'is_file'))); 
if(empty($files)){ 
    echo "There were no matching files to insert into the database."; 
} else {  
    $query = "INSERT INTO name (picurl) VALUES ('" . implode("'),('",$files) . "')"; 
    if(!mysql_query($query)){ 
     echo "There was a problem inserting the data."; 
     trigger_error("Query failed: $query<br />Error: " . mysql_error()); 
    } else { 
     echo "The data was inserted successfully."; 
    } 
}?> 

我需要的是類似的東西在查詢

$query = "INSERT INTO gbpics (picname, picacat, picurl) VALUES ('" . implode("'),('",$files) . "')"; 

而且具有擴展與整個部分加上圖像和擴展替換圖像。 任何想法?

有些幫助會很大。 感謝

回答

0

想通了這裏是解決方案腳本:

<?php 





$server = 'localhost'; 
$dbuser = 'root'; 
$dbpass = 'password'; 
$dbname = 'dbname'; 

$connect = mysql_connect($server,$dbuser,$dbpass); 
mysql_select_db($dbname,$connect); 





$path = "folder/subfolder/"; 
$files = array_map('mysql_real_escape_string',array_filter(glob("{$path}*.*"),'is_file')); 
if(empty($files)){ 
    echo "There were no matching files to insert into the database."; 
} else {  
    $insertValues = array(); 
foreach($files as $file) 
{ 
    $insertValues[] = "('value1', 'value2', '{$file}')"; 
}$query = "INSERT INTO `name` (`picname`, `piccat`, `picurl`) VALUES " . implode(', ', $insertValues); 
    if(!mysql_query($query)){ 
     echo "There was a problem inserting the data."; 
     trigger_error("Query failed: $query<br />Error: " . mysql_error()); 
    } else { 
     echo "The data was inserted successfully."; 
    } 
}?> 
+0

標記此作爲回答。 –

相關問題