Duplication of Data in MySQL?

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:






<?php } else { if ($_SESSION['username'] == true && $_SESSION['try_numb'] < 11) { echo "Ihr Spielername: ".$_SESSION['username']."
"; 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!!!

simply run and a query to check if what you are inserting already exist.

if it doesnt exist go ahead and insert it

Yeah, I also thought of it. But the problem is that my user can play countless times and each time he starts new. If I refresh page few secs later in the end of game, then it already looks like “new” game, cause timestamp will change. But if I check my DataBank with just a name and points? What if same player plays once again and get same points?

i understand,

i think the only 2 solutions will be check the database before any Insert OR update any records after each game

hopefully other folks here will drop by and give you more advise

Thanks for your advices anyway! :slight_smile:

Any any help?

Yuhu! I did it! :slight_smile: I added in the output (quiz) a new session variable in the end of saving data to mysql and in the logic used header(Location: ‘…’); :slight_smile:

Here is the full code, maybe it will help someone one day. :slight_smile:

The Output (Quiz):
[php]<?php

	if($_SESSION['username'] == false){

?>

Spielername:






<?php } else { if ($_SESSION['username'] == true && $_SESSION['try_numb'] < 11) { echo "Ihr Spielername: ".$_SESSION['username']."
"; 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'] + 7200; $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!"; $_SESSION['sqldata'] = $sql; ?>
<form method="post">
<input type="submit" name="new_game" value="new game" />
<input type="hidden" name="formular" value="new_game" />
</form> 
<?php } } ?>[/php]

And The Logic:

[php]<?php
session_start();

if(isset($_SESSION[‘sqldata’])){
session_destroy();
header(‘Location: ?menu=quiz’);
exit;

	}

$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]

Sponsor our Newsletter | Privacy Policy | Terms of Service