0

My image is saying uploaded in the URL which mean it's been correctly uploaded but I'm not sure because I don't see it appearing on my webpage or database, it appears in my folder directory though.

Here is the two pages I'm using to go through with the process.

The account.php is where I upload the image and the profile-upload.php is the serverside/backend for the account.php.

My account.php:

<?php
session_start();
include_once "safe-header.php";
include_once "serverside/database-server.php";

if (isset($_SESSION['useremail']) && !empty($_SESSION['useremail'])) {
    $useremail = $_SESSION['useremail'];
} else {
    $useremail = 'User';
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <link rel="stylesheet" href="Css/account.css">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Alternative Account Page</title>
</head>
<body>
    
<div class="upload-container">
    <p class="useremail">Welcome, <?php echo htmlspecialchars($useremail); ?></p>
    <div class="account-options">
        <a href="update-profile.php" class="update-profile">Update Profile</a>
        <a href="serverside/logout-server.php" class="logout-btn">Logout</a>
    </div>
    <div>  
    <?php
    $userId = $_SESSION['userid'] ?? null;
    $sql = "SELECT image FROM profile WHERE usersId = ?";
    $stmt = $conn->prepare($sql);
    $stmt->bind_param("i", $userId);
    $stmt->execute();
    $result = $stmt->get_result();
    $row = $result->fetch_assoc();

    if ($row && !empty($row['image'])) {
        echo '<img src="' . $row['image'] . '">';
    } else {
        echo "<p>Nothing here.</p>";
    }
    ?>
          <form action="serverside/profile-upload.php" method="post" enctype="multipart/form-data">
            <label for="profile-image">Select Image</label>
            <input type="file" name="profile-image">
            <button type="submit" name="upload-btn">Upload Image</button>
          </form>
    </div>
</div>

<?php
include_once "footer.php";
?>

Heres the profile-upload.php:

<?php
session_start();
require "database-server.php";

if (isset($_POST['upload-btn'])) {
    $validExt = ['jpg', 'jpeg', 'png'];
    $ext = strtolower(pathinfo($_FILES['profile-image']['name'], PATHINFO_EXTENSION));

    if (in_array($ext, $validExt)) {
        $location = "../uploads/";
        $target = $location . uniqid() . '.' . $ext;

    if (move_uploaded_file($_FILES['profile-image']['tmp_name'], $target)) {
        $userId = $_SESSION['userid'];
        $sql = "UPDATE profile SET image = ? WHERE usersId = ?";
        $stmt = $conn->prepare($sql);
        $stmt->bind_param("si", $target, $userId);
        $stmt->execute();
        $stmt->close();

        header("location: ../account.php?success=imageuploaded");
        exit();
    } else {
        header("location: ../account.php?error=imagecantbeuploaded");
        exit();
    }
} else {
    header("location: ../account.php?error=cannotusethisfiletype");
    exit();
}
} else {
    header("location: ../account.php");
    exit();
}

I tried editing the code around and refreshing the database but I'm not seeing the problem. It's connected to the database and the CSS is correct as well.

AJ Ande
  • 80
  • 7
  • Add error checking of the database query. See https://stackoverflow.com/questions/22662488/how-to-get-mysqli-error-in-different-environments – Barmar Aug 14 '23 at 16:59
  • Are you taking into account the difference between the physical location of the image on your server, and the relative location that you need to provide to the `` tag? If you view the html source of the page once it's drawn, does the source file path look correct to you, when you compare it to that of other images on the page? – droopsnoot Aug 14 '23 at 17:12

0 Answers0