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.";

}

}

关于我哪里出错的任何提示?

谢谢。

4

2 回答 2

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'])."'";

希望能帮助到你

于 2013-06-04T06:05:08.300 回答
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'];
于 2013-06-04T07:11:32.607 回答