-2

based on my question above, currently, when there is no image at the database, it will display a broken image while if there is an image, it will display the image. Now how to display "No Image" when there's no image 'BLOB' at MySQL database.

I uses BLOB format at the MySQL Database. Below is the code:

<?php

$query = $conn->query("SELECT * FROM report LEFT JOIN users ON report.badgeid = users.badgeid LEFT JOIN team ON team.team_id = users.team_id WHERE team.team_id = '$team' ORDER BY report.report_date DESC LIMIT 10");
$query -> execute();
$results = $query -> fetchAll(PDO::FETCH_OBJ);
if($query->rowCount() == 0){

  echo "<table class = 'table-bordered' width ='100%'>";
  echo "<thead>";
    echo "<tr>";
        echo "<th>Date</th>
        <th>Task Name</th>
        <th>Before Task</th>
        <th>After Task</th>
        <th>OT From</th>
        <th>OT To</th>
        <th>Status</th>
        <th>Action</th>
      </tr>
    </thead>
    <tbody >
    <tr>
    <td colspan='8'>No report at this moment</td>
    </tr>
    </tbody>
    </table>";
}else{

      echo "<table class = 'table-bordered' width ='100%'>";
      echo "<thead>";
        echo "<tr>";
            echo "<th>Date</th>
            <th>Task Name</th>
            <th>Before Task</th>
            <th>After Task</th>
            <th>OT From</th>
            <th>OT To</th>
            <th>Status</th>
            <th>Action</th>
          </tr>
        </thead>
        <tbody >";

        $query = $conn->query("SELECT * FROM report LEFT JOIN users ON report.badgeid = users.badgeid LEFT JOIN team ON team.team_id = users.team_id WHERE team.team_id = '$team' ORDER BY report.report_date DESC LIMIT 10");
        while($row = $query->fetch(PDO::FETCH_ASSOC)){
        echo "<tr>";

          echo "<td rowspan='2'>". $row['report_date'] . "</td>";
          echo "<td rowspan='2'>". $row['task_name'] . "</td>";
          echo "<td align='center'><img src='data:image/jpeg;base64,".$before."'/></td>";
          echo "<td align='center'><img src='data:image/jpeg;base64,".$row['photo_after']."'/></td>";
          echo "<td rowspan='2' align='center'>". $row['ot_start']. "</td>";
          echo "<td rowspan='2' align='center'>".$row['ot_end']. "</strong></td>";
          echo "<td rowspan='2'><strong>". $row['report_status'] . "</strong></td>";
          echo "<td rowspan='2' align='center'>";
            echo "<a class='btn-view btn-primary btn-sm' href='../view_task/view_task.php?report_id=". $row['report_id'] ."' data-toggle='tooltip'>View</a>";
            echo "</td>";

        echo "</tr>";

        echo "<tr>";

          echo "<td align='center'>". $row['time_photo_before'] . "</td>";
          echo "<td align='center'>". $row['time_photo_after'] . "</td>";

        echo "</tr>";
        }

        echo "</tbody>";
      echo "</table><br>";  
    echo "</div>";
  echo "<div>";
}
?>            
Dharman
  • 30,962
  • 25
  • 85
  • 135
pundek
  • 3
  • 2
  • Use an `if` statement to check if `$row['photo_after']` contains anything? – brombeer Nov 25 '19 at 07:58
  • **Warning:** You are wide open to [SQL Injections](https://php.net/manual/en/security.database.sql-injection.php) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/5741187) – Dharman Nov 25 '19 at 09:31

2 Answers2

0

Just put a condition to check whether the image exist or not empty in the row.

if(!isset($row['photo_after']) || empty($row['photo_after'])) {
   echo "<td align='center'><img src='default.png'/></td>";
}
else 
{ 
   echo "<td align='center'><img src='data:image/jpeg;base64,".$row['photo_after']."'/></td>"; 
}
-1

Try this..., I hope this will work for you. Inside your while loop, I made a condition which checks there is something in your field or it's empty. if it's empty a variable store the text you want to print and if it's not empty it will show your image in variable $image and I echo the variable where you want to show your image.

<?php

$query = $conn->query("SELECT * FROM report LEFT JOIN users ON report.badgeid = users.badgeid LEFT JOIN team ON team.team_id = users.team_id WHERE team.team_id = '$team' ORDER BY report.report_date DESC LIMIT 10");
$query -> execute();
$results = $query -> fetchAll(PDO::FETCH_OBJ);
if($query->rowCount() == 0){

  echo "<table class = 'table-bordered' width ='100%'>";
  echo "<thead>";
    echo "<tr>";
        echo "<th>Date</th>
        <th>Task Name</th>
        <th>Before Task</th>
        <th>After Task</th>
        <th>OT From</th>
        <th>OT To</th>
        <th>Status</th>
        <th>Action</th>
      </tr>
    </thead>
    <tbody >
    <tr>
    <td colspan='8'>No report at this moment</td>
    </tr>
    </tbody>
    </table>";
}else{

      echo "<table class = 'table-bordered' width ='100%'>";
      echo "<thead>";
        echo "<tr>";
            echo "<th>Date</th>
            <th>Task Name</th>
            <th>Before Task</th>
            <th>After Task</th>
            <th>OT From</th>
            <th>OT To</th>
            <th>Status</th>
            <th>Action</th>
          </tr>
        </thead>
        <tbody >";

        $query = $conn->query("SELECT * FROM report LEFT JOIN users ON report.badgeid = users.badgeid LEFT JOIN team ON team.team_id = users.team_id WHERE team.team_id = '$team' ORDER BY report.report_date DESC LIMIT 10");
        while($row = $query->fetch(PDO::FETCH_ASSOC)){


      // Here is my code...



          if(empty($row['photo_after'])) {
               $image = "NO IMAGE FOUND..."; //anything you want to diaplay if there is no image
             }
             else{ $image = "<img src='data:image/jpeg;base64,".$row['photo_after']."'/>"; }





     // Check the $image variable in your table




        echo "<tr>";

          echo "<td rowspan='2'>". $row['report_date'] . "</td>";
          echo "<td rowspan='2'>". $row['task_name'] . "</td>";
          echo "<td align='center'><img src='data:image/jpeg;base64,".$before."'/></td>";
          echo "<td align='center'>".$image."</td>";   // $image store your data
          echo "<td rowspan='2' align='center'>". $row['ot_start']. "</td>";
          echo "<td rowspan='2' align='center'>".$row['ot_end']. "</strong></td>";
          echo "<td rowspan='2'><strong>". $row['report_status'] . "</strong></td>";
          echo "<td rowspan='2' align='center'>";
            echo "<a class='btn-view btn-primary btn-sm' href='../view_task/view_task.php?report_id=". $row['report_id'] ."' data-toggle='tooltip'>View</a>";
            echo "</td>";

        echo "</tr>";

        echo "<tr>";

          echo "<td align='center'>". $row['time_photo_before'] . "</td>";
          echo "<td align='center'>". $row['time_photo_after'] . "</td>";

        echo "</tr>";
        }

        echo "</tbody>";
      echo "</table><br>";  
    echo "</div>";
  echo "<div>";
}
?>           
Tarun Pandat
  • 109
  • 1
  • 9
  • While this code may solve the question, [including an explanation](//meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply. – Dharman Nov 25 '19 at 09:31