jQuery的 - 加载从数据库中的信息,为不同的变量?数据库中、变量、加载、不同

2023-09-10 20:27:41 作者:笙歌叹离愁

基本上我能做到这一点,打印出例如一个字段的表,但我想有所有这些不同的表或什么的,我将如何实现这一目标?我得到这个作为我的保存/载入code:

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'));
    }
});

请注意,我知道我没有保存功能/ AJAX还没有,但我的工作负载功能现在。

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.

这是我的loadData.php文件:

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.

推荐答案

看起来Joroen帮助您与阿贾克斯边,希望你加了逗号,他在说什么。在PHP / mysqli的部分可以这样写的:

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);
?>

在现实中,这code是可怕的,因为你没有任何的清洁输入数据,并直接在SQL语句中使用它。既然你已经知道它是只允许A-ZA-Z0-9的字符,你应该检查$ _GET [code']的值,以确保它的安全使用。我也强烈建议使用mysqli的prepared语句来避免一些有趣的东西,可以用输入被污染的发生。

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.