I would like to check if variable $gok is between 1 and 100. If not, the form can not be submitted and no data can be stored in the database.
I use this function but it doesn't work properly:
if ($gok >= 1 && $gok <= 100) {
} else {
echo 'getal tussen 1 en 100 aub';
header('Location: index.php');
}
When I enter a number in the form that is not between 1 and 100, the form will be returned to index.php (which is good). But the data is still stored into the database which is not my intention. How do I fix this problem?
This is my index.php:
<?php
date_default_timezone_set('UTC');
//db-connectie wordt hier ge-include
include_once 'core/db_connect.php';
//head wordt hier ge-include
include_once 'inc/head.php';
//leeftijd wordt gecheckt d.m.v. de in te voeren geboortedatum (de minimum leeftijd moet 18 jaar zijn)
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$geboortedatum = $_POST['geboortedatum'];
$leeftijd = date('Y-m-d') - $geboortedatum;
if ($leeftijd > 17) {
$roepnaam = $_POST['roepnaam'];
$achternaam = $_POST['achternaam'];
$aantal = $_POST['aantalRondes'];
$inzet = $_POST['inzet'];
$gok = $_POST['gokgetal'];
$ipRonde = $inzet / $aantal;
$gokArray = Array();
if ($gok >= 1 && $gok <= 100) {
} else {
echo 'getal tussen 1 en 100 aub';
header('Location: index.php');
}
//formulier invoer wordt naar de tabel 'user' toegestuurd en opgeslagen
$sql = "INSERT INTO user (achternaam, roepnaam, geboortedatum, gokgetal, inzet) VALUES ('$achternaam', '$roepnaam', '$geboortedatum', '$gok',
'$inzet')";
if (mysqli_query($conn, $sql)) {
// echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
//variabelen worden opgehaald en ge-echo-ed
echo '<p>Beste '.$roepnaam.'<br />
Je inzet bedraagt € '. $ipRonde .' per ronde <br />
Je hebt in totaal € '. $inzet .' betaald <br />
Jouw kansen gaan nu beginnen!</p>';
//tabel wordt ge-echo-ed
echo '<table>
<tr>
<td>Ronde</td>
<td>Gokgetal</td>
<td>Echt getal</td>
<td>Prijs</td>
</tr>';
$i = 1;
$raak = 0;
$totprijs = 0;
$random = Array();
$sql = "SELECT user_id
FROM user
WHERE roepnaam = '$roepnaam'
AND geboortedatum = '$geboortedatum'
AND gokgetal = '$gok'";
$result = $conn->query($sql);
$row = $result->fetch_assoc();
echo $row['user_id'];
while($i <= $aantal) {
//random getal tussen 1 en 100 wordt aangemaakt d.m.v. de mt_rand(1, 100) functie
$random[$i] = mt_rand(1, 100);
echo '<tr><td>'.$i.'</td>';
echo '<td>'.$gok.'</td>';
echo '<td>'.$random[$i].'</td>';
if($random[$i] == $gok){
$raak++;
switch ($raak) {
case 1:
$prijs = 3*$aantal*$inzet;
break;
case 2:
$prijs = 2*$aantal*$inzet;
break;
default:
$prijs = 10*$inzet;
break;
}
$totprijs += $prijs;
echo '<td>€ '.$prijs.'</td>';
} else {
$prijs = 0;
echo '<td>€ 0</td>';
}
echo '</tr>';
// gegevens uit de tabel worden verstuurd naar de gok_regel tabel in de database
$sql = "INSERT INTO gok_regel (user_id, echtgetal, prijs) VALUES ('" . mysqli_real_escape_string( $conn, $row['user_id'] ) . "', '$random[$i]', '$prijs')";
if (mysqli_query($conn, $sql)) {
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
$i++;
}
echo '</table>';
if ($totprijs > 0) {
echo '<p>Wauuw!! Je hebt maar liefst € ' . $totprijs . ' gewonnen!!</p>';
} else {
echo '<p>Och wat jammer, wat en pech!! Volgende keer wint u zeker!!</p>';
}
// als de geboortedatum niet 18 jaar of ouder is krijgt de gokker dit bericht te zien
} else {
echo '<p>Helaas, je bent niet 18 jaar of ouder!</p>';
}
} else {
include_once 'inc/formulier.php';
}
// include_once 'inc/footer.php';
And this is my formulier.php:
<form action="" method="POST">
<label for="roepnaam">Roepnaam</label>
<input type="text" name="roepnaam" placeholder="roepnaam" required>
<label for="achternaam">Achternaam</label>
<input type="text" name="achternaam" placeholder="achternaam" required>
<label for="geboortedatum">Geboortedatum (minimaal 18 jaar!)</label>
<input type="date" name="geboortedatum" placeholder="geboortedatum" required>
<label for="gokgetal">Gokgetal</label>
<input type="number" name="gokgetal" placeholder="tussen 1 en 100" required>
<label for="aantalRondes">Aantal rondes</label>
<input type="number" name="aantalRondes" placeholder="bijvoorbeeld 10" required>
<label for="inzet">Inzet</label>
<input type="number" name="inzet" placeholder="bijvoorbeeld 2" required>
<input type="submit" value="VERSTUUR">
</form>
Aucun commentaire:
Enregistrer un commentaire