0

I have to make a booking system for a school project im really close but then this problem occured, which i cant solve.

So i hoped you guys could help me out :)

This is the PHP code getting data from the database (formulier.php):

<div id="room1" style="margin-left: 10%;"><br><br>
    <img src=”source.php?id=2” width="150" height="150" />&nbsp;
    <?php
    $query = "SELECT * FROM dt_tb WHERE dt BETWEEN '2004-01-01' AND '2005-01-25'";
    /* SELECT * FROM dt_tb WHERE dt BETWEEN BETWEEN '$dt1' AND '$dt2' */
    $result = mysqli_query($connect,$query);

    if (mysqli_num_rows($result)) {
        while($row = $result->fetch_assoc()) {
            echo "HERE COMES THE DESCRIPTION &nbsp;";
            echo "&nbsp; check-in: ". $row["dt"] ." | check-out: ". $row["dt2"] ." ";
        }
    }
    else {
        echo "An error occured, Please choose another date.";
    }
    $connect->close();
    ?>
    <br><br><br>
    <form method="post" action="#">
        <input required type="submit" id="Reserve" name="Reserve" value="Book Now"  style="margin-left: 0.4%;">
    </form>
    <hr style="width: 65%;">
</div>

<div id="room1" style="margin-left: 10%;"><br><br>
    <img src=”source2.php?id=1” width="150" height="150" />&nbsp;
    <?php
    $query2 = "SELECT * FROM dt_tb WHERE dt BETWEEN '2000-01-01' AND '2100-01-25'";
    /* SELECT * FROM dt_tb WHERE dt BETWEEN BETWEEN '$dt1' AND '$dt2' */
    $result2 = mysqli_query($connect,$query2);
    if (mysqli_num_rows($result2)) {
        while($row2 = $result2->fetch_assoc()) {
            echo "HERE COMES THE DESCRIPTION &nbsp;";
            echo "&nbsp; check-in: ". $row2["dt"] ." | check-out: ". $row2["dt2"] ." ";
        }
    }
    else {
        echo "An error occured, Please choose another date.";
    }
    $connect->close();
    ?>
    <br><br><br>
    <form method="post" action="#">
        <input required type="submit" id="Reserve" name="Reserve" value="Book Now"  style="margin-left: 0.4%;">
    </form>
    <hr style="width: 65%;">
</div>

This is the PHP code for getting the image in the database (source.php):

$query = "select * from images WHERE id = 2";
$stmt = $connect->prepare( $query );
$stmt->bindParam(2, $_GET['id']);
$stmt->execute();
$num = $stmt->rowCount();
if( $num ){
    $row = $stmt->fetch(PDO::FETCH_ASSOC);
    header("Content-type: image/png");
    print $row['data'];
    exit;
}else{
}

This is for the second image (source2.php):

$query = "select * from images WHERE id = 1";
$stmt = $connect->prepare( $query );
$stmt->bindParam(1, $_GET['id']);
$stmt->execute();
$num = $stmt->rowCount();
if( $num ){
    $row = $stmt->fetch(PDO::FETCH_ASSOC);
    header("Content-type: image/png");
    print $row['data'];
    exit;
}else{
}

These are the databases:

TABLE IMAGES
========================================
id | name          | data
========================================
1  | sample image  |[BLOB - 31,9 KiB]
2  | sample image2 |[BLOB - 370,8 KiB]

TABLE DT_TB
========================================
id | dt          | dt2
========================================
1  | 2004-10-26  | 2005-01-25

TABLE DT_TB10
========================================
id | dt          | dt2
========================================
1  | 2006-01-01  | 2007-01-25

This is the outcome: https://gyazo.com/e3263b030bd2a367e86d39db262551ba

So my problem is basically that it wont show the images from the database and that the second "booking" will show an error and not the same data as the first "booking".

I would appreciate it very much if someone could help me out with this problem.

Thanks in advance.

Mickaël Leger
  • 3,426
  • 2
  • 17
  • 36
Derman
  • 13
  • 1
  • 1
  • 7
  • I'm a first year student, so have mercy with my coding skills :) – Derman Jun 19 '18 at 07:57
  • How did you stored your pic ? Why not store the url pic. Take a look at https://stackoverflow.com/questions/20556773/php-display-image-blob-from-mysql for blob stockage – executable Jun 19 '18 at 08:02
  • 1
    Shouldn't the Image query be `$query = "select * from images WHERE id = ?";` instead of a hard-coded `1`? I will be surprised if it works the way you do it. Look at the Image with a browser and then view source and add error handling to the Images code. – Adder Jun 19 '18 at 08:03
  • You've literally set the source of the image to "source.php?id=2" Save the image as a URL not a blob, because that is what the src attribute on an image tag expects to get. – Rick Calder Jun 19 '18 at 08:47

1 Answers1

0

Guys you can use this as an example but i've fixed my problem :).

You never close your $connection mid way.

$connect->close();

was used 2 times but its only used when you actually want to close your connection with your database.

As for the images, @Rick Calder has a good explanation about it.

thanks for the help.

Derman
  • 13
  • 1
  • 1
  • 7