我正在从头开始构建个人博客,并且在从 MySQL 输出文本正文时无法正确显示链接。
例如; 我在博客文章中嵌入了大约 200 个单词的链接,当我从数据库中调用博客文章时,链接无法正确显示。我尝试过使用stipslashes()
,htmlentities()
但这些都不起作用。
这是我将博客文章保存到数据库的代码:
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
die($problem);
}
return $data;
}
if(isset($_POST['addBlog'])) { //form submitted?
// get form values, escape them and apply the check_input function
$title = $link->real_escape_string($_POST['title']);
$category = $link->real_escape_string(check_input($_POST['category'], "You must choose a category."));
$content = $link->real_escape_string(check_input($_POST['blogContent'], "You can't publish a blog with no blog... dumbass."));
$date = $link->real_escape_string(check_input($_POST['pub_date'], "What day is it foo?"));
mysqli_connect($db_host, $db_user, $db_pass) OR DIE (mysqli_error());
// select the db
mysqli_select_db ($link, $db_name) OR DIE ("Unable to select db".mysqli_error($db_name));
// our sql query
$sql = "INSERT INTO pub_blogs (title, date, category, content) VALUES ('$title', '$date', '$category', '$content');";
//save the blog
mysqli_query($link, $sql) or die("Error in Query: " . mysqli_error($link));
if (!mysqli_error($link))
{
print "<p> Blog Successfully Published! </p>";
}
}
这是我显示博客文章的代码:这就是问题所在
// Grab the data
$result = mysqli_query($link, "SELECT * FROM pub_blogs") or die ("Could not access DB: " . mysqli_error($link));
while ($row = mysqli_fetch_assoc($result))
{
$id = $link->real_escape_string($row['id']);
$title = $link->real_escape_string($row['title']);
$date = $link->real_escape_string($row['date']);
$category = $link->real_escape_string($row['category']);
$content = $link->real_escape_string($row['content']);
$id = stripslashes($id);
$title = stripslashes($title);
$date = stripslashes($date);
$category = stripslashes($category);
$content = stripslashes($content);
}
echo "<div class='blog_entry_container'>";
echo "<span class='entry_date'><a href='#'>" .$date. "</a> - </span><span class='blog_title'><a class='blogTitleLink' href='#'>" .$title. "</a></span>";
echo "<p>" .$content. "</p>";
echo "</div>";
问题在于$content
变量