没有图像的表单提交错误

时间:2012-05-22 09:00:49

标签: php forms file-upload

我有一个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");
                    }
    }
}
?>

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

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

您的问题可能在于if(!empty($_FILES['image'])){,因为您说上传图片时效果非常好,但是在您上传图片时收到空白页。

当您没有上传图片并查看正在发送的内容时,我会print_r($_FILES['image])。我怀疑你的empty()仍在挑选某些数组。

也按照建议做了什么davidethell。