user2657978
user2657978

Reputation: 5

Image could not be displayed

I am working on a profile page. I want to set a default image for those users whom have yet to upload a profile pic. However, the picture could not be displayed. It shows a cracked image. Why is that so?

Here is my code.

<html>
<head>
    <title>User Profile</title>
</head>
    <body>

<?php 

include ('connect.php');

if (isset($_GET['id'])) {
    $id = $_GET['id'];
    mysql_connect("localhost", "root", "") or die ("Could not connect to the server");
    mysql_select_db("userprofile") or die ("Invalid database!");
    $userquery = mysql_query("SELECT * FROM userprofile WHERE user_id = '$id'") or die ("The query could not be completed");

        while ($row = mysql_fetch_array($userquery)) {
            $id = $row['user_id'];
            $username = $row['user_name'];
            $firstname = $row['first_name'];
            $lastname = $row['last_name'];
            $email = $row['email'];
            $description = $row['description'];
            $image = $row['image'];

        }

if (empty($image)) {
    $image = "avatardefault.png";
}
?>
    <h2><?php echo $firstname; ?> <?php echo $lastname; ?>'s Profile</h2><br />
    <table>
        <tr><td>Username: </td><td><?php echo $username; ?></td></tr>
        <tr><td>Firstname: </td><td><?php echo $firstname; ?></td></tr>
        <tr><td>Lastname: </td><td><?php echo $lastname; ?></td></tr>
        <tr><td>Email: </td><td><?php echo $email; ?></td></tr>
        <tr><td>Description: </td><td><?php  echo $description; ?></td></tr>
        <tr><td>Image: <img src = "/avatars/'.$image.'" width = "200px" height = "200px"></td></tr>
    </table>

<?php 
} else die ("You will need to specify a id!");
?>

</body>
</html>

I have read through some of the similar questions on stackoverflow and has also tried out the methods. Yet to no avail. Much help is needed. Thanks.

P.S I have limited knowledge on php and mysql.

Upvotes: 0

Views: 66

Answers (2)

Corey Thompson
Corey Thompson

Reputation: 398

Your code:

<img src = "/avatars/'.$image.'" width = "200px" height = "200px">

You've forgotten PHP tags around the .$image. part. It should read:

<img src = "/avatars/<?php echo $image; ?>" width = "200px" height = "200px">

Upvotes: 0

Tobias Golbs
Tobias Golbs

Reputation: 4616

This is not inside an <?php ?> block:

<img src = "/avatars/'.$image.'"

So it should be:

<img src = "/avatars/<?php echo $image; ?>"

Upvotes: 1

Related Questions