Hi everyone!
I got a little problem, but I really have no idea how to solve it. I had to create a little number guessing game and be able to collect data in the databank. My DataBank is very simple. It consists of username, points and time when played. So I don’t have any primary key or things like that. So how can I actually avoid duplications when page is refreshed??? My code is divided to 2 pages, quiz.php output and logic.php. Here they are:
logic.php
[php]<?php
session_start();
$dblink = mysqli_connect(‘localhost’, ‘root’) or die("Cannot select database! " . mysql_error());
$gamedata = mysqli_select_db($dblink, ‘guessgame’);
if (!$gamedata) {
die ('Cannot use DB : '.mysqli_error($dblink));
}
####################################
############— MENU —############
####################################
$title = ‘’;
$content = ‘’;
$menu = $_GET[‘menu’];
if($menu == ‘quiz’) {
$title = ‘Quiz’;
$content = ‘quiz_c.php’;
}elseif($menu == ‘info’){
$title = ‘Info’;
$content = ‘info_c.php’;
}else{
$title = ‘Home’;
$content = ‘home_c.php’;
}
########################################
############— USERNAME —############
########################################
if($_POST[‘formular’] == “username_form”){
if(ctype_alpha($_POST[“username”])==false || strlen($_POST[“username”]) > 20){
echo “Please use only letters of max length 20”;
return false;
} else {
$_SESSION[‘username’] = true;
$_SESSION[‘username’] = $_POST[‘username’];
}
}
####################################
############— GAME —############
####################################
if (empty($_SESSION[‘try_numb’]))
{
$_SESSION[‘try_numb’] = 1;
$_SESSION[‘points’] = 0;
}
if($_POST['formular'] == "guess_game"){
if ($_SESSION['try_numb'] < 11){
$guess = $_POST['guess'];
$random_num = rand(1, 5);
if($guess == $random_num){
$_SESSION['points']++;
}
$_SESSION['try_numb']++;
}
#else {
#}
}
if ($_POST['formular'] == 'new_game'){
session_destroy();
session_start();
unset($_SESSION['username']);
}
?>[/php]
And the game:
[php]<?php
if($_SESSION[‘username’] == false){
?>
Spielername:
"; echo "Das ist dein ".$_SESSION['try_numb'].". Versuch!"."
"; echo "Du hast bis jetzt ".$_SESSION['points']." korrekte Antworten!"."
"; ?>
An welche Zahl denke ich jetzt?
<?php } else { $_SESSION['timestamp'] = time(); $time = $_SESSION['timestamp']; $name = $_SESSION['username']; $points = $_SESSION['points']; $sql="INSERT INTO guessgame (name, points, time) VALUES ('$name','$points','$time')"; $result = mysqli_query($dblink,$sql); if ( !$result ) { die('Ups, Fehler: '.mysqli_error($dblink)); } echo "Ihr Spielername: ".$_SESSION['username'].""; echo "Fertig!"."
"; echo "Du hast ".$_SESSION['points']." korrekte Antworten!"; ?>
<form method="post">
<input type="submit" name="new_game" value="new game" />
<input type="hidden" name="formular" value="new_game" />
</form>
<?php
}
}
?>[/php]
I would be sooo greatful if someone could help me with it!!!
