有没有一种有效的方法将包含代码片段的博客条目插入MySQL数据库?
我正在构建一个博客引擎用于练习。我想这个设计应该相当简单......这就是我希望它如何工作的方式:
- 我有index.html、一张CSS表、一些javascript和一个我命名为pullContent.php的PHP文件。
- Index.html 是您查看博客文章的主页。 pullContent.php 与包含我的帖子的 MySQL 数据库表连接。
- 在 index.html 的正文中,我有一个 id="blog_entries" 的 div,其中包含博客文章。在该 div 中,我将有一堆带有 class="post" 的 div,它们将构成我的博客文章集。每篇文章都将从 MySQL 表中的一行中提取,显示在 index.html 上,然后通过 CSS 设置样式。这个肮脏的工作是在 pullContent.php 中完成的,
正如您所看到的,我必须将我的每个博客条目插入到数据库表中,并且插入的内容应该采用 HTML 标记的形式(因为帖子的某些部分可能需要使用 CSS 进行样式设置,因此需要一些类/id 标识符)。现在如果我想在我的博客上放一些代码片段,是不是会很麻烦?有没有使用我没有提到的其他技术的解决方法?
任何意见/建议将不胜感激。谢谢。
I am building a blog engine for practice. The design is supposed to be fairly simple I guess..Here is how i want it to work:
- I have index.html, a CSS sheet, some javascript, and one PHP file i named pullContent.php.
- Index.html is the main page where you see the blog posts. pullContent.php interfaces with MySQL database table containing my posts.
- inside the body of index.html i have a div with id="blog_entries" containing the blog posts. Inside that div i will have a bunch of divs with classes="post" that will make up the set of my blog posts. Each post will be pulled from a row on MySQL table, displayed on index.html and then after that be styled by CSS. This dirty work is done in pullContent.php
As you can see, I will have to insert each of my blog entry into the database table and the content inserted should be in the form of HTML markup (since some parts of the post probably needs to be styled using CSS and thus needs some class/id identifier). Now In the case where I want to put some code snippet on my blog, isn't it going to be such a hassle? is there any workaround using some other technology i did not mention?
any comments/suggestions would be appreciated. thx.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,您应该确保在将数据传递到数据库时正确清理数据。
当您想要显示代码时,只需将所有内容放入预块中(就像您在SO上所做的那样)
这样您的帖子将显示所有空格,换行符,因为它被传递到原来你。
另一方面,您仍然必须确保转义某些字符:例如。而不是 <、> 或 &,您应该输入 <、& ;gt; 和 &
如果您希望代码看起来不错,请查看 google-code-prettify (一些额外的 js 和 CSS,你的代码看起来棒极了!)
First of all you should make sure to properly sanitise your data when passing it to the database.
When you want to display code, just put everything in a pre-block (just like you would here on SO)
This way your post will be displayed with all the spaces, newlines, as it was passed to you originally.
On the other hand you still have to make sure to escape certain characters: eg. instead of <, > or & you should put <, > and &
If you want your code to look good, then check out google-code-prettify (some extra js & CSS and your code will look aweseome!)