我正在尝试为我的网站创建个人资料图片功能。文件上传和图像显示功能正常工作,但是,它不会将我表中的“配置文件”列更新为图像名称。
我的数据库名称是“intranet”,表名称是“profile”。这是我的源代码:
<?php session_start();
$_SESSION['username'];
?>
<?php
if(isset($_POST['submit'])){
move_uploaded_file($_FILES['file']['tmp_name'],"images/profile/".$_FILES['file']['name']);
$con = mysqli_connect("localhost","root","","intranet");
$q = mysqli_query($con,"UPDATE user SET profile = '".$_FILES['file']['name']."' WHERE username = '".$_SESSION['username']."'");
}
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" name="submit">
</form>
<?php
$con = mysqli_connect("localhost","root","","intranet");
$q = mysqli_query($con,"SELECT * FROM user");
while($row = mysqli_fetch_assoc($q)){
echo $row['Username'];
if($row['profile'] == ""){
echo "<img width='100' height='100' src='images/profile/default.jpg' alt='Default Profile Pic'>";
} else {
echo "<img width='100' height='100' src='images/profile/".$row['image']."' alt='Profile Pic'>";
}
echo "<br>";
}
?>
</body>
</html>
最佳答案
"I got this result from running the "echo" Notice: Undefined index: username in C:\IntranetBeta\test.php on line 9 UPDATE user SET profile = 'Koala.jpg' WHERE username = ''"
这段代码:
<?php session_start();
$_SESSION['username'];
?>
没有任何内容分配给该 session 数组,因此您需要将其分配给某些内容。
如果它来自上一个页面并在那里分配,那么您将需要检查它是否已设置以及分配给了什么。
否则,您将需要删除它或为其分配一些内容。
即:
<?php
session_start();
if(isset($_SESSION['username'])){
$_SESSION['username'] = "John Doe";
}
else{
$_SESSION['username'] = "Empty";
}
?>
另外,正如我之前在评论中指出的那样,在尝试查询之前先建立您的连接。
- “这是因为您没有在它上面进行连接。将您的连接代码单独放置在所有内容之上。”
关于php - 无法在 PHP 中更新图像源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29613011/