当我尝试更新图片时,我的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.";
}
}
关于我哪里出错的提示?
感谢。
答案 0 :(得分: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'])."'";
希望有所帮助
答案 1 :(得分: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'];