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

如何使用PHP变量正确插入MySQL,php,mysql,insert-into,Php,Mysql,Insert Into,我的个人服务器出现了问题,我试图为我为Yu-Gi的10年历史的活页夹创建一个数据库哦!交易纸牌游戏(已经好几年没玩了)。在测试插入时,我不断遇到一个特定的问题 您的SQL语法有错误;检查与您的MySQL服务器版本对应的手册,以了解使用接近“Magic”(名称、说明、卡片ID、包装、P_ID、数量)值的正确语法(“Post”、“Post”在第1行 现在,当我注释掉查询函数并回显到我的网页时,我的代码正确输出,但我一直看到上面显示的mysql_error()消息 我的代码片段如下所示 if(isse

我的个人服务器出现了问题,我试图为我为Yu-Gi的10年历史的活页夹创建一个数据库哦!交易纸牌游戏(已经好几年没玩了)。在测试插入时,我不断遇到一个特定的问题

您的SQL语法有错误;检查与您的MySQL服务器版本对应的手册,以了解使用接近“Magic”(名称、说明、卡片ID、包装、P_ID、数量)值的正确语法(“Post”、“Post”在第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>";
}
?>
if(isset($\u会话['username'])){
mysql_连接(“本地主机”、“我的用户名”、“我的密码”)或死亡(mysql_错误());
mysql_选择_db(“my_db”)或死亡(mysql_error());
函数clean_字符串($value){
如果(获取\u魔术\u引号\u gpc()){
$value=stripslashes($value);
}
返回mysql\u real\u escape\u字符串($value);
}
$Show=clean_字符串($_POST['Show']);
$Table=clean_字符串($_POST['Table']);
$Insert\u M\u T=$\u POST['Insert\u magic\u traps'];
$Insert_Monster=$_POST['Insert_Monster_effect'];
$Insert_Card_Type=clean_字符串($_POST['I_Type']);
$Insert_Card_Name=clean_字符串($_POST['I_Card_Name']);
$Insert_Description=clean_字符串($_POST['I_C_Description']);
$Insert_Card_ID=clean_字符串($_POST['I_Card_ID']);
$Insert_CardPack=clean_string($_POST['I_C_Pack']);
$Insert_PackID=clean_字符串($_POST['I_C_P_ID']);
$Insert_Quantity=clean_字符串($_POST['I_C_Quantity']);
如果(isset($Insert)){
$sql=“插入到“$INSERT\U Card\U Type”(名称、说明、卡片ID、包装、P\U ID、数量)值(“$INSERT\U Card\U Name”、“$INSERT\U说明”、“$INSERT\U Card\U ID”、“$INSERT\U CardPack”、“$INSERT\U PackID”、“$INSERT\U数量”);
mysql_query($sql)或die(mysql_error());
echo“添加到表中的记录:$Insert\u Card\u Type”;
echo“名称:$Insert\u Card\u Name描述:$Insert\u description Card ID:$Insert\u Card\u IDPack:$Insert\u Card pack ID Number$Insert\u pack数量:$Insert\u数量”;
}
?>
//更多html和php代码
任何建议都会很有帮助。我已经尝试过寻找如何解决这个问题,但没有效果。我觉得这是一个简单的解决方案,但我错过了。请提供建议

先谢谢你

~z~丹斯林

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\u Card\u Type
,而不是单引号。

您应该切换到mysqli或pdo,因为mysqli或pdo函数已被弃用。任何有关mysqli或pdo的教程都会澄清清理+1返回mysql错误,因为表名用单引号括起来。@zdhickman,先生(或女士)太棒了!谢谢你的工作!而且,对于那些建议mySQL的人,我会考虑它,我必须研究它,尽管我已经习惯了MySQL。谢谢大家!