I wanted to have multiple upload buttons on one page but it keeps giving me error on the first upload "query was empty". The second uploader works perfectly (s2). Here is my code:
<?
if(isset($_POST['s1']))
{
$qset = "select frontimg1 from cat";
$rset = mysql_query($qset) or die(mysql_error());
$aset = mysql_fetch_array($rset);
unset($aset);
$ImageName = $_FILES['images']['name'];
if(!empty($ImageName))
{
$t = time();
$NewImageName = "$t$ImageName";
copy($_FILES['images']['tmp_name'], "../img/header_images/$NewImageName");
$q1 = "update cat set
frontimg1='$NewImageName'";
}
mysql_query($q1) or die(mysql_error());
echo "<div class=alert fade in><b>Website settings was updated successfully!</b> </div>";
}
elseif(isset($_POST['s2']))
{
$qset = "select frontimg2 from cat";
$rset = mysql_query($qset) or die(mysql_error());
$aset = mysql_fetch_array($rset);
unset($aset);
$ImageName = $_FILES['images']['name'];
if(!empty($ImageName))
{
$t = time();
$NewImageName = "$t$ImageName";
copy($_FILES['images']['tmp_name'], "../img/header_images/$NewImageName");
$q1 = "update cat set
frontimg2='$NewImageName'";
}
mysql_query($q1) or die(mysql_error());
echo "<div class=alert fade in><b>Website settings was updated successfully!</b> </div>";
}
//get the main site settings
$qset = "select * from cat";
$rset = mysql_query($qset) or die(mysql_error());
$aset = mysql_fetch_array($rset);
?>
<form method=post name=f1 onsubmit="return CheckSettings();" enctype="multipart/form-data">
<table align=center width="90%" cellpadding="4" cellspacing="1">
<tr>
<td colspan=2 class=header><font size="2">
<b>WEBSITE SETTINGS</b></font></td>
</tr>
<tr>
<td class="form_request">Current Header Image:</td>
<td class="form_answer">
<p><label>
<? if($aset['frontimg1']==""){?>
NA
<?}else{?>
<img src="../img/header_images/<?=$aset['frontimg1']?>"> </label></p>
<p><label>
<?}?>
</label></p>
</td>
</tr>
<tr>
<td class="form_request">Upload New Header Image:</td>
<td class="form_answer"><label>
<input type="file" name="images">
<br />
(Leave it blank to keep the same image)</label></td>
</tr>
<tr>
<td> </td>
<td>
<input type=submit name=s1 value=Save class="btn btn-primary">
</td>
</tr>
<tr>
<td class="form_request">Second Header Image:</td>
<td class="form_answer">
<p><label>
<? if($aset['frontimg2']==""){?>
NA
<?}else{?>
<img src="../img/header_images/<?=$aset['frontimg2']?>"> </label></p>
<p><label>
<?}?>
</label></p>
</td>
</tr>
<tr>
<td class="form_request">Upload New Header Image:</td>
<td class="form_answer"><label>
<input type="file" name="images">
<br />
(Leave it blank to keep the same image)</label></td>
</tr>
<tr>
<td> </td>
<td>
<input type=submit name=s2 value=Save class="btn btn-primary">
</td>
</tr>
</table>
</form>
Any help would be great
Your file input fields both have the same name (images
) so s2 will always override s1. Try changing them to be unique ie imagess1
and imagess2
.