简体   繁体   English

将 PHP 变量插入 MySQL

[英]Inserting PHP variables into MySQL

I'm making a small game to pass time in PHP, and for that, I use a database in MySQL.我正在制作一个用 PHP 打发时间的小游戏,为此,我在 MySQL 中使用了一个数据库。 But, everytime that I check on if it inserted the variables into the database, it doesn't show up.但是,每次我检查是否将变量插入数据库时​​,它都没有显示。
This is the query that I'm using:这是我正在使用的查询:

$sql = "
INSERT INTO `Users`
    (
        Username, 
        Class, 
        Level, 
        Health, 
        Max_Health, 
        Mana, 
        Max_Mana, 
        Intelligence, 
        Strength, 
        Dexitery, 
        Endurance, 
        Vitality, 
        Experience, 
        Required_Experience, 
        Gold, 
        Equiped_Armor, 
        Equiped_Weapon, 
        Inventory_Slot1, 
        Amount1, 
        Inventory_Slot2, 
        Amount2, 
        Inventory_Slot3, 
        Amount3, 
        Inventory_Slot4, 
        Amount4, 
        Inventory_Slot5, 
        Amount5, 
        Inventory_Slot6, 
        Amount6, 
        Inventory_Slot7, 
        Amount7, 
        Inventory_Slot8, 
        Amount8, 
        Inventory_Slot9, 
        Amount9, 
        Inventory_Slot10, 
        Amount10, 
        Skill_1, 
        Skill_2, 
        Skill_3, 
        Skill_4, 
        Skill_5, 
        Skill_6, 
        Skill_7, 
        Location
    ) 
    VALUES 
    (
        ".$Username.", 
        ".$Class.", 
        ".$Level.", 
        ".$Health.", 
        ".$Max_Health.", 
        ".$Mana.", 
        ".$Max_Mana.", 
        ".$Intelligence.", 
        ".$Strength.", 
        ".$Dexitery.", 
        ".$Endurance.", 
        ".$Vitality.", 
        ".$Experience.", 
        ".$Req_Experience.", 
        ".$Gold.", 
        ".$Equiped_Armor.", 
        ".$Equiped_Weapon.", 
        ".$Inventory_Slot1.", 
        0, 
        ".$Inventory_Slot2.", 
        0, 
        ".$Inventory_Slot3.", 
        0, 
        ".$Inventory_Slot4.", 
        0, 
        ".$Inventory_Slot5.", 
        0, 
        ".$Inventory_Slot6.", 
        0, 
        ".$Inventory_Slot7.", 
        0, 
        ".$Inventory_Slot8.", 
        0, 
        ".$Inventory_Slot9.", 
        0, 
        ".$Inventory_Slot10.", 
        0, 
        ".$Skill_1.", 
        ".$Skill_2.", 
        ".$Skill_3.", 
        ".$Skill_4.", 
        ".$Skill_5.", 
        ".$Skill_6.", 
        ".$Skill_7.", 
        'Town'
    )";
mysql_query($sql, $link);

I have absolutely no idea what I'm doing wrong, so some help would be very much appreciated.我完全不知道我做错了什么,所以非常感谢一些帮助。 Thank you in advance.先感谢您。

You can try echoing out the query in PHP before executing it.在执行之前,您可以尝试在 PHP 中回显查询。

echo $query;

You can then run the echoed out query in your mysql shell or phpmyadmin to see why it isn't getting executed.然后,您可以在 mysql shell 或 phpmyadmin 中运行回显查询以查看为什么它没有被执行。

您需要将查询中的值引用为如下所示:

$sql = "INSERT INTO `Users`(Username, Class, Level) VALUES ('$Username', '$Class', '$Level');

'Probably it's because of capitalized column names (ex. U sername). “也许这是因为资本化的列名(如:U sername)的。 Type those names in grave accents `用重音符输入这些名字`

 $sql = "INSERT INTO `Users`(`Username`, `Class`, ...

also variables other than numerical should be in quotes数值以外的变量也应该用引号引起来

$sql = "INSERT INTO `Users`(`Username`, `Class`, ..., `Map`) 
VALUES ('".$Username."', '".$Class."', ... , '".$Map."')";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM