2012-05-31 3 views
0

데이터베이스의 레코드를 업데이트하려고합니다. 이 코드는 사용자가 이미지 편집 옵션을 사용하여 웹 사이트의 항목을 업데이트 할 수 있도록하기위한 것입니다. 처음에이 코드를 테스트했을 때 문제없이 작동했습니다. 그들이 이미지를 선택하면 이미지가 업데이트되고 이미지를 선택하지 않으면 이미지가 업데이트에 포함되지 않습니다. 이 코드를 페이지에 옮기면 더 이상 작동하지 않습니다. 사용자가 업로드 할 이미지를 선택하지 않은 것처럼 항상 읽습니다. 테스트 코드와이 코드 사이에서 변경된 유일한 것은 데이터베이스의 이름과 변수 $ title 및 $ description에 대한 mysql_real_escape_string()의 추가입니다.PHP 파일 업로드가 제대로 작동하지 않습니다.

<?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> 

답변

1

시도 폼이 추가 : enctype="multipart/form-data"

다음
<?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 코드

다음은 양식 콘텐츠 유형에 대한 내용입니다. http://www.w3.org/TR/html4/interact/forms.html#h-17.13.4.2

+0

tch, 고마워! – akari

관련 문제