2013-06-04 56 views
0

我遇到了問題,當我嘗試更新我希望有人可以幫助的圖像時,我的php表單出現問題。使用表格更新圖像問題使用表格

表單將更新的字段提交給數據庫,但是當我更新時,將圖像路徑保存到數據庫並移動圖像的圖像上傳功能有問題。

當我嘗試編輯/更新圖像時,表格中的所有記錄都會被相同的數據覆蓋並且不會保存圖像。

這是更新腳本我寫道:

<?php 

//include database connection 

include 'includes/db_connect.php'; 

//check any user action 

$action = isset($_POST['action']) ? $_POST['action'] : ""; 

if($action == "update"){ //if the user hit the update button 

//This is the directory where images will be saved 
$target = "images/"; 
$target = $target . basename($_FILES['profileimage']['name']); 
move_uploaded_file($_FILES['profileimage']['tmp_name'], $target); 

    //write our update query 

$query = "update models 
set 
name = '".$mysqli->real_escape_string($_POST['name'])."', 
artisttype = '".$mysqli->real_escape_string($_POST['artisttype'])."', 
residence = '".$mysqli->real_escape_string($_POST['residence'])."', 
agerange = '".$mysqli->real_escape_string($_POST['agerange'])."', 
complexion = '".$mysqli->real_escape_string($_POST['complexion'])."', 
enthnicity = '".$mysqli->real_escape_string($_POST['enthnicity'])."', 
haircolor = '".$mysqli->real_escape_string($_POST['haircolor'])."', 
hairlength = '".$mysqli->real_escape_string($_POST['hairlength'])."', 
eyecolor = '".$mysqli->real_escape_string($_POST['eyecolor'])."', 
height = '".$mysqli->real_escape_string($_POST['height'])."', 
weight = '".$mysqli->real_escape_string($_POST['weight'])."', 
bustsize = '".$mysqli->real_escape_string($_POST['bustsize'])."', 
waistsize = '".$mysqli->real_escape_string($_POST['waistsize'])."', 
hipsize = '".$mysqli->real_escape_string($_POST['hipsize'])."', 
shoesize = '".$mysqli->real_escape_string($_POST['shoesize'])."', 
insideleg = '".$mysqli->real_escape_string($_POST['insideleg'])."', 
outsideleg = '".$mysqli->real_escape_string($_POST['outsideleg'])."', 
dresssize = '".$mysqli->real_escape_string($_POST['dresssize'])."', 
profileimage = '".$mysqli->real_escape_string($_FILES['profileimage']['name'])."', 
brasize = '".$mysqli->real_escape_string($_POST['brasize'])."' 
WHERE id='".$mysqli->real_escape_string($_REQUEST['id'])."'"; 


//execute the query 

if($mysqli->query($query)) { 

//if updating the record was successful 

echo "User was updated."; 

}else{ 

//if unable to update new record 

echo "Database Error: Unable to update record."; 

} 

} 

在哪裏我去錯任何提示嗎?

謝謝。

+3

你應該用的print_r($ _ FILES)測試;檢查$ _FILES是否設置。 – Hasina

+0

與插入和編輯/更新相同嗎? – Roopendra

回答

0

如果你的動作是update和用戶沒有更新的圖像只更新你要檢查$_FILES如果是空的,然後如果用戶選擇新的圖像,然後更新的像場像

不更新像場這種情況下,信息
//write our update query 

$query = "update models 
set 
name = '".$mysqli->real_escape_string($_POST['name'])."', 
artisttype = '".$mysqli->real_escape_string($_POST['artisttype'])."', 
residence = '".$mysqli->real_escape_string($_POST['residence'])."', 
agerange = '".$mysqli->real_escape_string($_POST['agerange'])."', 
complexion = '".$mysqli->real_escape_string($_POST['complexion'])."', 
enthnicity = '".$mysqli->real_escape_string($_POST['enthnicity'])."', 
haircolor = '".$mysqli->real_escape_string($_POST['haircolor'])."', 
hairlength = '".$mysqli->real_escape_string($_POST['hairlength'])."', 
eyecolor = '".$mysqli->real_escape_string($_POST['eyecolor'])."', 
height = '".$mysqli->real_escape_string($_POST['height'])."', 
weight = '".$mysqli->real_escape_string($_POST['weight'])."', 
bustsize = '".$mysqli->real_escape_string($_POST['bustsize'])."', 
waistsize = '".$mysqli->real_escape_string($_POST['waistsize'])."', 
hipsize = '".$mysqli->real_escape_string($_POST['hipsize'])."', 
shoesize = '".$mysqli->real_escape_string($_POST['shoesize'])."', 
insideleg = '".$mysqli->real_escape_string($_POST['insideleg'])."', 
outsideleg = '".$mysqli->real_escape_string($_POST['outsideleg'])."', 
dresssize = '".$mysqli->real_escape_string($_POST['dresssize'])."',  
brasize = '".$mysqli->real_escape_string($_POST['brasize'])."' "; 

if(!empty($_FILES['profileimage'])){ 
$target = "images/"; 
$target = $target . basename($_FILES['profileimage']['name']); 
move_uploaded_file($_FILES['profileimage']['tmp_name'], $target); 
$query.=",profileimage = '".$mysqli->real_escape_string($_FILES['profileimage']['name'])."'"; 
} 

$query.=" WHERE id='".$mysqli->real_escape_string($_REQUEST['id'])."'"; 

希望它可以幫助

0
$query = "update models 
set 
name = '".$mysqli->real_escape_string($_POST['name'])."', 
artisttype = '".$mysqli->real_escape_string($_POST['artisttype'])."', 
residence = '".$mysqli->real_escape_string($_POST['residence'])."', 
agerange = '".$mysqli->real_escape_string($_POST['agerange'])."', 
complexion = '".$mysqli->real_escape_string($_POST['complexion'])."', 
enthnicity = '".$mysqli->real_escape_string($_POST['enthnicity'])."', 
haircolor = '".$mysqli->real_escape_string($_POST['haircolor'])."', 
hairlength = '".$mysqli->real_escape_string($_POST['hairlength'])."', 
eyecolor = '".$mysqli->real_escape_string($_POST['eyecolor'])."', 
height = '".$mysqli->real_escape_string($_POST['height'])."', 
weight = '".$mysqli->real_escape_string($_POST['weight'])."', 
bustsize = '".$mysqli->real_escape_string($_POST['bustsize'])."', 
waistsize = '".$mysqli->real_escape_string($_POST['waistsize'])."', 
hipsize = '".$mysqli->real_escape_string($_POST['hipsize'])."', 
shoesize = '".$mysqli->real_escape_string($_POST['shoesize'])."', 
insideleg = '".$mysqli->real_escape_string($_POST['insideleg'])."', 
outsideleg = '".$mysqli->real_escape_string($_POST['outsideleg'])."', 
dresssize = '".$mysqli->real_escape_string($_POST['dresssize'])."',  
brasize = '".$mysqli->real_escape_string($_POST['brasize'])."' "; 

if(!empty($_FILES['profileimage'])){ 
$target = "images/"; 
$target = $target . basename($_FILES['profileimage']['name']); 
move_uploaded_file($_FILES['profileimage']['tmp_name'], $target); 
$query.=",profileimage = '".$mysqli->real_escape_string($_FILES['profileimage']['name'])."'"; 
} 

$query.=" WHERE id='".$mysqli->real_escape_string($_REQUEST['id'])."'"; 

please use $_POST['id']. save id value in hidden field and use id from $_POST['id'];