简体   繁体   English

在Php页面中显示数据库中的图像

[英]Display image from database in Php page

I try to make online quiz with images questions, and i need your help/advice. 我尝试通过图像问题进行在线测验,并且需要您的帮助/建议。 My images is stored on database where have an id "image". 我的图像存储在ID为“ image”的数据库中。 My upload works fine, image is stored on database...but i can't show image in questions. 我的上传效果很好,图像存储在数据库中...但是我无法显示图像中的问题。

I succeeded to show icon for image and name, but not image. 我成功显示了用于图像和名称的图标,但未显示图像。 Image is stored in database with base64_encode, and here is a structure from my database. 图像使用base64_encode存储在数据库中,这是我数据库中的结构。

数据库映像

And the result from my code is here: http://imageshack.com/a/img922/6874/U4hkbj.jpg 我的代码的结果在这里: http : //imageshack.com/a/img922/6874/U4hkbj.jpg

I put name there to verify my connection to database, it's not necessary from final code. 我在此处输入名称以验证与数据库的连接,从最终代码中不必这样做。

And here is code for display images: 这是显示图像的代码:

 require_once("scripts/connect_db.php");
    $res=mysqli_query($connection, "SELECT * FROM questions WHERE id='$question'");

            echo "<table>";
            while($row=mysqli_fetch_array($res)) {

                echo "<tr>";
                echo "<td>";?> <img src= data:image/png;base64 ' . $row['image']; .  '  > <?php echo "</td>";
                echo "<td>" ; echo $row["name"]; echo" </td>";

                echo "</tr>";

            }
            echo "</table>";

And that's my code from show questions with image: 这是我的显示问题与图像的代码:

   <?php 
   session_start();
   require_once("scripts/connect_db.php");
   $arrCount = "";
    if(isset($_GET['question'])){
$question = preg_replace('/[^0-9]/', "", $_GET['question']);
$output = "";
$answers = "";
$q = "";
$sql = mysqli_query($connection, "SELECT id FROM questions");
$numQuestions = mysqli_num_rows($sql);
if(!isset($_SESSION['answer_array']) || $_SESSION['answer_array'] < 1){
    $currQuestion = "1";
}else{
    $arrCount = count($_SESSION['answer_array']);
}
if($arrCount > $numQuestions){
    unset($_SESSION['answer_array']);
    header("location: index.php");
    exit();
}
if($arrCount >= $numQuestions){
    echo 'finished|<p>There are no more questions. Please enter your first and last name and click next</p>
            <form action="userAnswers.php" method="post">
            <input type="hidden" name="complete" value="true">
            <input type="text" name="username">
            <input type="submit" value="Finish">
            </form>';
    exit();
}


    require_once("scripts/connect_db.php");
    $res=mysqli_query($connection, "SELECT * FROM questions WHERE id='$question'");

            echo "<table>";
            while($row=mysqli_fetch_array($res)) {

                echo "<tr>";
                echo "<td>";?> <img src= data:image/png;base64 ' . $row['image']; .  '  > <?php echo "</td>";
                echo "<td>" ; echo $row["name"]; echo" </td>";

                echo "</tr>";

            }
            echo "</table>";




    $singleSQL = mysqli_query($connection, "SELECT * FROM questions WHERE id='$question' LIMIT 1");
                    while($row = mysqli_fetch_array($singleSQL)){
        $id = $row['id'];
        $thisQuestion = $row['question'];
        $type = $row['type'];
        $question_id = $row['question_id'];
        $q = '<h2>'.$thisQuestion.'</h2>';
        $sql2 = mysqli_query($connection, "SELECT * FROM answers WHERE question_id='$question' ORDER BY rand()");
        while($row2 = mysqli_fetch_array($sql2)){
            $answer = $row2['answer'];
            $correct = $row2['correct'];
            $answers .= '<label style="cursor:pointer;"><input type="radio" name="rads" value="'.$correct.'">'.$answer.'</label> 
            <input type="hidden" id="qid" value="'.$id.'" name="qid"><br /><br />
            ';

        }
        $output = ''.$q.','.$answers.',<span id="btnSpan"><button onclick="post_answer()">Submit</button></span>';
        echo $output;
       }
    }
?>

I'm new in php and that's my first project, i really need your help for solve my problem. 我是php的新手,这是我的第一个项目,我确实需要您的帮助来解决我的问题。

Thank you so much for help and for interest! 非常感谢您的帮助和关注!

EDIT: The images is stored on database with this code: 编辑:图像使用以下代码存储在数据库中:

if (isset($_FILES['image'])) {
$name = $_FILES['image']['tmp_name'];
$image = base64_encode(
    file_get_contents(
        $_FILES['image']['tmp_name']
    )
);

You forgot wrapping quotes, comma and you don't echo $row['image'] . 您忘记了用引号引起来,逗号,并且您没有回显$row['image'] Change: 更改:

<img src= data:image/png;base64 ' . $row['image']; .  '  >

to: 至:

<img src="data:image/png;base64,<?= $row['image'] ?>"  >

or (coherently with your global syntax) to: 或(与您的全局语法一致)以:

echo '<img src="data:image/png;base64,' . $row['image'] . '">';

Edit: 编辑:

I see the pastebin in your last comment. 我在您的最后一条评论中看到了pastebin。 First of all, I think it is not the HTML source, but it is the source from page inspector (after DOM rendering). 首先,我认为它不是HTML源,而是页面检查器的源(在DOM渲染之后)。 If you look at the code, you can see that there is not any <img> tag. 如果查看代码,可以看到没有任何<img>标签。 Your rendered code is: 您呈现的代码是:

<div id="answers">/9j/(...)/2Q=="&gt;<h2>
                  └────────────┘
                   base64 image

The base64 encoded image is correct (as you can see here ), but it is not wrapped by correct <img> tag: base64编码的图像是正确的(如您在此处看到的),但是它没有被正确的<img>标签包裹:

<img src="data:image/png;base64,/9j/(...)/2Q==">
                                └────────────┘
                                 base64 image

Seeing your code: 看到您的代码:

echo "<table>";
while($row=mysqli_fetch_array($res)) {
    echo '<img src="data:image/png;base64,' . $row['image'] . '">';
}
echo "</table>";

Even if <table><img></table> is not valid HTML, I think that the problem is not in your HTML, but it is in your javascript, probably inside the getQuestion() function: test it deeply to retrieve corrected code. 即使<table><img></table>不是有效的HTML,我也认为问题不在您的HTML中,而是在您的javascript中,可能在getQuestion()函数内部:对其进行深入测试以检索更正的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM