2012-05-15 78 views
-1

下面我有它增加了一個數據庫行PHP腳本,如果一個新的或現有的文件上傳放置此:在哪裏,如果statment在PHP腳本

<?php 
session_start(); 
...//connect to DB 

$result = 0; 
if(file_exists("ImageFiles/".$_FILES['fileImage']['name'])) { 
    $imagesql = "INSERT INTO Image (ImageFile) 
    VALUES ('ImageFiles/".mysql_real_escape_string($_FILES['fileImage']['name'])."')"; 

    mysql_query($imagesql); 
} else { 
    $imagesql = "INSERT INTO Image (ImageFile) 
    VALUES ('ImageFiles/".mysql_real_escape_string($_FILES['fileImage']['name'])."')"; 
    mysql_query($imagesql); 
} 
mysql_close(); 
?> 

<script language="javascript" type="text/javascript">window.top.stopImageUpload(<?php echo $result ? 'true' : 'false'; ?>, '<?php echo $_FILES['fileImage']['name'] ?>');</script> 

但問題是,我想補充當$ result = 2時,else語句下面的if語句會刪除數據庫行。當我嘗試這樣做時,它不會在上傳新文件或現有文件後插入數據庫行。

if ($result = 2){ 
    $imagecancelsql = "DELETE FROM Image 
    WHERE ImageFile = 'ImageFiles/". mysql_real_escape_string($_FILES['fileImage']['name'])."'"; 
    mysql_query($imagecancelsql); 
} 

所以我的問題是:我該如何將if語句放在php腳本中,以便它執行必要的數據庫更新?

+0

它應該是'如果($結果== 2)' – Jon

+1

我不看的時候$結果是以往任何時候都爲2 – DaOgre

+1

^,我沒有看到如何意外設置$ result = 2會影響其他if/else語句 – Jon

回答

1

你不能在任何地方定義你的$ result var,所以if永遠不會被執行。反正你應該有把它:

<?php 
session_start(); 
...//connect to DB 

$result = 0; 
if(file_exists("ImageFiles/".$_FILES['fileImage']['name'])) { 
    $imagesql = "INSERT INTO Image (ImageFile) 
    VALUES ('ImageFiles/".mysql_real_escape_string($_FILES['fileImage']['name'])."')"; 

    mysql_query($imagesql); 
} else { 
    $imagesql = "INSERT INTO Image (ImageFile) 
    VALUES ('ImageFiles/".mysql_real_escape_string($_FILES['fileImage']['name'])."')"; 
    mysql_query($imagesql); 
} 

if ($result == 2){ 
    $imagecancelsql = "DELETE FROM Image 
    WHERE ImageFile = 'ImageFiles/". mysql_real_escape_string($_FILES['fileImage']['name'])."'"; 
    mysql_query($imagecancelsql); 
} 

mysql_close(); 
?>