我正在构建一个具有不同内容类型(图片、帖子、问题)的网站(php 和 mysql)。每个都需要一个评论部分。我不想在每一页中重复自己。为了更容易维护,我不知道是否应该创建一个注释函数或一个 comment.php 文件,将其包含在我想要的任何地方,但问题是输出将是高度可变的。我将非常感谢任何帮助。我对这个东西有点陌生
最佳答案
首先我需要知道你的数据库是如何构造的,假设你有带有 id、comment_title、content、post_id(评论了哪个帖子)的表调用评论
Note: if you need to comment for different section which stored in different tables this won't work properly. Your id's has to be unique. For example if you used 1 as id in post table you can't use it in the question table as primary key.
Another way create different comment table for different section, which is a repetition, so figure out a way.
正如您所说,您可以使用函数来做到这一点,
如果您使用一张表,则不需要 $table
参数,对于多个表,您可以使用它
function storeComment($title,$content,$id,$table="defult_table_name"){
$stmt = $conn->prepare("INSERT INTO $table (title,content,id) VALUES (?, ?,?)");//No of question marks are equal to columns you have mentioed
$stmt->bind_param("ssi",$empName, $title,$id);
// i - integer
// d - double
// s - string
// b - BLOB
$stmt->execute();
$stmt->close();
}
希望有帮助:)
关于php - 如何跨不同页面构建可重用且可维护的评论,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43929416/