Hey guys.... please help me out on sorting this php issue... i have made a form to upload images to folder and its details to database but not able to code a form to retrive image from folder and its details from database.


HTML form -

<form method="post" action="addMember.php" enctype="multipart/form-data">
    <p>
            
            </p>
            <p>
              Product Name
            </p>
            <input type="text" name="productname"/>
            
            <br><p>
              Product Image:
            </p>
            <input type="hidden" name="size" value="350000">
            <input type="file" name="photo"> 
        <br>
            <p>
              Product Details :
            </p>
<textarea rows="10" cols="35" name="productdetails">
</textarea>
                     <br/>
            <br/>
            <input TYPE="submit" name="upload" title="Add data to the Database" value="Submit"/>
          </form>

addMember.php -

<?php

//This is the directory where images will be saved
$target = "images/";
$target = $target . basename( $_FILES['photo']['name']);

//This gets all the other information from the form
$name=$_POST['productname'];
$pic=($_FILES['photo']['name']);
$about=$_POST['productdetails'];

// Connects to your Database
mysql_connect("localhost", "root", "") or die(mysql_error()) ;
mysql_select_db("emp") or die(mysql_error()) ;

//Writes the information to the database
mysql_query("INSERT INTO empp (productname,photo,productdetails)
VALUES ('$name', '$pic', '$about')") ;

//Writes the photo to the server
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))
{

//Tells you if its all ok
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";
}
else {

//Gives and error if its not
echo "Sorry, there was a problem uploading your file.";
}
?>

Dani AI

Generated

— since the upload already writes the filename to your table (as noted), the display page just needs to SELECT the rows, build a safe URL to the stored filename, and print the HTML for each product while escaping output to avoid XSS. Common mistakes are echoing the whole row variable or forgetting to use the specific column names, not checking the filesystem path, and not URL-encoding filenames with spaces.

Example retrieval (use this as view.php — change DB credentials and the table/column names to match yours):

<?php
$mysqli = new mysqli('localhost','root','','emp');
if ($mysqli->connect_error) { die('Connect error: '.$mysqli->connect_error); }

$sql = "SELECT productname, photo, productdetails FROM empp";
if ($res = $mysqli->query($sql)) {
    while ($row = $res->fetch_assoc()) {
        $filePath = __DIR__ . '/images/' . basename($row['photo']);
        $imgUrl = file_exists($filePath)
            ? 'images/' . rawurlencode(basename($row['photo']))
            : 'images/placeholder.png';

        $name = htmlspecialchars($row['productname'], ENT_QUOTES, 'UTF-8');
        $details = nl2br(htmlspecialchars($row['productdetails'], ENT_QUOTES, 'UTF-8'));

        echo '<div class="product">';
        echo '<img src="' . $imgUrl . '" alt="' . $name . '" style="max-width:200px;">';
        echo '<h3>' . $name . '</h3>';
        echo '<p>' . $details . '</p>';
        echo '</div>';
    }
    $res->free();
}
$mysqli->close();
?>

Troubleshooting: verify the images/ path is correct relative to view.php, confirm webserver read permissions on that folder, ensure the DB photo field contains just the filename (not a full server path), and provide a placeholder image for missing files. For security and compatibility use mysqli or PDO (mysql_* was removed in modern PHP), validate uploads on the upload side, use move_uploaded_file, create unique filenames, and escape user output with htmlspecialchars. See the PHP docs for mysqli, htmlspecialchars, and move_uploaded_file.

Recommended Answers

All 3 Replies

I have checked your code its working fine.
What do you mean by code a form to retrieve image.

Well what i exactly mean is using the above form and php file i am able to upload image to folder and its details to the database but m not able to retrive it ..

example :

When i click open view.php


<image1 from folder> - <details of the image1 which is stored in database>

<image2 from folder> - <details of the image2 which is stored in database>

i guess that explains what i actually want...

got it .. the code to view is -

<?php
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("emp") or die(mysql_error());
$query = "SELECT * FROM empp";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
Echo "<img src=images/".$row . "><br>";
Echo "<b>Product Name:</b> ".$row . "<br> ";
Echo "<b>Product Details :</b> ".$row . " <hr>"; } ?>

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.