2013-09-25 70 views
-1

我對PHP編程還很陌生,我環顧四周,但我仍然感到困惑。我試圖更新我的用戶表中的圖像路徑,我不太確定如何去做。這是我將圖像放入數據庫的代碼,它可以插入圖像,但我不確定如何更新數據庫中的圖像圖片路徑以使用新插入的圖像,而不是使用者當他們創建一個帳戶時選擇。更新圖像路徑

// Make sure we didn't have an error uploading the image 
($_FILES[$image_fieldname]['error'] == 0) 
or handle_error("the server couldn't upload the image you selected.", 
$php_errors[$_FILES[$image_fieldname]['error']]); 

// Is this file the result of a valid upload? 
@is_uploaded_file($_FILES[$image_fieldname]['tmp_name']) 
or handle_error("you were trying to do something naughty. Shame on you!", 
"upload request: file named " . 
"'{$_FILES[$image_fieldname]['tmp_name']}'"); 

// Is this actually an image? 
@getimagesize($_FILES[$image_fieldname]['tmp_name']) 
or handle_error("you selected a file for your picture " . 
"that isn't an image.", 
"{$_FILES[$image_fieldname]['tmp_name']} " . 
"isn't a valid image file."); 

// Name the file uniquely 
$now = time(); 
while (file_exists($upload_filename = $upload_dir . $now . 
'-' . 
$_FILES[$image_fieldname]['name'])) { 
$now++; 
} 

// Finally, move the file to its permanent location 
@move_uploaded_file($_FILES[$image_fieldname]['tmp_name'], $upload_filename) 
or handle_error("we had a problem saving your image to " . 
"its permanent location.", 
"permissions or related error moving " . 
"file to {$upload_filename}"); 

$insert_sql = "UPDATE users set user_pic_path WHERE user_id = $user_id = 
replace(user_pic_path, '$upload_filename', '$upload_filename'); 

//insert the user into the database 
mysql_query($insert_sql); 
</code> 

編輯: 我失蹤了「這是我固定的,現在還沒有SQL錯誤,它把圖片放到數據庫,但不能取代在數據庫中的圖像路徑我已經搞亂?在$ INSERT_SQL,但它仍然沒有更新爲新圖像路徑數據庫,我能做些什麼這是我的新的更新代碼:

<code> 
$insert_sql = "UPDATE users WHERE user_id = $user_id set user_pic_path = 
replace(user_pic_path, '$upload_filename', '$upload_filename')"; 
</code> 
+0

所以,你沒有任何計劃或編碼技巧,對吧?我敢打賭,有一些文件,你從哪裏得到這些代碼。 – djot

+1

你覺得'user_pic_path'代表什麼? – djot

+0

我確實有一些編程技巧,我只是比較新的PHP和堅持這一點。我不確定要在$ insert_sql中放入什麼來更新數據庫中的映像路徑。它可以很好地將圖像放入數據庫,但它不會替換路徑,這正是我期望做的 – nikito2003

回答

-1

在最後的線,插入您的SQL的一個試驗:

$insert_sql = "UPDATE users WHERE user_id = $user_id set user_pic_path = replace(user_pic_path, '$upload_filename', '$upload_filename')"; 

// check the query 
echo $insert_sql."<br />"; 

//insert the user into the database 
mysql_query($insert_sql); 

然後,您可以觀看您要運行的查詢,在PHPMyAdmin中測試它,確定它應該是什麼。對其他關鍵變量也是值得的。更好的是,您應該編寫一個「調試」功能,記錄服務器上文件中發生的情況,以便發生錯誤時可以跟蹤其詳細信息,包括每個文件中的關鍵變量值。

+0

我仍然得到一個SQL /內部服務器錯誤。我認爲我的問題是更新代碼,但我不知道如何去改變它以使其起作用 – nikito2003

+0

感謝您的幫助,我找到了答案。 – nikito2003