www.webdeveloper.com
Results 1 to 4 of 4

Thread: How to retrieve and display an image from SQL database

  1. #1
    Join Date
    Apr 2014
    Posts
    42

    How to retrieve and display an image from SQL database

    The div that I want the image to be displayed in (form is sent to send_post.php):

    Code:
    <div style="width:200px ; height: 200px ; border:1px dashed red ; display:inline-block ; margin-top:5px ; margin-left:5px" class="postedBy"></div><!--end image-->
    Code from send_post.php:

    Code:
    <?php
    
    session_start();
    
        if(!isset($_SESSION['username']))   {
        header('location: mustLogin.php');
    } else  {
        $username = $_SESSION['username'];
    }
    
    $title = $_POST['title'];
    $description = $_POST['description'];
    $image = $_POST['image'];
    $dateAdded = date('Y-m-d');
    $addedBy = $username;
    
    if (!empty('title') && !empty('description') && !empty('image')) {
    //establish connection to SQL
    $conn = mysqli_connect("localhost", "root", "") or die ("Couldn't connect to SQLI");
    //connect to DB
    mysqli_select_db($conn, "accounts") or die ("Couldn't find DB");
    
    $sqliCommand = "INSERT INTO `posts` (title, description, image, date_added, added_by) VALUES ('$title', '$description', '$image', '$dateAdded', '$addedBy')" or die ('Info couldnt go to database');
    
    mysqli_query($conn, $sqliCommand) or die ('MySQLI error');
    
    header('location: profile.php?user='.$username);
    
    } else  {
    
        header('location: error.php');
    }
    The info get's sent to the database just fine, but can someone explain to me how I can get the images added by the user (all of them) to display in the first div I listed?

    So far for displaying the image in the div I have:

    Code:
    $conn = mysqli_connect("localhost", "root", "") or die ("Couldn't connect to SQLI"); mysqli_select_db($conn, "accounts") or die ("Couldn't find DB"); $sqli = ("SELECT image FROM posts WHERE added_by = '$username'"); $result = mysqli_query($conn, $sqli);
    But I don't know if that's correct, and if so, I don't know where to go from here.
    any help iis appreciated.

  2. #2
    Join Date
    Apr 2014
    Posts
    42
    also I think i should note that I'm trying to get the divs to display different images that the user has updated (without any of them repeating)

  3. #3
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,604
    Are you storing the actual image in the database (e.g. as a BLOB field), or do you just need to retrieve path/filename info to embed in IMG tags (e.g. the image is simply stored as a file on the server)?
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  4. #4
    Join Date
    Apr 2014
    Posts
    42
    I have a row called images that has the file name (i.e. penguins.jpeg). What I'm unsure of if whether or not this is the actual image, or the filename.

    I've heard about storing images on server and retrieving their filenames but hadn't had much luck on finding any documentation to help me go about doing that.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles