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));
Related
How are these images being shown. Are they displaying it by calling the php echo on the set html table or do they just echo it using while loop. The data are taken from the database. and how do they make the 4th image a new row after 3 images are displayed. Beginner here. Thanks.
The Basic Principle is Keep track of the total number of pictures you add with a variable, say $reconi, and if the modulus (Remainder of Division by 3) is 0, then add a line break (In the example below I am showing Images pulled from a Directory, I already had the script handy, but you shall easily adapt it for doing from database as well). If not clear, feel free to get back.
$directory = "D:/ItemPics/";
$files = glob($directory . "*");
$reconi=1;
foreach($files as $file){
if($reconi % 3==0){
// Add picture
// Add a line break
}else{
// Add picture;
}
$reconi++
}
I'm basically a designer who hand codes HTML, but am a hack when it comes to PHP. I've been asked to add image icons within a product search results table on a PHP/MySQL site that has had many programmers over a decade, causing a sloppy mess of code.
I have the following code at the top of a search results page that calls out the name of the image:
$image2 = $row['item'] . ".jpg";
$imagefile2 = $_SERVER['DOCUMENT_ROOT'] . "/product_images/$imagefile2";
if(file_exists($imagefile2)){
Within the table itself I hacked this to get the correct image to show:
print "<td><center><img src=/product_images/$image2 width=80><br>
Of course, if there is no image, there is a broken image link. I dod not know the proper syntax to tell the server that IF there is no image, THEN show noimage.jpg (located in same folder). This is probably a couple lines of added code at best, but after a couple hours of searches and attempts I surrender.
Check to see if the file exists and if it does, set the image2 variable to point to it. If it doesn't exist, set the image2 variable to point to the "no image" image.
if(file_exists($_SERVER['DOCUMENT_ROOT'] . "/product_images/".$row['item'].".jpg")){
$image2=$row['item'].".jpg";
}else{
$image2="/product_images/noimage.png";
}
<?php
if(file_exists($_SERVER['DOCUMENT_ROOT'] . "/product_images/".$row['item'].".jpg")){
$image2=$row['item'].".jpg";
}else{
// Specify No Image File Path to Variable
$image2="/product_images/noimage.png";
}
?>
<td><center><img src="<?php echo $image2; ?>" width="80"><br>
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.
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 edited the title, so I hope this one will be helpful...
Case:
I have many images and have uploaded it to a server. I was manually change their names with numbers. Of course I remember the last number of the file but could anyone help me with upload script that enable me to check the last number existed on the server folder and rename the file I'm gonna upload so that the name of the file is last_number_exist_on_server+1?
Ah, helping on generating the thumbnail to a specific folder and size will be appreciated also.
Last but not least, I am using PHP. No database... and the image file type is JPG.
Sorry, newbie's question.
Note: there is zero error checking here. Add some before you use in production! :)
<?php
// get the next number for our filename
// returns an array of files and directories in descending alpha-order
$files = scandir("./images", SCANDIR_SORT_DESCENDING);
$last_filename = array_shift($files);
$last_number = (int)substr($last_filename, 0, strrpos($last_filename, "."));
$next_number = $last_number++;
// now rename your file...
?>