Hi ,

I am trying to display images stored in database.

Actually, i have stored image names with extenstion in the database, images are stored in my root directory images folder.

Now i want to display all the images with other filelds in that table, in same page.

I have written the code but its not happening. Plz help me

here is my code

 <?php  
                  require("includes/conn.php"); 



                $sql = "select CategoryID, CategoryName, FileName, Status from Categories";

                $result = mysql_query($sql) or die ("Could not access DB: " . mysql_error());



                while ($row = mysql_fetch_assoc($result))

                {

                     echo "<table border='0' cellpadding='10'>";


                                        echo "<tr><td>  </td><td>Category ID</td><td>Category Name</td><td>Status</td><td>Edit</td><td>Delete</td></tr>";

                                        echo "<tr><td> <img src=\"images/" . $row['FileName'] . "\" alt=\"\" /> </td>";
                                        echo "<td>". $row['CategoryID'] . "</td>";
                                            echo "<td>". $row['CategoryName'] . "</td>";
                                            echo "<td>". $row['Status']. "</td>";
                                            echo "<td> <a href= 'edit.php?CategoryID=" .$row['id']. "'> Edit </a></td>";
                                            echo "<td> <a href= 'delete.php?CategoryID=" .$row['id']. "'> Edit </a></td>";
                                         echo "</tr> </table>";

                }



            ?>

Please help

Check what echo "<tr><td>".$row['FileName']."</td>"; is showing and check if that file exist under images folder?

Also is this php page is at root?

FileName is the fileld name in the table and yes this file is there in the root directory

if file is exist under images folder it should show, post any one one FileName value from database.

echo "<tr><td> <img src=/images/" . $row['FileName'] . "alt='.$row['FileName'].' "." /> </td>";

No. It doesn't display. Actually its not displaying anything from that table. i feel its not entering to that display function itself. This is my complete code. Here i i do insert and then display. Please check whether am going wrong

<?php

session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link href="style.css" rel="stylesheet" type="text/css" />
<title>Welcome</title>


<script type="text/javascript" src="jquery.js"></script>
    <script type="text/javascript">
$(document).ready(function () { 

    $('#nav li').hover(
        function () {
            //show its submenu
            $('ul', this).slideDown(100);

        }, 
        function () {
            //hide its submenu
            $('ul', this).slideUp(100);         
        }
    );

});
    </script>

</head>

<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td class="header">&nbsp;</td>
  </tr>
  <tr>
    <td class="menu"></td>
  </tr>

              <tr>
                <td>
                  <table width="90%" border="0" cellspacing="0" cellpadding="0">
                    <tr>
                      <td width="26%">&nbsp;</td>
                      <td width="74%"><h2>Manage Categories</h2></td>
                    </tr>
                  </table></td>
                </tr>
              <tr>
                <td height="30">&nbsp;

                </td>
                </tr>
              <tr>
                <td>


</td>
                </tr>

                <tr>
                <td>
                <table width="49%" align="center" cellpadding="0" cellspacing="0">
                <tr><td>

                <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">

                <p>
                 <label class="style4">Category Name</label>

                   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <input type="text" name="categoryname" /><br /><br />

                    <label class="style4">Category Image</label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

                    <input type="file" name="image" /><br />

                    <input type="hidden" name="MAX_FILE_SIZE" value="100000" />

                   <br />
<br />
 <input type="submit" id="submit" value="UPLOAD" />

                </p>

                </form>

                        <?php


require("includes/conn.php");


function is_valid_type($file)

{

    $valid_types = array("image/jpg", "image/jpeg", "image/bmp", "image/gif", "image/png");

    if (in_array($file['type'], $valid_types))

        return 1;

    return 0;
}

function showContents($array)

{

    echo "<pre>";

    print_r($array);

    echo "</pre>";
}


$TARGET_PATH = "images/category/";

$cname = $_POST['categoryname'];

$image = $_FILES['image'];

$cname = mysql_real_escape_string($cname);

$image['name'] = mysql_real_escape_string($image['name']);

$TARGET_PATH .= $image['name'];

if ( $cname == "" || $image['name'] == "" )

{

    $_SESSION['error'] = "All fields are required";

    //header("Location: managecategories.php");

    exit;

}

if (!is_valid_type($image))

{

    $_SESSION['error'] = "You must upload a jpeg, gif, or bmp";

    header("Location: managecategories.php");

    exit;

}

if (file_exists($TARGET_PATH))

{

    $_SESSION['error'] = "A file with that name already exists";
    exit;

}


if (move_uploaded_file($image['tmp_name'], $TARGET_PATH))

{



    $sql = "insert into Categories (CategoryName, FileName) values ('$cname', '" . $image['name'] . "')";

    $result = mysql_query($sql) or die ("Could not insert data into DB: " . mysql_error());


    exit;

}

else

{


    $_SESSION['error'] = "Could not upload file.  Check read/write persmissions on the directory";

    header("Location: mangagecategories.php");

    exit;

}

?> 

                  </td>
                </tr>
            </table></td>
          </tr>
          <tr><td height="25">&nbsp;</td></tr>
                <tr>
                  <td></td></tr>
                <tr>
                  <td valign="top">

                 //To Display


                  <?php  
                  require("includes/conn.php"); 



                $sql = "select CategoryID, CategoryName, FileName, Status from Categories";

                $result = mysql_query($sql) or die ("Could not access DB: " . mysql_error());



                while ($row = mysql_fetch_assoc($result))

                {

                     echo "<table border='0' cellpadding='10'>";


                                        echo "<tr><td>  </td><td>Category ID</td><td>Category Name</td><td>Status</td><td>Edit</td><td>Delete</td></tr>";

                                        echo "<tr><td> <img src=\"images/" . $row['FileName'] . "\" "alt='.$row['FileName'].' "." /> </td>";
                                        echo "<td>". $row['CategoryID'] . "</td>";
                                            echo "<td>". $row['CategoryName'] . "</td>";
                                            echo "<td>". $row['Status']. "</td>";
                                            echo "<td> <a href= 'edit.php?CategoryID=" .$row['id']. "'> Edit </a></td>";
                                            echo "<td> <a href= 'delete.php?CategoryID=" .$row['id']. "'> Edit </a></td>";
                                         echo "</tr> </table>";

                }



            ?>


</body>
</html>

hello.. any solutions?

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.