duancong7573 2018-08-06 14:18
浏览 34
已采纳

表示图像表单数据库

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>
  • 写回答

1条回答 默认 最新

  • doutuan4361 2018-08-06 15:23
    关注

    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!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持