【发布时间】:2019-08-12 09:57:58
【问题描述】:
我目前有一个登录系统,用户可以在其中注册并以用户身份登录。 我的系统是基于PHP PDO的。
当用户登录后,他们应该能够上传与其帐户相关联的图片。
现在我有一个功能齐全的登录系统,太好了,用户目前可以将图片上传到数据库,但他还不能在网站上看到它。
现在我的问题是让图片显示在网站上。
我希望用户能够看到他上传的自己的照片,而不是其他任何人的照片。
这是我目前所拥有的! :)
这是我的数据库!
具有以下行的表格图片:
描述图片
身份证
图片全名图片
标题图片
用户名
具有以下行的 TABLE USERS:
用户电子邮件
user_id
用户名
用户密码
用户电话
user_zip
到目前为止,这是我的代码:
DBH.INC.PHP
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "chhoe17";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname",
$username,
$password,
array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
}
catch(PDOException $e) {
echo $e->getMessage();
}
UPLOAD.INC.PHP
<?php
include "../upload.php";
//Find the ID of the USER
// session_start();
include_once 'dbh.inc.php';
$pictureTitle = ($_POST["filetitle"]);
$pictureText = ($_POST["filedesc"]);
//Fnd ID from the user
//$user = $_SESSION["u_id"];
$user = $_SESSION['u_id'];
$queryUserID = 'SELECT user_id from '.'users'. ' where user_name="'. $user.'";';
$stmt = $conn -> prepare($queryUserID);
$stmt -> execute();
$result = $stmt -> fetch(PDO::FETCH_ASSOC);
//FileDic
$fileDirectory = "../uploads/";
$fileHandled = $fileDirectory . basename($_FILES["file"]["name"]);
//The "tmp_name" is the temporary location the file is stored in the browser, while it waits to get uploaded
if (move_uploaded_file($_FILES["file"]["tmp_name"], $fileHandled)) {
//echo "The file " . basename($_FILES["file"]["name"]) . " has been uploaded.";
$picture = 'INSERT INTO pictures (titlePicture, descPicture, userid, imageFullNamePicture)
VALUES (:titlePicture, :descPicture, :userid, :imageFullNamePicture);';
$stmt = $conn->prepare($picture);
$stmt -> bindParam(":titlePicture", $pictureTitle);
$stmt -> bindParam(":descPicture", $pictureText);
$stmt -> bindParam(":userid", $user);
//$stmt -> bindParam(":userid", $result['user_id']);
$stmt -> bindParam(":imageFullNamePicture", $fileHandled);
$stmt -> execute();
header("Location: ../upload.php?`Success");
?>
<?php } else {
header("Location: ../upload.php?Error");
//echo "Sorry, there was an error uploading your file.";
}
header("Location: ../upload.php");
UPLOAD.PHP
<body>
<section class="main-container">
<div class="main-wrapper">
<h2>Manage your pictures</h2>
<?php
//display a message and images if logged in!
if (isset($_SESSION['u_id'])) {
echo "Upload your pictures";
echo '<div class="picture-upload">
<h2>Upload</h2>
<br>
<br>
<br>
<form action="includes/upload.inc.php" id="upload" method="POST" enctype="multipart/form-data">
<input type="text" name="filetitle" placeholder="Image title">
<input type="text" name="filedesc" placeholder="Image description">
<input type="file" id="file" name="file">
<button type="submit" name="submit">Upload</button>
</form>
</div>';
}
if (isset($_SESSION['users'])) {
echo ' <section class="picture-links">
<div class="wrapper">
<h2>Pictures</h2> ';
$user_data = 'SELECT * FROM' . ' users ' . 'INNER JOIN pictures on users.user_id
= pictures.userid WHERE name="' . $_SESSION['u_id'] . '";';
$stmt = $conn->prepare($user_data);
$stmt->execute();
while ($data = $stmt->fetch(PDO::FETCH_ASSOC)) { ?>
<div class="pictures">
<a target="file" href= <?php ?>>
<img class="pic" src= <?php echo $data['imageFullNamePicture']; ?>></a>
<div class="titlePicture"><?php echo $data['titlePicture']; ?> <br> </div>
<div class="descPicture" >Your description:</div>
<div class="text"><?php echo $data['titleDesc']; ?> <br> ?> </div>
</div>
<?php
}
};
?>
</div>
</section>
</body>
</html>
<?php
include_once 'footer.php';
?>
所以是的,问题是我无法获取连接到当前登录用户的图片以显示在页面 upload.php 上
我希望有人可以帮助我! :)
编辑!!!:
所以我目前有这段代码。 IT 应该让用户看到他上传到数据库的图片,但它非常有问题。每个用户只显示一张图片。有人可以帮助完成这项工作吗?
if (isset($_SESSION['u_id'])) {
echo ' <section class="picture-links">
<div class="wrapper">
<h2>Pictures</h2> ';
?>
<div id="pictures">
<?php
$sql = "SELECT * FROM pictures WHERE userid = '{$_SESSION['u_id']}'";
//$sql = "SELECT * FROM pictures ORDER BY userid DESC LIMIT 20;";
$stmt = $conn->prepare($sql);
$stmt->execute();
$pictures = $stmt->fetchAll();
// if ($pictures !== null) {
foreach ($pictures as $pic)
?>
<figure id="<?php echo $pic['id']; ?>">
<b><figcaption><?php echo $pic["titlePicture"] ?>
<img src = <?php echo $pic["imageFullNamePicture"] ?>>
<?php echo $pic["descPicture"] ?> <br>
</figure>
<?php
// }
}
?>
</div>
【问题讨论】:
-
图片路径是否成功保存到数据库?如果您手动输入文件的路径,它会提供图片吗?在
WHERE子句中,检查名称是否等于 u_id。应该是WHERE users.user_id = :u_id(然后绑定u_id)?我相信@ArtisticPhoenix 是对的,你需要 ti FETCH_ASSOC -
这可能有助于获取 phpmyadmin(假设您拥有)并为用户查询图片文件,以确保您拥有该权限。然后将该查询返回到您的 php 中。
-
图片路径是否成功保存到数据库? = 是的 是的 是的
-
您在使用 Upload.inc.php 之前调用了
session_start()吗?$_SESSION没有一个,因此如果你没有if (isset($_SESSION['users'])) {,你的输出都不会起作用 -
感谢您的帮助,我编辑了代码!我把代码改得更好了,但它仍然有效
标签: javascript php html mysql pdo