使用MySQL和PHP生成HTML网页时出错

时间:2013-12-09 23:45:37

标签: php html mysql wordpress mysqli

对于一个类项目,我决定创建一些类似于简化的Wordpress的东西,它根据用户的选择生成HTML布局。

现在一切都很完美,除了html中的属性搞砸了很多“\”的插入。 (我注意到它接近引号所以我的理论是当MYSQLi正在编写运行查询时,它会添加斜杠来保护查询)

无论如何这里是相关部分的代码

if ($stmt = $db_link->prepare("INSERT INTO pages (pageid, pagetitle, styleid,pagetypeid, importheader, importfooter, javascript, multiplepages, bodyhtml, additionalnotes)

VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")) {

// Bind the variables to the parameter as strings. 
$stmt->bind_param("isiissssss",$pageid,$pagetitle,$stylesheet,$pagetype,$header,$footer,$javascript,$multiplepages,$html,$notes);
// Execute the query
$stmt->execute();
// Close the prepared statement.
$stmt->close();
}

HTML应该是这样的:

<div class='main'><div class='nav'><br><a href='#' class='myButton'>hey</a><br><a href='#' class='myButton'>hey</a><br><a href='#' class='myButton'>hey</a><br><a href='#' class='myButton'>hey</a></div><div class='frame'> 

<p id='paragraph'>hopefully the last test</p></div></div></div>
</div>

这就是它的样子:

<div class=\'main\'><div class=\'nav\'><br><a href=\'#\' class=\'myButton\'>hey</a><br><a href=\'#\' class=\'myButton\'>hey</a><br><a href=\'#\' class=\'myButton\'>hey</a><br><a href=\'#\' class=\'myButton\'>hey</a></div><div class=\'frame\'> \r\n\r\n<p id=\'paragraph\'>hopefully the last test</p></div></div></div>\r\n</div>\r\n</body>

我知道\ n引用了返回字符,所以我猜是类似于''引用'字符。问题是如何阻止这种情况发生?同样为了记录,我知道问题肯定是通过这个程序输入查询,因为我用phpMyAdmin手动插入的HTML工作正常。

如果有人可以帮助我,我真的很感激。非常感谢你!

编辑:我明白了! Sort've。无论如何如何解决它。我正在使用

  

mysqli_real_escape_string();

在我尝试使用

删除“\ n”和“\ r”之前的功能
  

str_replace函数();

  

stripslashes()函数;

所以现在很明显为什么那不起作用。它仍然无法解决为什么首先生成斜杠的问题,但至少现在我有一个解决方案可以使代码工作。我希望如果其他人有这个问题,他们会看到这个!

2 个答案:

答案 0 :(得分:0)

进行了一些挖掘,我发现这是HTML存储问题的常见解决方案。试试这个:HTML Purifier这个库可以让你安全地存储HTML和其他功能。

您还应该使用htmlspecialchars()这将转换任何“错误”的字符并将其转换为HTML等效字符。 IE:'被转换为“&amp;#039”并将其存储在数据库中。然后,您可以使用htmlspecialchars_decode()将其还原。

答案 1 :(得分:0)

我的猜测是,由于某些原因,你有激活的魔术引号。它会解释你的问题......

返回什么:get_magic_quotes_gpc()?如果是,那么您就有了解决方案。如果不对,我错了。自PHP5.4起,该功能不再存在。

您可以尝试在php.ini配置中禁用它吗?