2012-07-21 100 views
0

嗨,大家好我正在一個項目,我有一個student_edit.php文件,更新學生 詳細信息我所有的數據更新成功,但有一個問題,當我更新只能說兩個領域和不是所有的圖像是空白的,我的領域更新成功。無法更新圖像在PHP和MYSQL

我在做什麼是我顯示學生圖片,除此之外,我有另一個輸入字段來瀏覽圖像更新圖像。我想要的很簡單,如果我不更新圖像,而是我更新其他字段圖像應該仍然存在,而不是變成空白。

必需的代碼段是這樣的:

<?php 

    $file_name = $_FILES['picture']['name']; 
    $tmp_name = $_FILES['picture']['tmp_name']; 

    if (copy($tmp_name, "images/" . $file_name)) { 
     $picture = "images/" . $file_name; 
    } 

    if (!isset($_POST['picture'])) { 
     $res = mysql_query("UPDATE student SET `id`='$id',`branch_id`='$branch_id',`class_id`='$class_id',`section_id`='$section_id',`roll_number`='$roll_number',`student_name`='$student_name',`father_name`='$father_name',`dob`='$dob',`student_address`='$student_address',`gender`='$gender',`status`='$status',updated=now() WHERE id='$id'") or die(mysql_error()); 
    } 
    else { 
     $res = mysql_query("UPDATE student SET `id`='$id',`branch_id`='$branch_id',`class_id`='$class_id',`section_id`='$section_id',`roll_number`='$roll_number',`student_name`='$student_name',`father_name`='$father_name',`dob`='$dob',`student_address`='$student_address',`gender`='$gender',`status`='$status',`picture`='$picture',updated=now() WHERE id='$id'") or die(mysql_error()); 
    } 

?> 

他們是畫面區域在哪裏調用圖片

<p> 
    <label for="picture"><strong>Picture:</strong> </label> 
    <a href="#"><img src="<?php echo $rec['picture'];?>" width="100" height="100"/></a> 
    <input name="picture" type="file" value=""> 

</p> 

他們是畫面區域在哪裏調用圖片

<p> 
    <label for="picture"><strong>Picture:</strong> </label> 
    <a href="#"><img src="<?php echo $rec['picture'];?>" width="100" height="100"/></a> 
    <input name="picture" type="file" value=""> 

</p> 
+2

請不要將'mysql_ *'函數用於新代碼。他們不再被維護,社區已經開始[棄用流程](http://goo.gl/KJveJ)。請參閱[**紅框**](http://goo.gl/GPmFd)?相反,您應該瞭解[準備好的語句](http://goo.gl/vn8zQ)並使用[PDO](http://php.net/pdo)或[MySQLi](http://php.net/ mysqli的)。如果你不能決定,[本文](http://goo.gl/3gqF9)將有助於選擇。如果你關心學習,[這裏是很好的PDO教程](http://goo.gl/vFWnC)。 – 2012-07-21 20:31:24

+0

感謝您的推薦! – user1313942 2012-07-21 20:37:53

回答

0

通過查看您的代碼只是一個快速的猜測,您不會逃脫的價值當您將其放置在數據庫中時,這可能是數據庫中圖像值未更新的原因。

此外,請勿使用if(!isset($_POST['picture'])),但請嘗試if (!isset($_FILES['picture']) || (isset($_FILES['picture']) && $_FILES['picture']['name'] == "")。超級全球$_POST將無法​​正常工作,因爲文件通過超級全球$_FILES發送。

全部放在一起:

$file_name = $_FILES['picture']['name']; 
$tmp_name = $_FILES['picture']['tmp_name']; 

if (!isset($_FILES['picture']) || (isset($_FILES['picture']) && $_FILES['picture']['name'] == "")) { 
    $res = mysql_query("UPDATE student SET `id`='$id',`branch_id`='$branch_id',`class_id`='$class_id',`section_id`='$section_id',`roll_number`='$roll_number',`student_name`='$student_name',`father_name`='$father_name',`dob`='$dob',`student_address`='$student_address',`gender`='$gender',`status`='$status',`picture`='$picture',updated=now() WHERE id='$id'") or die(mysql_error()); 
} else { 
    copy($tmp_name, "images/" . $file_name) 

    $picture = mysql_real_escape_string("images/".$file_name); 
    $res = mysql_query("UPDATE student SET `id`='$id',`branch_id`='$branch_id',`class_id`='$class_id',`section_id`='$section_id',`roll_number`='$roll_number',`student_name`='$student_name',`father_name`='$father_name',`dob`='$dob',`student_address`='$student_address',`gender`='$gender',`status`='$status',updated=now() WHERE id='$id'") or die(mysql_error()); 
} 

另外,我真的建議你可用MySQLi:http://php.net/manual/en/book.mysqli.php

上面的代碼是完全未經測試,但應該工作。

+0

感謝您的寶貴意見。我試過我們的代碼,但沒有運氣。你能告訴我如何讓這種情況發生,如果我不更新圖像領域,但更新其他領域,那麼圖像不會變成空白。 – user1313942 2012-07-21 21:04:12

+0

@ user1313942只要你已經定義了SQL查詢中的每個變量(並且同樣重要),那麼'if'語句中的查詢(不在'else中)應該可以做到這一點 – 2012-07-21 21:42:20

+0

我試過了我知道的事情,但我不能得到它正確的任何人rply表示讚賞。 – user1313942 2012-07-22 08:49:24