2012-05-31 55 views
0

我試圖更新數據庫中的記錄。該代碼旨在允許用戶更新網站上的條目,並可選擇編輯圖像。當我最初測試這個代碼時,它沒有任何問題。當他們選擇一張圖片時,它會更新圖片,當他們沒有選擇圖片時,它不會在更新中包含圖片。當我將這段代碼移動到它所需要的頁面上時,它就不再起作用了。它總是讀取它,就好像用戶沒有選擇要上傳的圖像一樣。測試代碼和此代碼之間唯一改變的是數據庫中的名稱,以及爲變量$ title和$ description添加了mysql_real_escape_string()。PHP文件上傳無法正常工作

這裏是不是爲我工作的PHP代碼:

<?php 
require_once ("connect.php"); 
if (isset($_POST['description'])) { 
    $id = $_GET['id']; 
    $title = $_POST['title']; 
    $description = $_POST['description']; 
    $title = mysql_real_escape_string($title); 
    $description = mysql_real_escape_string($description); 
    $target = "../images/contests/"; 
    $target = $target.basename($_FILES['image']['name']); 
    $ok=1; 

    if($_FILES['image']['name'] == "") { 
     $query = "UPDATE tbl_contests SET contests_title='$title', contests_description='$description' WHERE contests_id='$id'"; 
     $result = mysql_query ($query); 
      if ($result) { 
      header ("Location: contests.php?=noimage"); 
      exit(); 
     } else { 
      header ("Location: contests.php?=error"); 
      exit(); 
     } 
    } else { 
     if ($ok==0){ 
      header("Location: contests.php?=error"); 
     } else { 
      if(move_uploaded_file($_FILES['image']['tmp_name'], $target)){ 
       echo "<p>Your upload was sucessful.</p>"; 
       $query = "UPDATE tbl_contests SET contests_title='$title', contests_description='$description', contests_image='$target' WHERE contests_id='$id'"; 
       $result = mysql_query ($query); 
       if ($result) { 
        header ("Location: contests.php?=image"); 
        exit(); 
       } else { 
        header ("Location: contests.php?=error"); 
        exit(); 
       } 
      } 
     } 
    } 
} 
?> 

這裏是形式屬於上面的代碼:

<?php 
    $postnum = $_GET['id']; 
    $query = "SELECT * FROM tbl_contests WHERE contests_id=".$postnum; 
    $result= mysql_query($query); 
    $row = mysql_fetch_array($result); 
    $path = "../images/contests/"; 
    ?> 

    <form action="update-past.php?id=<?php print $row[contests_id]; ?>" method="post" id="updatepast"> 
    <br /><label>Title:</label> <p><input type="text" name="title" id="title" class="input" value="<?php print $row[contests_title]; ?>" /></p> 
    <?php if ($row['contests_image'] == !null) { ?> 
    <p><img src="<?php print $path.$row['contests_image']; ?>" width="425" height="500" /></p> 
    <br /><label>Edit Image: (Optional)</label> <p><input name="image" type="file" id="image" class="file" size="50" /></p> 
    <?php } else { ?> 
    <br /><br /><br /><br /><label>Add Image: (Optional)</label> <p><input name="image" type="file" id="image" class="file" size="50" /></p> 
    <?php } ?> 
    <br /><br /><br /><br /><br /><label>Description:</label><p><textarea name="description" cols="85" id="description" class="contentinput" rows="10"><?php print $row[contests_description]; ?></textarea></p> 
    <p><input type="submit" name="submit" id="button" value="Edit" /></p> 
    </form> 

回答