首页 > 解决方案 > Represent image form database

问题描述

I’d like to represent an image stored in a database every time I press a button. The column name is “solution_path”. In my code I can represent the image outside the button, but I would like it to appear when I press the button. Part of the code is shown below. I know how to represent images taken from the database, but in this case, I do not know why it’s not working. Can you help me? I am already connected to the database and to the table I need.

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "project";

$conn = new mysqli($servername, $username, $password, $dbname);
/*echo*/ $id=$_GET['id'];
$sql = "SELECT * FROM exercises where exercise_id='$id'";
$result = $conn->query($sql); /*Check connection*/
?>

<div id="centered_B" class="header">

<?php
while($id = $result->fetch_assoc())
    echo '<h1>' . $id["exercise_id"]. ". " . $id["title"] . '</h1>' . "<br>" . '<p>' . $id["text"] . '</p> <img width="603" height="auto" src="' . $id["image_path"] . '"><br><br>


    <input type="radio" name="choice" value= "1" /><img width="550" height="auto" src="' . $id["image_path_A"] . '"/>
    <input type="radio" name="choice" value= "2" /><img width="550" height="auto" src="' . $id["image_path_B"] . '"><br>
    <input type="radio" name="choice" value= "3" /><img width="550" height="auto" src="' . $id["image_path_C"] . '"><br>';


/*var_dump($id)*/
?>


 <button onclick="solutionFunction()" class="button_Solution" >Solution</button>



<div id="solution" style="display: none;">
    This is the solution.
    <img width="500" height="auto" src="<?php echo $id["solution_path"]; ?>" >

</div>



<script>
    function solutionFunction() {
        var x = document.getElementById("solution");
        if (x.style.display === "none") {
            x.style.display = "block";
        } else {
            x.style.display = "none";
        }
    }
</script>

标签: phphtmldatabase

解决方案


Giving my comment as an answer for you to close your question with ;)

If you are only dealing with one result row for that specific page build, you should get rid of the while loop and simply assign the one result row to your $id variable, to use throughout the page.

So change this one line:

while($id = $result->fetch_assoc())

To just:

$id = $result->fetch_assoc();

Now $id will persist through the page generation, and your lower echo of $id["solution_path"] should present you with the solution image url string you need.


The reason your original code was not working, is a while has the specific behavior of 'wiping out' the assigned variable on the last loop iteration (thus causing the condition to turn 'false' and end the loop).

Also, since you did not have curly braces, that while was only controlling the immediate following echo line. This then meant the further echo you had for the $id["solution_path"] variable, would have been echoing a null result. If you had php error reporting fully enabled, you may had seen a warning about undefined index too.

I hope that helps you out!


推荐阅读