2012-05-22 55 views
0

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

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

回答

1

更新你的代码中加入这个检查,看是否有文件实际上上传:

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

这解决了我的问题。我只是把它写到我的代码中,现在已经很好了。 bt我不知道这个代码如何修复我的错误.... 任何方式谢谢你先生..... – Yasitha

0

你的问题可能是if(!empty($_FILES['image'])){就像你说的,当你上传图片,它工作完全正常,但您会收到一当你没有上传图片时,你的空白页面。

我会print_r($_FILES['image])当你没有上传图片,看看有什么发送。我怀疑你的empty()仍然在挑选一些可能​​是数组的东西。

也按照建议做什么davidethell。

+0

我做了这个BT它没有显示我任何线索.. BT我添加Mr.Daviedethell的代码然后它工作正常.. 谢谢你先生... – Yasitha