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>