-3

我在我的网站上有一个新闻提要。您可以在 post.php 网站上发布新闻,当您发布新闻时,新闻就会出现在这个 news.php 上。代码如下所示:

<!DOCTYPE html>
<html lang='en'>
<head>
<style type="text/css">
element {
color: black;
}


body {
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-attachment: fixed;
background-image: url(bgn1.png);
background-repeat: no-repeat;
background-position: center top;
 }
.news {
font-family: Rough_Typewriter;
font-size: 36px;
 }
 </style>

 <meta charset="UTF-8">
 <title>iWrite</title>
 <meta name="view" content="width-device-width, initial-scale=1.0">
 <meta name="description" content="">
 <meta name="keywords" content="">

 <link href="css/bootstrap.css" rel="stylesheet">
 </head>

 <h1 class="news">iWrite</h1>
 <p>
 <hr>
  <p>
 <p>
    <p>
    <p>
  <p>
   <p>
  <p>
  <p>
   <p>
  <a href='post.php'>Want to post a text?</a>
  <?php

   //connect

   mysql_connect("myserver","username","password") or die(mysql_error());
    mysql_select_db("databasename") or die(mysql_error());

    //query the database
    $getnews = mysql_query("SELECT * FROM news ORDER BY id DESC") or die(mysql_query());

     while ($row = mysql_fetch_assoc($getnews))
     {

//get data
$id = $row['id'];
$title = $row['title'];
$body = $row['body'];
$date = $row['date'];

echo "
<b>$title posted on $date</b><br>
";

echo nl2br($body);

echo "<hr>
";

    }



    ?>

但是当它在 index.html 网站上显示它时,它只是回显在 $date 上发布的 $title ......请帮助我!

4

2 回答 2

2

如果要嵌入要解析的 PHP 代码,则必须使用 PHP 文件扩展名。如果您需要使用 .html,请尝试mod_rewrite在您的 .htaccess 中使用。

于 2012-05-17T09:37:40.597 回答
0

你不应该在 PHP 中回显变量。让它们远离字符串

echo "<b>". $title ." posted on ". $date ."</b><br />"

另外:这是不好的做法,or die()因为这会杀死脚本。改为使用适当的错误处理。

于 2012-05-17T09:38:42.047 回答