php - 如何使用 PHP 变量正确插入 MySQL

标签 php mysql insert-into

我的个人服务器出现问题,我正在尝试为《游戏王》的十年前 Binder 创建一个数据库!集换式卡牌游戏(已经好几年没玩了)。在测试 INSERT INTO 时,我不断遇到一个特定的问题......

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''Magic'(Name, Description, Card_ID, Pack, P_ID, Quantity) VALUES ('Post', 'Post ' at line 1

现在,当我注释掉查询函数并回显到我的网页时,我的代码可以正确输出,但我不断显示上述 mysql_error() 消息。

我的代码片段如下...

if(isset($_SESSION['username'])) {
mysql_connect("localhost", "my_username", "my_password") or die(mysql_error());
mysql_select_db("my_db") or die(mysql_error());

function clean_string($value) {
    if(get_magic_quotes_gpc() ) {
            $value = stripslashes($value);
    }
    return mysql_real_escape_string($value);
}

$Show = clean_string($_POST['show']);
$Table = clean_string($_POST['table']);
$Insert_M_T = $_POST['insert_magic_traps'];
$Insert_Monster = $_POST['insert_monster_effect'];

$Insert_Card_Type = clean_string($_POST['I_Type']);
$Insert_Card_Name = clean_string($_POST['I_Card_Name']);
$Insert_Description = clean_string($_POST['I_C_Description']);
$Insert_Card_ID = clean_string($_POST['I_Card_ID']);
$Insert_CardPack = clean_string($_POST['I_C_Pack']);
$Insert_PackID = clean_string($_POST['I_C_P_ID']);
$Insert_Quantity = clean_string($_POST['I_C_Quantity']);

if(isset($Insert_M_T)) {
    $sql = "INSERT INTO '$Insert_Card_Type'(Name, Description, Card_ID, Pack, P_ID, Quantity) VALUES ('$Insert_Card_Name', '$Insert_Description', '$Insert_Card_ID', '$Insert_CardPack', '$Insert_PackID', '$Insert_Quantity')";
    mysql_query($sql) or die(mysql_error());
    echo "<center><h2>Record added to Table: $Insert_Card_Type</h2></center>";
    echo "<center><table><tr><th>Name:</th><td>$Insert_Card_Name</td></tr><tr><th>Description:</th><td>$Insert_Description</td></tr><tr><th>Card ID:</th><td>$Insert_Card_ID</td></tr><tr><th>Pack:</th><td>$Insert_CardPack</td></tr><tr><th>Pack ID Number</th><td>$Insert_PackID</td></tr><tr><th>Quantity:</th><td>$Insert_Quantity</td></tr></table></center>";
}
?>
//more html and php code
<?php
} else {
    echo "<h1><center><font color=#ff0000 >ACCESS DENIED!!!</font></center></h1>";
    echo "<h2><center><a href=index.php >Login Here!</a></center></h2>";
}
?>

任何建议都会有帮助。我尝试寻找如何解决这个问题,但没有成功。我觉得这是一个简单的修复,但我错过了它。请指教。

提前谢谢您。

~舞蹈链接

最佳答案

INSERT INTO `$Insert_Card_Type` (Name, Description, Card_ID, Pack, P_ID, Quantity) 
  VALUES ('$Insert_Card_Name', '$Insert_Description', '$Insert_Card_ID', '$Insert_CardPack', '$Insert_PackID', '$Insert_Quantity')

$Insert_Card_Type 周围有反引号,而不是单引号。

关于php - 如何使用 PHP 变量正确插入 MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17118038/

相关文章:

php - Laravel artisan 制作 :auth

MySQL #1064 - 你的 SQL 语法有错误

php - 从数据库中输出元组的总和

MySQL AFTER INSERT 触发器 - 使用新值选择 INTO 不起作用

php - 一张表尚未创建,另一张表正在创建

Java `json_decode` (PHP) 等效项

php - jquery文件上传php数据库blueimp

php - 我无法在菜单中的所有 3 个点上激活它

java - MySQLSyntaxErrorException : Mybatis :Bulk update query using foreach

sql - 将值插入表 Oracle SQL