Q:

PHP like system random

Hey guys i found my problem, now i need help how to solve it. I get one person from data base by rand 
and after clicking like button it should like current number that it shows, but its not. It likes next number that shows after clicking button i dont know how to display it better. Here's my code
<?php

include_once 'session.php';
include_once 'dbh.php'; 
?>

<!doctype html>

<html lang="en">
<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>

A:

You need to put the user ID into the form, and use that when inserting.
Also, the form needs to be inside <body>, not after it.
<?php

include_once 'session.php';
include_once 'dbh.php'; 
?>

<!doctype html>

<html lang="en">
    <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>
<?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'];
    ?>
    <form action ="" method="post">
    <button value = "<?php echo $ide; ?>" name="like">like</button>
    </form>
    <?php
}

if(isset($_POST['like'])){
    $sql1= "INSERT INTO likes (id, likes)
        VALUES ('1', ?)";
    $stmt = $conn->prepare($sql1);
    $stmt->bind_param("s", $_POST['like']);
    if ($stmt->execute()) {
        echo "New record created successfully";
    } else {
        echo "Error: ".$sql1."<br>". $stmt->error;
    }
}
$conn->close();
?>
</body>
</html>

