我有清單中的汽車列表,每個列表最多可以有12張照片。如果我願意,我可以成功擦除汽車,而我遇到的問題是擦除圖片。PHP清除for循環中的文件
所以我可以查詢是這樣的:
$pictures = "picture1, picture2, picture3, picture4, picture5, picture6, picture7, picture8, picture9, picture10, picture11, picture12";
$data = mysql_fetch_assoc(mysql_query("SELECT $pictures FROM `auto` WHERE `auto_id` = $autoid"));
隨着$data
變量,我可以像這樣echo $data['picture1'];
我試圖讓通過循環來訪問圖片的名稱每張圖片,只要db中的字段不爲空,就從數據創建這些文件路徑的數組,並循環遍歷數組,使用unlink()
擦除它們?
迄今方案的工作,但需要改進?]
if (isset($_POST['delete']) === true)
{
$pictures = "picture1, picture2, picture3, picture4, picture5, picture6, picture7, picture8, picture9,
picture10, picture11, picture12";
$data = mysql_fetch_assoc(mysql_query("SELECT $pictures FROM `auto` WHERE `auto_id` = $autoid"));
$a = 1;
while ($a <= 12)
{
$picturepath = $data['picture'.$a];
if (empty($picturepath) !== true)
{
unlink('../../' . $picturepath);
}
$a++;
}
mysql_query("DELETE FROM `auto` WHERE `auto_id` = $autoid");
header('Location: ../../admin.php?manage_vehicles');
exit();
}
我把它的字段(列)picture1,pcture2等包含圖片文件的路徑名? –
我的解決方案是否包含任何不良的php? –