I have a php script below where it adds a database row if a new or existing file is uploaded:
<?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>
But the problem is that I want to add an if statement below the else statement when $result = 2 so that it deletes a database row. When I try this, then it doesn’t insert a database row after uploading a new or existing file.
if ($result = 2){
$imagecancelsql = "DELETE FROM Image
WHERE ImageFile = 'ImageFiles/". mysql_real_escape_string($_FILES['fileImage']['name'])."'";
mysql_query($imagecancelsql);
}
So my question is: how am I supposed to place the if statement above in the php script so that it performs its necessary database update?
You don’t define anywhere your $result var, so the if would never get executed. Anyway you should place it there: