我在将图像路径上传到数据库时遇到问题。我有一个页面tutorLanding.php
,其中包含文件上传表单,允许用户更改其个人资料图像。
tutorLanding.php
<div class='upload-form'>
<form action='changeProfile.php?studentNumber='$studentID' method='POST' enctype='multipart/form-data'>
Change Profile Image:
<input type='file' name='fileToUpload' id='fileToUpload'>
<input type='submit' value='Upload Image' name='submit'>
<br>
</form>
</div>";
}
} else {
echo "<div class = 'imageWrapper'>
<img src = '/img/avatar/defaultAvatar.png' alt = 'your image'/>
</div>";
}
我正在尝试通过以下方式传递$ _GET:
<form action='changeProfile.php?studentNumber='$studentID' method='POST' enctype='multipart/form-data'>
因此,我可以为数据库中的唯一学生分配新的个人资料图片。
但是,当我进入changeProfile.php
时,这根本不起作用。没有文件路径移动到数据库中,也没有图像移动到我的文件夹中。我已启用所有PHP错误,但我只得到undefined index: studentID
,这使我相信$_GET
是此问题。即使当我将值'40061947'(用户1)硬编码为$studentID
时也不会通过。
我在这里想念什么?
changeProfile.php
$studentID = $_GET['studentNumber'];
$userQuery = "SELECT * from users WHERE studentNumber = '$studentID'";
$resultQuery = (mysqli_query($conn, $userQuery));
if (isset($_POST['submit'])) {
$fileName = $_FILES['fileToUpload']['name'];
$maxsize = 2097152;
$format = array('image/jpeg');
if ($_FILES['fileToUpload']['size'] >= $maxsize) {
$error_1 = 'File Size too large';
echo '<script>alert("' . $error_1 . '")</script>';
} elseif ($_FILES['fileToUpload']['size'] == 0) {
$error_2 = 'Invalid File';
echo '<script>alert("' . $error_2 . '")</script>';
} elseif (!in_array($_FILES['fileToUpload']['type'], $format)) {
$error_3 = 'Format Not Supported. Only .jpeg files are accepted';
echo '<script>alert("' . $error_3 . '")</script>';
} else {
$target = 'img/profile_images';
$tempFileName = $_FILES["fileToUpload"]["tmp_name"];
$fileTarget = $target . $fileName;
$result = move_uploaded_file($tempFileName, $fileTarget);
if ($result) {
echo "Your file <html><b><i>" . $fileName . "</i></b></html> has been successfully uploaded";
$insertPath = "INSERT INTO users (imagePath) VALUES ('$fileName') WHERE studentNumber ='$studentID'";
echo "$studentID";
$resultQuery = (mysqli_query($conn, $insertPath));
} else {
echo "Sorry !!! There was an error in uploading your file";
}
}
答案 0 :(得分:1)
$insertPath = "INSERT INTO users (imagePath) VALUES ('$fileName') WHERE studentNumber ='$studentID'";
应该更新而不是插入吗?由于您已经在尝试从数据库表用户中提取用户信息,因此该用户的实例已经存在?
尝试将代码更改为
$insertPath = "UPDATE users SET imagePath = '".$filename."' WHERE studentNumber = '".$studentID."'");
答案 1 :(得分:0)
您似乎在表单声明中没有右引号:
<form action='changeProfile.php?studentNumber='$studentID' method='POST' enctype='multipart/form-data'>
如您所见,您的表单操作不包含$ studentID。尝试将其更改为
<form action="changeProfile.php?studentNumber={$studentID}" method='POST' enctype='multipart/form-data'>