如何再次在PHP和PHP中用HTML编写HTML?

时间:2017-11-02 23:13:10

标签: php html

如何再次使用HTML在PHP和PHP中编写HTML?

出现以下错误消息:

  

解析错误:语法错误

<?php 
    echo"<div class='vod'>    

         "   /////// i need right php her
          $con = mysqli_connect ("localhost","root","","demo");

        $id = @addcslashes ($_REQUEST['id']);

        $image = mysqli_query ($con,"SELECT * FROM img WHERE id= '$id '")or  die(mysqli_error($con));
        $image = mysqli_fetch_assoc ($image);
        $image = $image['img'];


        header ("content-type: image/jpeg");
        echo $image ; "

    </div>";
?>

3 个答案:

答案 0 :(得分:0)

恐怕这是一个简单的拼写错误。您在第一个;之后错过了echo并错过了输出最后echo

的最后一行的div命令

在开发脚本时,最好确保已启用错误报告。

<?php 
    // turn on error reporting while developing a script
    ini_set('display_errors', 1); 
    ini_set('log_errors',1); 
    error_reporting(E_ALL); 
    mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

    echo"<div class='vod'>";
    // missing semi colon  ^

    $con = mysqli_connect ("localhost","root","","demo");
    $id = @addcslashes ($_REQUEST['id']);

    $image = mysqli_query($con,"SELECT * FROM img WHERE id= '$id'")
            or die(mysqli_error($con)); 
    $image = mysqli_fetch_assoc ($image); 
    $image = $image['img'];

    header ("content-type: image/jpeg"); 
    echo $image;

    echo "</div>";
//  ^^^^ the echo here was also missing
?>
  

注意:您还应该使用准备好的参数化查询来保护脚本免受SQL Injection Attacks

的影响

答案 1 :(得分:0)

这可能有效。

<?php

    // header has to appear before any html-tag; sure, that this is correct?
      header("content-type: image/jpeg");

    echo "<div class='vod'>";

      // connect to db and initialize parameters
        $con = mysqli_connect("localhost","root","","demo");
        $id = @addcslashes($_REQUEST['id']);

      // get image src
        $result = mysqli_query($con,"SELECT * FROM img WHERE id='.$id.'") or die(mysqli_error($con));
        $result = mysqli_fetch_assoc($result);
        $image = $result['img'];  // are you sure, that "img" is what you need? I think you need the path where the image is saved at

    echo "<img src='".$image."' alt='Text that shows up if Image cannot be displayed'></div>";
?>

答案 2 :(得分:-1)

实际上将html与php或sql混合是一个不太好的主意。 您应该尝试分离模板和开发逻辑。

例如,您可以在项目中创建文件夹“模板”,而不是在 html你可以阅读这个模板。对于sql也一样。

例如,好的做法是使用模板引擎 twig

最后,您可以查看框架方面。我想重温你的Symfony或Laravel。