Reputation: 3809
Basically I can do so it prints out for example one field from the table, but I want to have all of them to different tables or whatever, how would I achieve this? I got this as my save/load code:
// Save/Load data.
$('').ready(function() {
if($.cookie('code')) {
$.ajax({
type: "POST",
url: "ajax/loadData.php",
data: "code=" + $.cookie('code'),
dataType: "json"
success: function() {
var json = $.parseJSON();
var coins = json.coins;
var lvl = json.lvl;
$('#test').html('lvl: ' + lvl + ' coins: ' + coins);
}
});
console.log('Loaded data from the database with the code ' + $.cookie('code'));
} else {
// Generate save&load-code
var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
var string_length = 64;
var randomCode = '';
for(var i = 0; i < string_length; i++) {
var num = Math.floor(Math.random() * chars.length);
randomCode += chars.substring(num, num+1);
}
// Save code in cookies & database
$.cookie('code', randomCode);
console.log('Generated and saved code ' + $.cookie('code'));
}
});
Note that I do know that I do not have a save-function/ajax yet, but I'm working on the load feature right now.
This is my loadData.php file:
<?php
include '../inc/_db.php';
$code = $_GET['code'];
$query = mysqli_query($db, "SELECT * FROM data WHERE code='$code'");
while($row = mysqli_fetch_array($query)) {
echo $row['coins'];
}
?>
Simple enough, yeah. This prints out the amount of coins that belongs to the specific user. Here's the table structure:
How would I go about to load both coins AND the lvl field, into different variables or alike, so I can use them properly.
Upvotes: 1
Views: 381
Reputation: 4415
Looks like Joroen helped you out with the ajax side and hopefully you added the comma he was talking about. The php/mysqli part can be written like this:
<?php
include '../inc/_db.php';
$code = $_GET['code'];
$query = mysqli_query($db, "SELECT * FROM data WHERE code='$code'");
$row = mysqli_fetch_array($query));
print json_encode($row);
?>
In reality this code is scary because you're not cleaning any of the incoming data and using it directly in a SQL statement. Since you already know it's only allowing A-Za-z0-9 characters you should check the value of $_GET['code'] to make sure it's safe to use. I also highly recommend using mysqli prepared statements to avoid some of the funny stuff that can happen with tainted input.
Upvotes: 1
Reputation: 159
this is probably a bad programming practice but that is how i would do it.
while($row = mysqli_fetch_array($query)) {
$return = $row['coins'].",".$row['id'; //separate the send the values as a comma-separated string
echo $return;
}
the js would look like this
$('#coins').load('ajax/loadData.php?code=' + $.cookie('code')); //collect the values here
var values = $(#coins).text(); //save the values in a srting
var array_of_values = values.split(","); //split the string at a delimiter and save the values in an array
mind you, this is not likely to work if the query returns multiple rows
Upvotes: 0