一页上有多个上传按钮

时间:2014-05-16 15:09:58

标签: php uploader

我想在一个页面上有多个上传按钮,但它在第一次上传时一直给我错误"查询为空"。第二个上传器完美运行(s2)。这是我的代码:

<?

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>&nbsp;</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>&nbsp;</td>
      <td> 
        <input type=submit name=s2 value=Save class="btn btn-primary">
      </td>
    </tr>
  </table>

</form>

任何帮助都会很棒

3 个答案:

答案 0 :(得分:0)

您的文件输入字段都具有相同的名称(images),因此s2将始终覆盖s1。尝试将它们更改为唯一,即imagess1imagess2

答案 1 :(得分:0)

您的$ImageName获得空值,因为您有两个<input type="file" name="images">元素。因此,在提交表单时,第一个images覆盖第二个images没有任何元素其中的值,因此更改第二个输入文件名标记的名称。

if(!empty($ImageName))--> this is empty and if block will not be executed
    {
$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());--->so here `$q1` is empty

答案 2 :(得分:0)

I noticed some syntax errors that you may want to address:
in this line:

    <input type=submit name=s1 value=Save class="btn btn-primary"> 

the *value* attribute needs the following text string surrounded by quote marks
the *type* attribute needs the following text string surrounded by quotes
the *name* attribute needs the following text string surrounded by quotes
the corrections of the syntax results in:

    <input type="submit" name="s1" value="Save" class="btn btn-primary"> 

similarly, this line:

    <input type=submit name=s2 value=Save class="btn btn-primary">

should be:

    <input type="submit" name="s2" value="Save" class="btn btn-primary">

The HTML language requires quotes, which kind of quotes (single or double) 
is arbitrary,  so long as they match,
If the lack of quotes works, it is simply a idiosyncrasy of the browser being used.

Regarding the sequence:

       <p><label> 
      <?}?>
      </label></p>

The PHP items are handled in the server, 
before the html is passed to the client (your web browser)
Therefore, the above sequence becomes:

    <p><label>
    <label></p>

which does absolutely nothing useful.
So the sequence should be:

    <?}?>

BTW:
1) The *label* tag is to assist mouse users to click an input by 'expanding' 
the clickable area into the *label* text.
I.E.  wrapping the input area with the *label* tag does work, it accomplishes nothing
Better to write the *label* tag on the text before the input area, 
and link the two via the *for=* and *id=* attributes, like so:

    <td class="form_request" >
        <label for="image2">Second Header Image:</label></td>
    <td class="form_answer">
        <p> 
            <? if($aset['frontimg2']==""){?>
            NA 
            <?}else{?>
           <img id="image2" src="../img/header_images/<?=$aset['frontimg2']?>">
        </p>

As a final note regarding the use of the '<?' shortcut for the '<?php' tag, 
taken from the php manual:

    PHP also allows for short open tags <? and ?> 
    (which are discouraged because they are only available 
    if enabled with short_open_tag php.ini configuration file directive, 
    or if PHP was configured with the --enable-short-tags option. 

Also, IMO: they make the code much harder to read.