0

The aim is for a customer to upload their advert to the database, that triggers and email for approval. Once approved ad will display in location between the set dates.

I have written the code correctly to display by date and approval from an admin area. I just cannot get the image to display.

The code below details the process so far.

    <?php 
include('mysql_connect.php');{

  $location='1'; 
  }

 $resultSet = $mysqli->query("SELECT * FROM adverts WHERE adloc = '$location' AND approval ='Y' ");

if($resultSet->num_rows > 0){
    while($rows = $resultSet->fetch_assoc())
    {
        $id = $rows ['id'];
        $start = $rows ['start'];
        $end = $rows ['enddate'];
        $business = $rows ['business'];
        $email = $rows ['email'];
        $tel = $rows ['tel'];
        $web = $rows ['web'];
        $advert = $rows ['image'];

    $Date = date('Y-m-d');
    $Date=date('Y-m-d', strtotime($Date));; 
    $DateBegin = date('Y-m-d', strtotime("$start"));
    $DateEnd = date('Y-m-d', strtotime("$end"));
}

if (($Date > $DateBegin) && ($Date < $DateEnd))
    {
      echo "ADVERT";
      echo '<img src="getad.php?id=$id">';

    }
    else
    {
      echo "FILLER IMAGE";  
    }

}
?>

The code for the getad.php file is as follows

if(isset($_GET['id']))
{

$id = mysqli_real_escape_string($_GET['id']);
$query = mysqli_query("SELECT * FROM adverts WHERE id= '$id'");
while ($row = mysqli_fetch_assoc($query))
{
    $image = $row['image'];
}
header("content-type: image/jpeg");
echo $image;
}
else
{
    echo "Error!";
}
?>

I am sure I can't be far off. The image is a JPEG LONGBLOB in the database.

Thank you.

James Parsons
  • 895
  • 5
  • 18
  • 36
  • 2
    Why not just upload image to a directory and insert file path to database? – Abu Nooh Dec 10 '15 at 23:01
  • Why is the date not part of the sql query? Make it simple. MySQL has date/time functions. – developerwjk Dec 10 '15 at 23:03
  • I have considered doing that and that is my usual preferred choice hence the rustiness using this method. I preferred this option for organisational reasons. We will be working with a lot of data and would be nice to keep everything in one location . – James Parsons Dec 10 '15 at 23:06
  • You probably need `base64_encode($row['image'])` http://stackoverflow.com/questions/13225726/i-need-my-php-page-to-show-my-blob-image-from-mysql-database – Dan Dec 10 '15 at 23:09
  • this is failing you `mysqli_real_escape_string($_GET['id'])` and `$query = mysqli_query("SELECT * FROM adverts WHERE id= '$id'");` – Funk Forty Niner Dec 10 '15 at 23:10

1 Answers1

1

Firstly, this is failing you:

$id = mysqli_real_escape_string($_GET['id']);
$query = mysqli_query("SELECT * FROM adverts WHERE id= '$id'");

Both of those functions require a database connection be passed and as the first parameter:

$id = mysqli_real_escape_string($mysqli, $_GET['id']);
$query = mysqli_query($mysqli, "SELECT * FROM adverts WHERE id= '$id'");

References:

However, may need to use base64_decode:

header("content-type: image/jpeg");
echo base64_decode($image);

or

echo '<img src="data:image/gif;base64,' . $image . '" />';

Here is an example borrowed from https://stackoverflow.com/a/20564797/

$db = mysqli_connect("localhost","root","","DbName"); //keep your db name
$sql = "SELECT * FROM products WHERE id = $id";
$sth = $db->query($sql);
$result=mysqli_fetch_array($sth);
echo '<img src="data:image/jpeg;base64,'.base64_encode( $result['image'] ).'"/>';

For debugging purposes:

Add error reporting to the top of your file(s) which will help find errors.

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// rest of your code

Sidenote: Displaying errors should only be done in staging, and never production.

  • As well as or die(mysqli_error($mysqli)) to mysqli_query().
Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • Thanks @Fred -ii- `code`if (($Date > $DateBegin) && ($Date < $DateEnd)) { echo "ADVERT"; $sql = "SELECT * FROM adverts WHERE id = $id"; $sth = $db->query($sql); $result=mysqli_fetch_array($sth); echo ''; } else { echo "FILLER IMAGE"; }`code` I have changed to this as above. It still echo the ADVERT however doesnt even display a broken image like it did before. – James Parsons Dec 10 '15 at 23:43
  • @JamesParsons you're welcome. Can you add error reporting and error checking to see if anything comes of it? I made an edit to my answer with that, and unsure if you did see it. – Funk Forty Niner Dec 10 '15 at 23:46
  • Sorry quite new here havent quite grasped how to post everything! – James Parsons Dec 10 '15 at 23:46
  • @JamesParsons you may also have to use `mysqli_fetch_array()` instead of `mysqli_fetch_assoc()` but I can't be 100% sure about that. – Funk Forty Niner Dec 10 '15 at 23:47
  • Notice: Constant DB_USERNAME already defined in /Applications/MAMP/htdocs/Spire/mysql_connect.php on line 3 Notice: Constant DB_PASSWORD already defined in /Applications/MAMP/htdocs/Spire/mysql_connect.php on line 4 Notice: Constant DB_HOST already defined in /Applications/MAMP/htdocs/Spire/mysql_connect.php on line 5 Notice: Constant DB_DATABASE already defined in /Applications/MAMP/htdocs/Spire/mysql_connect.php on line 6 – James Parsons Dec 10 '15 at 23:49
  • Notice: Undefined index: email in /Applications/MAMP/htdocs/Spire/date1.php on line 22 Notice: Undefined index: tel in /Applications/MAMP/htdocs/Spire/date1.php on line 23 Notice: Undefined index: web in /Applications/MAMP/htdocs/Spire/date1.php on line 24 ADVERT Notice: Undefined variable: db in /Applications/MAMP/htdocs/Spire/date1.php on line 40 Fatal error: Call to a member function query() on null in /Applications/MAMP/htdocs/Spire/date1.php on line 40 – James Parsons Dec 10 '15 at 23:49
  • @JamesParsons something else is failing you then. Maybe you're doing the same include/require somewhere. and the additional notices doesn't correspond with what you posted. – Funk Forty Niner Dec 10 '15 at 23:50
  • Sorry @Fred -ii- here is the error reporting just for date1.php Notice: Undefined index: email in /Applications/MAMP/htdocs/spire/date1.php on line 22 Notice: Undefined index: tel in /Applications/MAMP/htdocs/spire/date1.php on line 23 Notice: Undefined index: web in /Applications/MAMP/htdocs/spire/date1.php on line 24 ADVERT Notice: Undefined variable: db in /Applications/MAMP/htdocs/spire/date1.php on line 40 Fatal error: Call to a member function query() on null in /Applications/MAMP/htdocs/spire/date1.php on line 40 – James Parsons Dec 10 '15 at 23:54