如何使 MySQL 表中的图像根据 id 进行更改


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

我有一个带有"id"的表,每次向表中添加新条目时,它都会自动递增。在同一张表中,我还存储图像。

无论 id 如何递增,图像始终相同。

我希望图像根据"id"进行更改。我如何实现这一点?在下面找到我的文件代码: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);

以及带有递增"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/>";

期待您的回复。任何帮助将不胜感激!

您尚未使用 ID 来限制 displayImage.php 文件中的结果。

改变

$sql = "SELECT image FROM flightSched";

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

这将使它工作,但这是一个示例,但请清理 GET 值,而不仅仅是直接在 sql 中使用它。