我如何在单击按钮时运行 mysql 查询

我是 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”但没有结果


提前致谢


缥缈止盈
浏览 144回答 2
2回答

ABOUTYOU

如果您当前的页面名为first.php,请放入按钮内<button><a href="first.php?p=like"></a></button><?phpif ( isset($_GET['p']) && $_GET['p']=="like") {do your query}?>如果你不想重新加载那么你需要ajax,

慕妹3242003

你这里有语法错误&nbsp;echo $row['uid']. " says: ".$row['postText']." &nbsp;<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']." &nbsp;<button onclick=".$q." name='likebtn'>👍</button>".$row['postLikes']."<br>";
打开App,查看更多内容
随时随地看视频慕课网APP