是否可以将PHP代码嵌入HTML标签的类或名称属性中


Is it possible to embed PHP code within the class or name attributes of HTML tags?

我正在尝试为博客文章创建一个喜欢按钮。我面临的问题是我无法将我的喜欢按钮与每篇博客文章联系起来。目前,所有类似的按钮共享相同的类和名称属性。我尝试使用以下方法:

 <?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(假设您的数据库中有一个。您应该始终为每条记录提供一个主键)。创建按钮时,可以执行以下操作:

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重写它。希望这篇文章对您有所帮助!

通常,不是在服务器端代码中使用计数器,而是使用数据库中的某种序列,或与博客文章内容一起存储的其他 uinique 标识符。 例如,许多人会对表中的每一行使用整数主键(例如,blog_post表中的所有行都有一个列blog_post_id)。 如果此主键设置为自动递增/序列或数据库软件使用的任何内容,则它将自动随每篇博客文章递增。

当您检索博客文章时,blog_post_id也会随之而来。 假设您的行是一个关联数组。 你可以做:

<form method="post" action="/like-blog-post.php" >
<?php foreach($rows as $row) { ?>
   <input type="submit" name="blog_post_<?php echo $row['blog_post_id']; ?> value="Like!" />
<?php } ?>
</form>

每当有人点击喜欢按钮时,就会向服务器发送一个帖子,您将收到一个帖子请求到/like-block-post.php

不过,以这种方式执行此类操作有点笨拙。 它会导致页面更改,必须解析出博客文章 ID,并且名称/值实际上位于 $_POST 数组的键中。

另一种方法是通过这样的链接标签(您可以设置链接样式以使其看起来像一个按钮):

<?php foreach($rows as $row) { ?>
<a href="like-blog-post.php?blog_post_id="<?php echo $row['blog_post_id']; ?>">Like!</a>
<?php } ?>

这仍然会导致页面更改,但数据在服务器端会更容易处理。 这也会有问题,因为任何人都可以使用博客文章ID向该URL发送垃圾邮件,并无限多次喜欢它。 为了防止这种情况,您需要传递会话 ID 和带有查询参数的 csrf 令牌。 页面加载后,您可以通过 javascript 添加 onclick 处理程序来解决页面更改问题。 设置处理程序的代码可以使用 href 属性中完全相同的 URL,但 onclick 处理程序可以异步发出请求以避免页面更改。

我会从链接或按钮开始,然后逐步增加对每个博客文章每个用户的多个喜欢的保护,然后避免页面更改/刷新。

不过,您绝对不应该维护自己的博客文章计数器,这是最适合数据库的任务。 如果您不想使用行 ID,您可以想出一些其他唯一标识符列,将其索引为 UNIQUE,并使用它来查找被喜欢的博客文章。

最初,您似乎在询问是否可以将php代码写入客户端并使其运行。 如果你对此感到好奇,答案是否定的。 一旦页面交付给客户端,任何 php 代码都将只是文档中的文本。 客户端不会执行它。

请像这样编写代码。 它会给你更好的主意。

<?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));
?>
<p class='blog-heading' align='left'>
    <font face='Narkisim' size='5' color='#3E537C'>
        <strong>
            "<?=$get_title?>"
        </strong>
    </font>
</p>
<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'>User Name</a>
        |
        <a class='blog-link1' href='eere'>UpVote</a>
    </font>
</pre>
<br>
<br>
<p class='blog-content' align='justify'>
    <font size='4' face='Narkisim' color='#545B6A'>
        "<?=$get_contents?>"
    </font>
</p>
<br>
<hr>
<br>
<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>
<?php
}
?>