gpt4 book ai didi

PHP + MySQLi - 尝试使用 enctype ="multipart/form-data"将图像上传到本地主机数据库中的用户行

转载 作者:行者123 更新时间:2023-11-28 01:18:17 24 4
gpt4 key购买 nike

我正在为登录的用户创建一个用户个人资料更新页面,以编辑他们的姓名、电子邮件和个人资料图片。我已成功更新文本,但无法使图片更新正常工作。没有产生错误,但数据库中的字段从空变为“”(空白)。我在下面粘贴了我的更新帐户页面代码,如果您能帮助我找出问题所在,我们将不胜感激!

<?php require 'Assets/Connections/Connections.php'; ?>
<?php session_start();
if(isset($_SESSION["UserID"])){
}else{
header('Location: LogIn.php');
die();
}
?>
<?php

$User = $_SESSION["UserID"];

$result = $con->query("SELECT * FROM user WHERE UserID ='$User'");

$row = $result->fetch_array(MYSQLI_BOTH);

$_SESSION["FirstName"] = $row['Fname'];
$_SESSION["LastName"] = $row['Lname'];
$_SESSION["Email"] = $row['Email'];
$_SESSION["PW"] = $row['Password'];
#$_SESSION["Picture"] = $row['ProfilePicture'];
?>
<?php

if(isset($_POST['Update'])){

$UpdateFName = $_POST['FirstName'];
$UpdateLName = $_POST['LastName'];
$UpdateEmail = $_POST['Email'];
$UpdatePassword = $_POST['Password'];
$StorePassword = password_hash($UpdatePassword, PASSWORD_BCRYPT, array('cost' => 10));

move_uploaded_file($_FILES['file']['tmp_name'], 'Assests/Images/'.$_FILES['file']['name']);
#move_uploaded_file($_FILES['file']['tmp_name'],"Assets/Images/".$_FILES['file']['name']);


$sql = $con->query("UPDATE user SET Fname = '{$UpdateFName}', Lname = '{$UpdateLName}', Email = '{$UpdateEmail}', Password = '{$StorePassword}', ProfileImage = '".$_FILES['file']['name']."' WHERE UserID = $User");

header('Location: Account.php');
die();
}
?>
<!doctype html>
<html>
<head>
<link href="Assets/CSS/Master.css" rel="stylesheet" type="text/css" />
<link href="Assets/CSS/Menu.css" rel="stylesheet" type="text/css" />
<meta charset="utf-8">
<title>Update Account</title>
</head>

<body>
<div class="Container">
<div class="Header"></div>
<div class="Menu">
<div id="Menu">
<nav>
<ul class="cssmenu">

<li><a href="Home.php">Home</a></li>

<li><a href="Account.php">Account</a></li>

<li><a href="Projects.php">Projects</a></li>

<li><a href="Users.php">Users</a></li>

<li><a href="LogOut.php">LogOut</a></li>

</ul>
</nav>
</div>
</div>
<div class="LeftBody"></div>
<div class="RightBody">
<form id="form1" name="form1" method="post">
<div class="FormElement">
<input name="FirstName" type="text" class="TField" id="FirstName" value="<?php echo $_SESSION["FirstName"]; ?>">
</div>
<div class="FormElement">
<input name="LastName" type="text" class="TField" id="LastName" value="<?php echo $_SESSION["LastName"]; ?>">
</div>
<div class="FormElement">
<input name="Email" type="email" class="TField" id="Email" value="<?php echo $_SESSION["Email"]; ?>">
</div>
<div class="FormElement">
<input name="Password" type="password" class="TField" id="Password" placeholder="Password" required="requried">
</div>
<div class="FormElement" action="" method="post" enctype="multipart/form-data">
<input type="file" name="file">
<br><br>
</div>
<div class="FormElement">
<input name="Update" type="submit" class="button" id="Update" value="Submit Changes">
</div>
</form>
</div>
<div class="Footer"></div>
</div>
</body>
</html>

最佳答案

你的表单元素需要enctype="multipart/form-data"来上传文件

你的表单标签是

 <form id="form1" name="form1" method="post" enctype="multipart/form-data">

从你的 div 中删除 action=""method="post"enctype="multipart/form-data"

 <div class="FormElement" action="" method="post" enctype="multipart/form-data">
<input type="file" name="file">
<br><br>
</div>

这些是表单元素。你在你的表格中提到这些东西

<div class="FormElement">
<input type="file" name="file">
<br><br>
</div>

阅读form

关于PHP + MySQLi - 尝试使用 enctype ="multipart/form-data"将图像上传到本地主机数据库中的用户行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34854019/

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