PHP 喜欢系统随机

嘿伙计们,我发现了我的问题,现在我需要帮助如何解决它。我通过 rand 从数据库中获取一个人,单击“喜欢”按钮后,它应该喜欢它显示的当前数字,但事实并非如此。它喜欢单击按钮后显示的下一个数字,我不知道如何更好地显示它。这是我的代码


<?php


include_once 'session.php';

include_once 'dbh.php'; 

?>


<!doctype html>


<html>

<head>

  <meta charset="utf-8">


  <title>The HTML5 Herald</title>

  <meta name="description" content="The HTML5 Herald">

  <meta name="author" content="SitePoint">


  <link rel="stylesheet" href="cs.css">


</head>


<body>


</body>

</html>

<?php



$sql = "SELECT * FROM users ORDER BY RAND () LIMIT 1 ; ";

$result = mysqli_query($conn, $sql);

$resultCheck = mysqli_num_rows($result);        

if ($resultCheck> 0){

    $row = mysqli_fetch_assoc($result);   

    $ide = $row['idUsers'];

    echo $ide;


    if(isset($_POST['like'])){

        $sql1= "INSERT INTO likes (id, likes)

        VALUES ('1', '$ide')";

         if ($conn->query($sql1) === TRUE) {

            echo "New record created successfully";

        } else {

            echo "Error: ".$sql1."<br>". $conn->error;

        }


        $conn->close();


    }


}

?>

<form action ="" method="post">

<button value = "like" name="like">like</button>

</form>


守着星空守着你
浏览 89回答 1
1回答

ITMISS

您需要将用户 ID 放入表单中,并在插入时使用。此外,表格需要在里面<body>,而不是在它之后。<?phpinclude_once 'session.php';include_once 'dbh.php';&nbsp;?><!doctype html><html>&nbsp; &nbsp; <head>&nbsp; &nbsp; <meta charset="utf-8">&nbsp; &nbsp; <title>The HTML5 Herald</title>&nbsp; &nbsp; <meta name="description" content="The HTML5 Herald">&nbsp; &nbsp; <meta name="author" content="SitePoint">&nbsp; &nbsp; <link rel="stylesheet" href="cs.css"></head><body><?php$sql = "SELECT * FROM users ORDER BY RAND () LIMIT 1 ; ";$result = mysqli_query($conn, $sql);$resultCheck = mysqli_num_rows($result);&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;if ($resultCheck> 0){&nbsp; &nbsp; $row = mysqli_fetch_assoc($result);&nbsp; &nbsp;&nbsp; &nbsp; $ide = $row['idUsers'];&nbsp; &nbsp; ?>&nbsp; &nbsp; <form action ="" method="post">&nbsp; &nbsp; <button value = "<?php echo $ide; ?>" name="like">like</button>&nbsp; &nbsp; </form>&nbsp; &nbsp; <?php}
打开App,查看更多内容
随时随地看视频慕课网APP