View difference between Paste ID: 7VDTQw2K and y2pKjDxN
SHOW: | | - or go back to the newest paste.
1
// include this function in your javascript and to be use to send won to backend
2
3
function update(won){
4
	if (won != '' || won != 'undefined') {
5
		$.ajax({
6
			url: "gameupdate.php",
7
			type: "POST",
8
			data: {"updatewon" : won},
9
			success: function (data) {
10
				if ($.trim(data) == 'success') {
11
					alert('updated');
12
				} else {
13
					alert('something goes wrong');
14
				}
15
			}
16
		});
17
	}
18
}
19
20
// when you want to update won
21
// update(won); <===
22
23
gameupdate.php
24
#########################
25
<?php
26-
if (!emtpy($_POST['updatewon'])) {
26+
if (!empty($_POST['updatewon'])) {
27
28
	// validate $updatewon is within 0 to 100
29
	$updatewon = (in_array($_POST['updatewon'], range(0, 100)))? $_POST['updatewon'] : 0;
30
31
	if(!$link = mysqli_connect("localhost", "root", "....", "db")) {
32
		die ("No connection: ".mysqli_connect_error());
33
	}
34
 
35
	$res = mysqli_query($link, "select balance from registrations where username='{$_SESSION[username]}'");
36
	
37
	// $Balance = $res;
38
	if (mysqli_num_rows($res) === 1) {
39
               
40
		//   insert newblc to database;          
41
		if (mysqli_query($link, "UPDATE registrations SET balance = balance + {$updatewon} WHERE username = '{$_SESSION[username]}'")) 	{
42
			echo 'success'; // this output will be send to DOM as data response
43
			exit;
44
		} else {
45
			echo 'upddate failure';
46
			exit;
47
		}
48
	} else {
49
		echo 'no balance was found';
50
		exit;
51
	}
52
}
53
?>