Try to search your question here, if you can't find : Ask Any Question Now ?

How to retrieve both text and image from MySQL Database to display on webpage

HomeCategory: stackoverflowHow to retrieve both text and image from MySQL Database to display on webpage
Avatarwillson asked 4 months ago

I am designing a website and have a news area that can be edited by an admin. There is a title and entry field and the entries are stored on a MySQL database. I am also trying to add an option that allows the admin to add an image (or possibly even a video). I can get the image stored into the database correctly (I believe) but when trying to retrieve it to display on my webpage, it is showing up as a bunch of random numbers, letters, and question mark symbols. Can anyone help me figure out what I am doing wrong?

Here is my form and the code that inserts the data into the database:

        $title = trim(strip_tags($_POST['title']));
        $entry = trim(strip_tags($_POST['entry']));
        $image = ($_POST['image']);
        $imgContent = addslashes(file_get_contents($image));

    $problem = TRUE;
    if ($problem) {
        // Define the query:
        $query = "INSERT INTO entries (entry_id, title, entry, date_entered, image) VALUES (0, '$title', '$entry', NOW(),'$imgContent')";
        // Execute the query
        if(@mysqli_query($dbc, $query) && ((!empty($_POST['entry'])) || (!empty($_POST['title'])) )) {
            print '<p>The blog entry has been added!</p>';
        }else{  
            print '<p style="color: red;">Could not add the entry. Please fill in one of the fields.</p>';
        }   
    } 
    mysqli_close($dbc);

} // End of form submission IF

print '<form action="add_entry.php" method="post">
    <p>Entry Title: <input type="text" name="title" size="40" maxsize="100" /></p>
    <p>Entry Text: <textarea name="entry" cols="40" rows="5"></textarea></p>
    <p><input type="file" name="image" /></p>
    <input type="submit" name="submit" value="Post This Entry!" />
    <input type="hidden" name="submitted" value="true" />
</form>';

Here is the code for viewing the data on the news page:

// Define the query:
$query = 'SELECT * FROM entries ORDER BY date_entered DESC';

if ($r = mysqli_query($dbc, $query)) { // Run the query.

    // Retrieve and print every record:
    while ($row = mysqli_fetch_array($r)) {
        print "{$row['image']}
        <dl><dt><h3><strong>{$row['title']}</strong></h3></dt>
        <dd>{$row['entry']}<br /><br />n</dd></dl>";
}
1 Answers
Best Answer
AvatarJyoti answered 4 months ago
Your Answer

9 + 7 =

Popular Tags

WP Facebook Auto Publish Powered By : XYZScripts.com