简体   繁体   中英

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. 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.

echo $query;

You can then run the echoed out query in your mysql shell or phpmyadmin to see why it isn't getting executed.

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

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

'Probably it's because of capitalized column names (ex. 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."')";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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