【问题标题】:Multiple upload buttons on one page一页上有多个上传按钮
【发布时间】:2014-05-16 15:09:57
【问题描述】:

我想在一个页面上有多个上传按钮,但它在第一次上传时一直给我错误“查询为空”。第二个上传器完美运行(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>

任何帮助都会很棒

【问题讨论】:

  • 你上传图片了吗?
  • &lt;?}?&gt;&lt;? }?&gt; - &lt;?}else{?&gt;&lt;? }else{?&gt; 之间的间距开始

标签: php uploader


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    您的 $ImageName 正在获得空值,因为您有两个 &lt;input type="file" name="images"&gt; 元素。因此,在提交表单时,第一个 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
    

    【讨论】:

      【解决方案3】:
      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.
      

      【讨论】:

        猜你喜欢
        • 2012-10-15
        • 2020-09-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-10-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多