0

新しいエントリがテーブルに追加されるたびに自動的に増加する「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/>";

お返事を楽しみにしています。どんな助けでも大歓迎です!

4

1 に答える 1

0

displayImage.php ファイルで結果を制限するために ID を使用していません。

変化する

$sql = "SELECT image FROM flightSched";

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

これで機能しますが、これは一例ですが、SQL で直接使用するのではなく、GET 値をサニタイズしてください。

于 2013-10-10T09:55:16.093 に答える