Mam problem, ogólnie cały skrypt działa w pewnym sensie. Pod guzik jest dopisany "roll" który aktywuje nam przez request funkcje, wszystko byłoby fajnie ale pierwsze kliknięcie jakby nic nie robi, to znaczy. Dodaje mi rekord do bazy danych losuje liczbie itp. Ale nie odświeża się jakby strona, tabela która wyświetla mi punkty na stronie jak i aktywności jest bez tego rekordu który pojawia się jako nowy za 1 kliknięciem, za drugim gdy skrypt jest jakby w gotowości to dodaje mi punkty i aktywność. Zależy mi na tym żeby po kliknięciu od razu dodało nam tą aktywność i punkty użytkownikowi, bo na razie wygląda tak jakby nic się nie stało.
Dokładniej można to zobaczyć na filmiku: https://i.imgur.com/CDR1Ddt.mp4
Skrypt:
<form action="faucet" method="post">
<h1 class="title counter is-size-1 mt-5">
<?php
if($_SERVER['REQUEST_METHOD'] == "POST" and isset($_POST['roll']))
{
func();
unset($_POST['roll']);
}
function func()
{
$db = mysqli_connect('localhost', 'root', '', 'app');
$db_activity = mysqli_connect('localhost', 'root', '', 'activity');
$dateFromDatabase = strtotime($_SESSION['last_faucet_claim']);
$dateTwelveHoursAgo = strtotime("-12 hours");
if ($dateFromDatabase >= $dateTwelveHoursAgo) {
echo "Sorry, but you used the faucet not long ago";
}
else {
$roll = rand(1,69998);
echo $roll;
$date = date('Y-m-d');
switch ($roll) {
case in_array($roll, range(1,69999)):
$faucet_reward = $_SESSION['points'] + 10;
// Update the points
$sql = "UPDATE users SET points='$faucet_reward' WHERE id = ".$_SESSION['id'];
mysqli_query($db, $sql);
// Add roll to the activity
$sql = "INSERT INTO ".$_SESSION['username']." (title, description, timestamp, count_points) VALUES ('Faucet', 'You roll $roll on the faucet', '$date', 10)";
mysqli_query($db_activity, $sql);
break;
case in_array($roll, range(70000, 89999)):
$faucet_reward = $_SESSION['points'] + 15;
// Update the points
$sql = "UPDATE users SET points='$faucet_reward' WHERE id = ".$_SESSION['id'];
mysqli_query($db, $sql);
// Add roll to the activity
$sql = "INSERT INTO ".$_SESSION['username']." (title, description, timestamp, count_points) VALUES ('Faucet', 'You roll $roll on the faucet', '$date', 15)";
mysqli_query($db_activity, $sql);
break;
case in_array($roll, range(90000, 96999)):
$faucet_reward = $_SESSION['points'] + 20;
// Update the points
$sql = "UPDATE users SET points='$faucet_reward' WHERE id = ".$_SESSION['id'];
mysqli_query($db, $sql);
// Add roll to the activity
$sql = "INSERT INTO ".$_SESSION['username']." (title, description, timestamp, count_points) VALUES ('Faucet', 'You roll $roll on the faucet', '$date', 20)";
mysqli_query($db_activity, $sql);
break;
case in_array($roll, range(97000, 98999)):
$faucet_reward = $_SESSION['points'] + 50;
// Update the points
$sql = "UPDATE users SET points='$faucet_reward' WHERE id = ".$_SESSION['id'];
mysqli_query($db, $sql);
// Add roll to the activity
$sql = "INSERT INTO ".$_SESSION['username']." (title, description, timestamp, count_points) VALUES ('Faucet', 'You roll $roll on the faucet', '$date', 50)";
mysqli_query($db_activity, $sql);
break;
case in_array($roll, range(99000, 99998)):
$faucet_reward = $_SESSION['points'] + 100;
// Update the points
$sql = "UPDATE users SET points='$faucet_reward' WHERE id = ".$_SESSION['id'];
mysqli_query($db, $sql);
// Add roll to the activity
$sql = "INSERT INTO ".$_SESSION['username']." (title, description, timestamp, count_points) VALUES ('Faucet', 'You roll $roll on the faucet', '$date', 100)";
mysqli_query($db_activity, $sql);
break;
}
}
};
?>
</h1>
<button class="button is-success is-large is-fullwidth is-rounded" name="roll">Roll the faucet</button>
</form>
Skrypt na throwbin zeby wygodniej bylo czytac:
https://throwbin.io/24qVq0I