I am building a simple webpage where I want to display a few images that are located on my computer # C:\xampp\htdocs\website\upload. I have stored their (relative) paths into the MySQL database, like this: website/upload/filename.extension.
Example:
INSERT INTO `photos` (`photoid`, `photopath`, `photoname`, `photoyear`)
VALUES ('20', 'website/upload/Putin.jpg', 'Putin', '2017');
The PHP code that's supposed to retrieve them is:
$query = "SELECT * FROM photos";
$result = mysqli_query($mysqli, $query);
while($photo = mysqli_fetch_assoc($result)) {
echo '<img src="'. $photo['photopath'] .'" alt="'.$photo['photoname'].'" height:"100" width="100"> <br />';
}
However, it only returns one picture and completely ignores the others, the result being something like this. And if I remove that particular picture from my database, it displays none of the others, although their location and filetype coincide.
What can I do so that all the images are displayed successfully?
Thank you!
Right-click on the images with errors and open Inspect Element, check whether the links are ok. If there's a 404 for images then the problem is with the link that you have stored.
Related
As the title says, I can't insert & display any image to & from mySQL db table. Image table looks like the photo attached and my php code looks like this:
<?php
$sql2 = "SELECT * FROM images";
$records2 = mysql_query($sql2);
while($images = mysql_fetch_assoc($records2)) {
echo $images['path'];
}
?>
The path is displayed in the page, not the actual image.
I've made the MYSQL connection so it is fine.
This is an answer that I had prepared beforehand, but never submitted it at the time.
Consult "Footnotes".
You're just echoing the path here and not the image source.
I.e.: <img src..>.
You might have problems with this also, in the way you set that path for it, if and when you access your executable from a different location.
echo "<img src=\"$images['path']\">";
If that doesn't work, then that will mean that the path you've set for it, should have been as a full server path starting at the root, and not a relative path.
If so, then you'll need to add to it.
I.e.: echo "<img src=\"/var/usr/htdocs/Projects/facebook/img/$images['path']\">";
or and as an example from the screenshot you left:
echo "<img src=\"/Projects/facebook/img/$images['path']\">";
Footnotes:
"It works now #Fred-ii- the mySQL path wasn't quite accurate – Sergiu Turus 5 hours ago"
It seems that I was right all along.
I have code where I upload 6 images in one part of my site, that code worked fine, now, I need to update or change any image that was uploaded, I mean I need to change the image number 3 and 6, for example, so I wrote this code
$i=0;
while ($i<=5){
if (!empty($_FILES['ufile']['name'][$i]) and ($_FILES['ufile']['name'][$i]<>"")){
$path[$i] = "../slider_new/".$_FILES['ufile']['name'][$i];
$path[$i] = str_replace(' ', '_',$path[$i]);
copy($_FILES['ufile']['tmp_name'][$i], $path[$i]);
echo "Ruta :".$path[$i]."<BR/>";
echo "File Name :".$_FILES['ufile']['name'][$i]."<BR/>";
echo "este es $i ",$i;
$sql="UPDATE accommo_main_images SET name='".$_FILES['ufile']['name'][$i]."',ruta='".$path[$i]."' where num='$num'";
$res=mysqli_query($cnx,$sql);
}
$i=$i+1;
}
The idea is that check the name and when it is different to "" so..... update the image, that is the idea but I don´t know why the code update all the 6 images with the image that I selected.
What cam be the problem ?
Thank you for you help
where num='$num'";
You use a SQL statement WHERE NUM=$num. Yet, you did not set $num anywhere in the code you gave us here. Most likely you never set the value of $num. You probally ment $i.
I'm guessing you also possibly made the same mistake in your upload code (initial upload) and therefor all images have number=0.
You are now updating images which have number 0, or empty string (depending on database field type) which would match every image you have in there.
Try running this code and check the table entries.
$res = mysqli_query($cnx,"SELECT num,name FROM accommo_main_images");
var_dump(mysqli_fetch_all($res,MYSQLI_ASSOC));
Am wondering why these image isn't showing even after properly retrieving the image path from MYSQL i've gone through the code and i still can't figure out why this is:
<?php
$sql = "SELECT description, quantity, product_price, category, product_img_url";
$sql .= " FROM *****, *****";
$sql .= " WHERE product_id = product_img_id";
$sql .= " AND product_id = ?";
$sql .= " LIMIT 1";
$stmt = $mysqli->prepare($sql);
$stmt->bind_param('s', $id);
$stmt->execute();
$stmt->store_result();
if($stmt->num_rows==1){
$stmt->bind_result($desc,$qty,$pr,$cat,$product_img_url);
$stmt->fetch();
?>
<div id="infobox">
<h3>Product View</h3>
<p align="left">
<?php
if(file_exists($product_img_url)){
echo '<img src="'.$product_img_url.'" alt="" width="300" height="300" />';
}else{
echo '<img src="" alt="product image" />';
}
?>
Please can someone tell me why this is happening?
If the image file does exist, then this might have something to do with the path.
I would use firebug to play with the image src path and try various things like putting a slash in the beginning, or removing it, or entering absolute path of image.
Once I get the image to show up in firebug, I would then proceed to fix the issue in the code.
As i am see you code and i just want to ask you that you have store complete path of your image in db or you have store just some part of it.
as i can see you have retrieve image path from mysql but the browser not able to retrieve image from your server path it may be because of path you r providing and and where the file store not match. the best way to check your file path and mysql path use
var_dump(your result set) and check you output weather image url match with your server image path, and i am prety much sure that your path will not match.
Can you please try with the following path structure
echo '<img src="/application_root_directory/'.$product_img_url.'" alt="" width="300" height="300" />';
Well, You can do like:
$product_img_url="guarantee.png"; ?>
<img src="images\<?php echo $product_img_url; ?>" alt="Hello">
It will not installed by default you have to install it manually. Either you can do goto tools in menubar>add-ons, and search in search fox for firebug or goto firebug website to install it.
Thanks everyone for all useful ideas presented, I appreciate. I've found a way to resolve the relative/absolute links. The images are now being displayed as they should! What I did was; since I have sub directories under my root folder, I created separate 'Meta' php scripts in each sub directory with names like INC, __ADM, etc. Each script links to another script in a toplevel directory. All I placed within the scripts were just paths, that I got using DOCUMENT_ROOT, doing this I was able to get the actual path to the image folder, that I then placed in the DB. Hope this idea helps someone else!
Hello dear Overflowers!
I have a little code that so far works so well,
it gets the corresponding Image ID to the corresponding House ID, also Caption and filename are no problem!
I just fail at displaying the image... :(
`
include("functions/config.php");
$images_dir = "/some/url/that/is/correct/cms/houses/";
$query = "SELECT houses.*, gallery_photos.* ".
"FROM houses LEFT JOIN gallery_photos ".
"ON houses.id = gallery_photos.photo_category";
$result = mysql_query($query) or die(mysql_error());
// Print out the contents of each row into a table
while($row = mysql_fetch_array($result)){
$house_id = $row['id'];
$photo_category = $row['photo_category'];
$photo_caption = $row['photo_caption'];
$photo_filename = $row['photo_filename'];
echo "House ID ". $house_id. " - ". $photo_category." - ". $photo_caption." - <img src='".$images_dir.$photo_filename."' />";
echo "<br />";
}
?>`
So as said, everything get selected well, even the image path is correct when you click Image Info in the browser, but I simply do not get the image to display, am I missing something very trivial here?
Thanks in advance!
Check Image Name in Table as well as in folder where you store particular image.
<img src='".$images_dir.$photo_filename."' />
It misses like .jpg or .png whatever the extension of your img is
Check with tool like Firebug what is the path in src attribute of img element. Then copy this link and paste it in browser to see if it opens (with your website name - so if link is '/img/houses/house1.jpg' then you paste to browser 'www.mypage.com/img/houses/house1.jpg').
If it does not then:
Check if path is correct (it will count from web root folder) (your path should be probably like '/cms/houses/5953.jpg')
Check of path has correct letter sizes ('house' vs. 'House')
Check if you can open file on server (maybe file is corrupted)
Check if you have rights to read file
All right guys, finally found the mistake, which I hope that with my anwser, some pople in the future can avoid it!
As it seems some Hosts do not like some of the PHP naming conventions, so what I had to do, so the files would actually be stored in the folder, was change the command to get them from "Tmp/In memory" to the final destination folder.
For better understanding, here what I changed :
copy($photos_uploaded['tmp_name'][$counter],
$images_dir . '/' . $filename);
to
move_uploaded_file($photos_uploaded['tmp_name'][$counter],
$images_dir . '/' . $filename);
I hope no one else ever has to fiddle this long and spend so much money on Coffee ever again.
Thanks to all that tried to help anyway!
I have a folder of images and a MySQL database to manage them. The database has one table: images, and 7 fields: imgID, imgTitle, imgURL, imgDate, imgClass, imgFamily, & imgGender. The primary key is imgID and the Index Key is imgDate.
I wish to display thumbnails of these images on a web page as links to the large versions, using the imgDate to organize them with newest first, and oldest last. The images have the same file names as their thumbnails. The large images are located in new_arrivals_img/ relative to the site root, and the thumbnails are located in new_arrivals_img/thumbnails/ again relative to the site root.
As I'm new to both MySQL and PHP, I was hoping someone could help me with the code. Everything I have tried has failed.
Right now I'm building the site, and thus using MAMP to host it locally. I have had some problem figuring out relative paths for my images. Is there a way to set new_arrivals_img/ as the root?
given no code, I think this is what you are looking for
//connect to mysql database
$query = mysql_query("SELECT `imgURL` FROM `images` ORDER BY `imgDate` DESC") or die(mysql_error());
if(!$query) {
echo "Cannot retrieve information from database.";
} else {
while($row = mysql_fetch_assoc($query)) {
echo "<img src='new_arrivals_img/thumbnails/".$row['imgURL']."'> <br/>";
}
}
this would show the images with line break from the folder new_arrivals_img/thumbnails