gpt4 book ai didi

php - 使用php使用表单更新图像问题

转载 作者:行者123 更新时间:2023-11-30 01:35:48 25 4
gpt4 key购买 nike

当我尝试更新图像时,我的 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.";

}

}

有什么关于我哪里出错的提示吗?

谢谢。

最佳答案

如果您的操作是更新并且用户没有更新图像,则仅更新信息,在这种情况下,您必须检查$_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'])."'";

希望对你有帮助

关于php - 使用php使用表单更新图像问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16910625/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com