我是 PHP 新手,所以这可能是我错过的明显事情。我试图制作一个按钮来增加数据库中的值:<?php $alist = mysqli_query($conn, "SELECT * FROM `posts` ORDER BY `posts`.`id` DESC"); $results = mysqli_num_rows($alist); if ($results > 0){ while($row = mysqli_fetch_array($alist)) { echo $row['uid']. " says: ".$row['postText']." <button onclick=".mysqli_query($conn, "UPDATE `posts` SET `postLikes` = postLikes+1 WHERE uid = ".$row['uid'])." name='likebtn'>👍</button>".$row['postLikes']."<br>"; } } ?>制作按钮的部分位于第 6 行,我只是想找到如何使用按钮 mysqli_query单击, 我已经尝试过:“https://stackoverflow.com/questions/3862462/php-mysql-run -query-on-button-press-click”但没有结果提前致谢
2 回答
ABOUTYOU
TA贡献1812条经验 获得超5个赞
如果您当前的页面名为first.php,请放入按钮内
<button><a href="first.php?p=like"></a></button>
<?php
if ( isset($_GET['p']) && $_GET['p']=="like") {
do your query
}
?>
如果你不想重新加载那么你需要ajax,
慕妹3242003
TA贡献1824条经验 获得超6个赞
你这里有语法错误 echo $row['uid']. " says: ".$row['postText']." <button onclick=".mysqli_query($conn, "UPDATE posts SET postLikes = postLikes+1 WHERE uid = ".$row['uid'])." name='likebtn'>👍</button>".$row['postLikes']."<br>";
你可以这样做
$q = mysqli_query($conn, "UPDATE posts SET postLikes = postLikes+1 WHERE uid = ".$row['uid']);
echo $row['uid']. " says: ".$row['postText']." <button onclick=".$q." name='likebtn'>👍</button>".$row['postLikes']."<br>";
- 2 回答
- 0 关注
- 143 浏览
添加回答
举报
0/150
提交
取消