简体   繁体   中英

How do I make the image in the MySQL table alter based on the id

I have a table with an "id" that automatically increment each time a new entry is added to the table. In the same table, I also store images.

Regardless of the id incrementing the image is always the same.

I would like the images to change based on the "id". How do I achieve this? Find below my code to file: displayImage.php:

$link = mysql_connect("localhost", "root", "") or die("Could not connect: " . mysql_error());

// select our database
mysql_select_db("flightSched") or die(mysql_error());

// get the image from the db
$sql = "SELECT image FROM flightSched";

// the result of the query
$result = mysql_query("$sql") or die("Invalid query: " . mysql_error());

// set the header for the image
header("Content-type: image/jpeg");
echo mysql_result($result,1);

// close the db link
mysql_close($link);

And the code with the incrementing "id"

$result = mysql_query("SELECT id, flightNo, airline, origin, arrivalTime, status 
                       FROM flightSched ") 
    or die(mysql_error());  

    $variable= 'id=basicBoard';
    //echo $variable;


    echo "<table border='1' id='customers'>";
    echo "<tr> <th></th> <th>Flight No</th> <th>Airline</th> <th>Origin</th> <th>Arrival</th> <th>Status</th> ";
    // keeps getting the next row until there are no more to get
    while($row = mysql_fetch_array( $result )) {
        // Print out the contents of each row into a table
        echo "<tr " . $variable . "><td>"; 

        echo '<img src="displayImage.php?id=' . $row['id'] . ' " width="40" height="40" >';
        echo "</td><td>"; 
        echo $row['flightNo'];
        echo "</td><td>"; 
        echo $row['airline'];
        echo "</td><td>"; 
        echo $row['origin'] . $row['id'];
        echo "</td><td>"; 
        echo $row['arrivalTime'];
        echo "</td><td>"; 
        echo $row['status'];
        echo "</td></tr>"; 

        if ($variable == 'id=basicBoard')
            {
            $variable = 'class=alt';
            //echo $variable;
            } 
        elseif ($variable == 'class=alt')
            {
            $variable = 'id=basicBoard';
            //echo $variable;
            } 
        } 

        echo "</table> <br/> <br/>";

Looking forward to your reply. Any help is greatly appreciated!

You haven't used the ID to limit the results in your displayImage.php file.

Change

$sql = "SELECT image FROM flightSched";

to

$sql = "SELECT image FROM flightSched WHERE id = '$_GET[id]'";

This will get it working but is an example, but please sanitize the GET value instead of just using it directly in the sql.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM