Recently I learned about PHP and Database, I wrote the script below to display the image as a Profile for Author, but the photo doesn't show up! , Here my script :
echo "<tr class=\"alert\" role=\"alert\">
<td class=\"center\">$data[id_pengarang]</td>
<td class=\"d-flex align-items-center\">
<div class=\"img\">
<img src=\"img_pengarang/$data[foto]\" alt=\"$data[id_pengarang]\"/>
</div>
</td>
<td>$data[nama]</td>
<td>$data[alamat]</td>
<td>$data[email]</td>
<td>$data[hp]</td>
<td class=\"status\"><span class=\"active\">Active</span></td>
<td><a href=\"nim-view.php id=$data[id_pengarang]\">Edit</a>  
<a href=\"nim-del.php id=$data[id_pengarang]\">Hapus</a>
</td>
</tr>";
And when I'm inspect the image , any error in Console it say
user-1.png:1 Failed to load resource: the server responded with a status of 404 (Not Found)
Can someone tell me how to correct this? I tried to change double quotes to single quotes, but still doesn't work at all.
Or Maybe there is an error in the database?
Thank you for the help
CodePudding user response:
My thoughts on this:
The image should be present at the exact path given in CSS Property: background-color
.
Also, please test if the image is working by trying to place image tag and path with:
<img src=[GENERATED PATH]/>
The cleaned up code should be like this:
echo '<tr role="alert">
<td >' . $data['id_pengarang'] . '</td>
<td >
<div style="background-image: url(' . $data['foto'] . ');"></div>
</td>
<td>' . $data['nama'] . '</td>
<td>' . $data['alamat'] . '</td>
<td>' . $data['email'] . '</td>
<td>' . $data['hp'] . '</td>
<td ><span >Active</span></td>
<td><a href="nim-view.php?id=' . $data['id_pengarang'] .'">Edit</a>  
<a href="nim-del.php?id=' . $data['id_pengarang'] .'">Hapus</a>
</td>
</tr>';
CodePudding user response:
Why you are using div
instead of img
tag.
Try it, this will work:
echo "
<tr class=\"alert\" role=\"alert\">
<td class=\"center\">$data[id_pengarang]</td>
<td class=\"d-flex align-items-center\">
<div class=\"img\">
<img src="$data['foto']" alt="" />
</div>
</td>
<td>$data[nama]</td>
<td>$data[alamat]</td>
<td>$data[email]</td>
<td>$data[hp]</td>
<td class=\"status\"><span class=\"active\">Active</span></td>
<td><a href="nim-view.php id=$data[id_pengarang]\">Edit</a>  
<a href=\"nim-del.php id=$data[id_pengarang]\">Hapus</a>
</td>
</tr>";