我正在尝试为博客文章创建一个“赞”按钮。我面临的问题是我无法将我的“喜欢”按钮与每篇博客文章链接起来。目前,所有类似按钮共享相同的类和名称属性。我尝试使用以下内容:
<?php
include_once('connectserver.php');
$query_blog = mysql_query("SELECT `category_x`,`sub_category_x`,`specified_sub_category_x`,`tag_1`,`tag_2`,`tag_3`,`title`,`contents`,`date_posted` FROM `posts` ORDER BY `date_posted` DESC");
while($get_rows = mysql_fetch_assoc($query_blog)) {
$get_title = $get_rows['title'];
$get_category = $get_rows['category_x'];
$get_sub_category = $get_rows['sub_category_x'];
$get_specified_sub_category = $get_rows['specified_sub_category_x'];
$get_tag1 = $get_rows['tag_1'];
$get_tag2 = $get_rows['tag_2'];
$get_tag3 = $get_rows['tag_3'];
$get_contents = $get_rows['contents'];
$get_date_posted = $get_rows['date_posted'];
$new_date = date('dS F Y', strtotime($get_date_posted));
echo "<p class='blog-heading' align='left'><font face='Narkisim' size='5' color='#3E537C'> <strong>".$get_title."</strong></font></p>";
echo "<br><pre class='blog-underheading'><font face='David' size='2' color='black'><font face='David' size='3' color='#0040A1'>".$new_date."</font> BY <a class='blog-link1' href='home.php'></a> | <a class='blog-link1' href='eere'>UpVote</a></font></pre><br>";
echo "<br><p class='blog-content' align='justify'><font size='4' face='Narkisim' color='#545B6A'>".$get_contents."</font></p><br><hr><br>";
echo "<pre class='blog-ending'><font face='David' size='2' color='black'>| POSTED IN <a class='blog-link1' href='eere'>".strtoupper($get_category)."</a>, <a class='blog-link1' href='eere'>".strtoupper($get_sub_category)."</a>, <a class='blog-link1' href='eere'>".strtoupper($get_specified_sub_category)."</a> | TAGGED <a class='blog-link1' href='eere'>".strtoupper($get_tag1)."</a>, <a class='blog-link1' href='eere'>".strtoupper($get_tag2)."</a>, <a class='blog-link1' href='eere'>".strtoupper($get_tag3)."</a> |</font></pre><br><hr>";
回显“
”;
回显“
”;
}
?>
这段代码用于在每次发布新博客文章时增加博客计数器,以便我可以将我的博客文章与该唯一名称字段链接起来。这在概念上是否可能?如果不可能,我可以使用哪些其他方法来解决该问题。顺便说一句,上面的代码片段给了我错误。
最佳答案
您可以尝试在查询博客文章时,同时查询博客文章 ID(假设数据库中有一个 ID。每条记录都应该有一个主键)。创建按钮时,您可以执行以下操作:
echo "<button class="..." name="$BlogID" type="submit">Like</button>";
或者为您获取的每篇博客文章创建一个隐藏输入。因此,当您提交表单时,隐藏的输入将随之提交。
echo "<input type="hidden" name="postID" value="$blogID">";
然后,您尝试传递给的 PHP 脚本可以处理该值并相应地修改数据库。
PS 你应该使用 PHP 扩展 PDO 来进行数据库查询。 mysql_query() 方法没有那么强大。我在开始第一个 Web 项目时犯了这个错误,现在我必须使用 PDO 重写它。希望这篇文章对您有所帮助!
关于javascript - 是否可以将 PHP 代码嵌入到 HTML 标记的类或名称属性中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27665028/