【问题标题】:error in form submission without image没有图像的表单提交错误
【发布时间】:2012-05-28 18:08:02
【问题描述】:

我有一个 html 表单,我用它来上传图片到服务器

    <form id="form1" name="form1" method="post" action="testimage.php" enctype="multipart/form-data" >   <table width="1000" border="0">
    <tr>
    <td width="108">ID</td>
    <td width="229">
      <input type="text" name="id" id="id" value="<?php if(isset($_GET['editcat'])){echo $_GET['editcat'] ;}; ?>" tabindex="1" />
   </td>
   <tr>
    <td>Image</td>
    <td>:</td>
    <td><input type="file" name="image" id="image" tabindex="7"  /></td>
    <td>&nbsp;</td>
    </tr>
    <tr>
    <td>User</td>
    <td>:</td>
    <td><input type="text" name="user" id="user" value="<?php echo $user ; ?>" tabindex="8" /></td>
    <td>&nbsp;</td>
    </tr>
    <tr>
    <td>&nbsp;</td>
    <td><?php if(isset($_GET['editcat'])){ ?> <input name="editcat" type="hidden" value="set" /> <?php }?> </td>
    <td><input type="submit" name="submit" id="submit" value="Add/Edit" tabindex="9" />                        </td>
    <td>&nbsp;</td>
  </tr>
</table> </form>

我通过一个 php 文件检索此表单数据

       <?php session_start();
       $user_name = $_SESSION['username'];
       $user_pass = $_SESSION['password'];
       if ( $user_name == '' ) {
       header('location:login.php');
       exit();
       }
       ?>
       <?php require_once('connection.php');?>
        <?php require_once('function.php');?>
       <?php 
       $err = 0;
       if(!isset($_SESSION['msgback'])){
       $_SESSION['msgback'] = array();
       }else{
   unset($_SESSION['msgback']);
   $_SESSION['msgback'] = array();
       }
       if(isset($_POST['id'])){
   $id = $_POST['id'];
       }else{
   $id = "";
       }if(isset($_POST['user'])){
   $user = $_POST['user'];
       }else{
   $user="";
        }
       if(!empty($_FILES['image'])){
       $name= basename($_FILES['image']['name']); 
       move_uploaded_file($_FILES["image"]                                  ["tmp_name"],"Hants/admin/uploads/fron_sect/$name")or die(mysql_error());   
        }
         ?>
        <?php
        /////          
        if(isset($_POST['editcat'])){
     if($err == 0){
    if($_POST['id'] == ""){ $_SESSION['msgback'][] = "empty id"; }
        if(empty($_SESSION['msgback'])){
        $user_query = "
        UPDATE  
         pro_category 
        SET  
        `image`='$name',
    `update_user`='$user'
    WHERE  
        `id`='$id'";
        $user_result = mysql_query($user_query) or die(mysql_error());
    $_SESSION['msgback'][] = "Product category is updated sucesfully.";
    header('location:product_cat.php');
    }
   } 
    } 

    ////////Inserting a new Iteme////////
    if(!isset($_POST['editcat'])){ echo "editcat not set" ."<br/>";
   if($err == 0){ 
    if($_POST['id'] == "") {    
         $_SESSION['msgback'][] = "empty id";
        }
        if(empty($_SESSION['msgback'])){
          $user_query = "INSERT                            INTO `pro_category` (`section_id`,`category_name`,`description`,`position`,`visible`,`image`,`pro_view`,`update_user`) VALUES('{$secid}','{$catnam}','{$descrptn}','{$pstion}','{$visble}','{$name}','{$proview}','{$user}')
                                ";
$user_result = mysql_query($user_query) or die(mysql_error());
//if(mysql_affected_rows()){
//$_SESSION['user_name'] = $username;
$_SESSION['msgback'][] = "Successfuly Added";
header('location:product_cat.php');
                    //}
                }else{
                $_SESSION['msgback'][] = "Not Successfuly Added";
                    header('location:product_cat.php');
                //redirect_to("product.php?msgback=msgerr");
                    }
    }
}
?>

当我选择要上传的图像时,此代码运行良好。但在某些情况下,我不需要上传图像,但表单字段的其余部分必须上传到服务器。当我尝试在不选择图像的情况下提交表单时,它会显示一个空白页并且什么也没有发生。 我无法弄清楚错误在哪里。如果有人可以帮助我。

【问题讨论】:

    标签: php forms file-upload


    【解决方案1】:

    更新您的代码以添加此检查以查看文件是否实际上传:

    if ($_FILES['image']['error'] == UPLOAD_ERR_OK) {
    

    【讨论】:

    • 这解决了我的问题。我只是把它放到我的代码中,现在它运行良好。 bt 我不知道这段代码是如何修复我的错误的......无论如何谢谢先生......
    【解决方案2】:

    您的问题可能出在if(!empty($_FILES['image'])){,正如您所说,当您上传图片时它工作得很好,但是当您不上传图片时,您会收到一个空白页。

    当您不上传图片并查看正在发送的内容时,我会print_r($_FILES['image])。我怀疑您的 empty() 仍在拾取一些东西,也许是一个数组。

    也按照 davidethell 的建议做。

    【讨论】:

    • 我这样做了,但它没有显示任何线索.. bt 我添加了 Mr.daviedethell 的代码,然后它工作正常.. 谢谢先生...
    猜你喜欢
    • 2021-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-09
    • 1970-01-01
    • 1970-01-01
    • 2013-02-17
    相关资源
    最近更新 更多