1

I'm trying to display an image which have been stored in MySQL, but haven't been able to get a success just yet. Apparently echoing the table header (img) gives me back something like this

enter image description here

In addition I would like to be able to add the image in the website itself rather than using the phpmyadmin and inserting the image there.

As of now this is the code I have for the standing.php page

<?php
    require_once('database.php');

    // Get all categories
    $query = 'SELECT * FROM categories
              ORDER BY categoryID';
    $statement = $db->prepare($query);
    $statement->execute();
    $teams = $statement->fetchAll();
    $statement->closeCursor();
?>
<!DOCTYPE html>
<html>

<!-- the head section -->
<head>
    <title>NBA</title>
    <link rel="stylesheet" type="text/css" href="css/index.css">
    <link rel="shortcut icon" type="image/png" href="images/favicon.ico"/>

</head>

<!-- the body section -->

<body>
    <main id="standingListMain">

    <h1 id="addCategoryh1">Team Standings</h1>
    <table id="standingListTable">
        <tr>
            <th>Team</th>
            <th>&nbsp;</th>
        </tr>
        <?php foreach ($teams as $team) : ?>
        <tr>
            <td><?php echo $team['categoryID']; ?></td>
            <td>
              <?php echo $team['categoryName']; ?>
              <?php echo $team['img']; ?>
            </td>
        </tr>
        <?php endforeach; ?>
    </table>
    <br>

    </main>
    <!-- <footer id="standingListFooter">
        <p>&copy; <?php echo date("Y"); ?> NBA</p>
    </footer> -->
</body>
</html>

Basically, the user can add or remove a team from the team_list.php page and view it on the standings page

<?php
    require_once('../Model/database.php');

    // Get all categories
    $query = 'SELECT * FROM categories
              ORDER BY categoryID';
    $statement = $db->prepare($query);
    $statement->execute();
    $teams = $statement->fetchAll();
    $statement->closeCursor();
?>
<!DOCTYPE html>
<html>

<!-- the head section -->
<head>
    <title>NBA</title>
    <link rel="stylesheet" type="text/css" href="../css/index.css">
    <link rel="shortcut icon" type="image/png" href="images/favicon.ico"/>

</head>

<!-- the body section -->

<body>
    <main>
    <h1 id="addCategoryh1">Teams</h1>
    <table id="categoryListTable">
        <tr>
            <th>Name</th>
            <th>&nbsp;</th>
        </tr>
        <?php foreach ($teams as $team) : ?>
        <tr>
            <td><?php echo $team['categoryName']; ?></td>
            <td>
                <form action="delete_team.php" method="post"
                      id="delete_product_form">
                    <input type="hidden" name="team_id"
                           value="<?php echo $team['categoryID']; ?>">
                    <input id="deleteCategoryList" type="submit" value="Delete">
                </form>
            </td>
        </tr>
        <?php endforeach; ?>
    </table>
    <br>

    <h2 id="add_category_h2">Add Team</h2>
    <form action="add_team.php" method="post"
          id="add_category_form">

        <label>Name:</label>
        <input type="input" name="name">
        <input id="add_category_button" type="submit" value="Add">
    </form>
    <br>
    <p><a href="../index.php">View Team List</a></p>

    </main>
    <footer id="categoryListFooter">
        <p>&copy; <?php echo date("Y"); ?> NBA</p>
    </footer>
</body>
</html>

Code above is the team_list.php page and below is the code to connect to the database called the add_team.php

<?php
// Get the team data
$name = filter_input(INPUT_POST, 'name');

// Validate inputs
if ($name == null) {
    $error = "Invalid team data. Check all fields and try again.";
    include('../Error/error.php');
} else {
    require_once('../Model/database.php');

    // Add the product to the database
    $query = 'INSERT INTO categories (categoryName)
              VALUES (:team_name)';
    $statement = $db->prepare($query);
    $statement->bindValue(':team_name', $name);
    $statement->execute();
    $statement->closeCursor();

    // Display the team List page
    include('team_list.php');
}
?>

enter image description here

The image above shows the page where u can add or remove a team.

Deepak Rai
  • 2,163
  • 3
  • 21
  • 36
rwd
  • 149
  • 1
  • 11
  • 3
    You have to put an image into an HTML `` tag if you want the browser to display it properly – RiggsFolly Feb 24 '19 at 14:25
  • 2
    ALso `phpMyAdmin` is not storing this, MYSQL is. `phpMyAdmin` is a tool written in PHP that makes maintaining a MYSQL database a little easier – RiggsFolly Feb 24 '19 at 14:26
  • How can i accomplish that If u don't me asking – rwd Feb 24 '19 at 14:28
  • So in the `standing.php` page is where i put the image tags right? – rwd Feb 24 '19 at 14:29
  • 1
    Yes, instead of putting it in a `` you do ` ` for example – RiggsFolly Feb 24 '19 at 14:31
  • Have a look at this answer https://stackoverflow.com/a/35056782/2310830 – RiggsFolly Feb 24 '19 at 14:32
  • Ahh got it :D. One more question, is there a way on how I can insert the image in the `team_list.php` page instead of going to phpmyadmin to insert it? – rwd Feb 24 '19 at 14:36
  • Not sure I understand that question :) – RiggsFolly Feb 24 '19 at 14:37
  • Sorry for the misunderstanding, I added an image to show you the `team_list.php` page where u can add or remove a team. Back to the question I had, I wanted to be able to add the image there, so like when I add a new team there is also an option to add an image to it as well. – rwd Feb 24 '19 at 14:42

1 Answers1

0

For testing purposes

First you need to know if the Image really exist. Let's assume that in your database you have an image with category Id of 1. Thus create another file, eg "image.php".

(Please ensure that this code runs correctly. I have not tested it but it should work for you).

image.php

<?php 

 require_once('database.php');

    // Get all categories
    $query = "SELECT img FROM categories where categoryID=1";
    $statement = $db->prepare($query);
    $statement->execute();
    $num = $statement->rowCount();
 
if( $num ){

    $teams = $statement->fetchAll();
   
    // Ensure to specify header with content type,
    // you can do header("Content-type: image/jpg"); for jpg,
    // header("Content-type: image/gif"); for gif, etc.

    header("Content-type: image/png");
    
    //display the image file
    print $teams['img'];
    exit;
}else{
 //echo no image found with that Category Id.
   
}

?>

Then in your "standing.php", remove this code:

  <?php echo $team['img']; ?>

and replace it with:

<!– "1" is the categoryID id of the image to be displayed –>

<img src="image.php?id=1" />
halfer
  • 19,824
  • 17
  • 99
  • 186
Nancy Moore
  • 2,322
  • 2
  • 21
  • 38
  • So what I did was I replaced `` with `'; ?>` and it seems to be working just fine. – rwd Feb 24 '19 at 16:39
  • Thats okay. Its just that your question was confusing. I taught that your image was saved as blob in the database. Anyway good luck my dear.... – Nancy Moore Feb 24 '19 at 19:56