INSERT由于某种原因不会运行

时间:2011-05-08 14:53:20

标签: php mysql

我确信这是一个愚蠢的事情,我很遗憾,但任何人都可以确定为什么以下内容不起作用:

#sanitise variables
$player1 = GetSQLValueString($_POST['player1'], "text");
$battle = GetSQLValueString($_POST['battle'], "text");
$subject = "You Have Registered For ".$battle."";
$subject = GetSQLValueString($subject, "text");
$message = "You Have Registered For ".$battle."<br /><br />
  Keep an eye on your PM's, it won't take long before your challenger appears!";
$message = GetSQLValueString($message, "text");

echo $player1;
echo "<br />";
echo $subject;
echo "<br />";
echo $message;
echo "<br />";
echo $time;
echo "<br />";

#insert alerts into private message databases
mysql_select_db($database_conn, $conn);
$query_rsSendAlert = "INSERT INTO site_private_messages 
  (sender, recipient, read_status, subject, message, `timestamp`) 
  VALUES ('Admin', $player1, 'n', $subject, $message, `$time`)";
mysql_query($query_rsSendAlert, $conn) or die(mysql_error());

回声就是这样,我可以在插入之前看到变量(换句话说就是帮助发现问题。以下是它吐出的一个例子:

'ferdia'
'You Have Registered For \'Fifa 11 Fracus!\''
'You Have Registered For \'Fifa 11 Fracus!\'

Keep an eye on your PMs, it won\'t take long before your challenger appears!'
1304866340

以下是表格格式:

CREATE TABLE `site_private_messages` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `sender` varchar(30) DEFAULT NULL,
  `recipient` varchar(30) DEFAULT NULL,
  `read_status` varchar(1) DEFAULT NULL,
  `subject` varchar(200) DEFAULT NULL,
  `message` text,
  `timestamp` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=39 DEFAULT CHARSET=latin1 ROW_FORMAT=COMPACT;

有什么建议吗?

3 个答案:

答案 0 :(得分:3)

值部分应如下所示:

VALUES ('Admin', '$player1', 'n', '$subject', '$message', '$time')";

答案 1 :(得分:3)

改变这个:

$query_rsSendAlert = "INSERT INTO site_private_messages 
                        (sender, recipient, read_status, subject, message, `timestamp`) 
                      VALUES 
                        ('Admin', $player1, 'n', $subject, $message, `$time`)";
             //Error, no ` allowed in values, use ' instead.         ^     ^

......进入这个:

$query_rsSendAlert = "INSERT INTO site_private_messages 
                        (sender, recipient, read_status, subject, message) 
                      VALUES 
                        ('Admin', $player1, 'n', $subject, $message)";

MySQL会自动填写时间戳。

答案 2 :(得分:2)

有可能,你会发现你的列名时间戳是一个保留字。 如果要将保留字用作列名或表名,则必须将它们括在backticks

但是回显实际的SQL语句,并进行错误处理以识别您获得的确切错误消息。每当使用SQL时,都要养成使用错误处理的习惯,如果让PHP在发生任何MySQL错误时实际显示它们,它将为您省去很多麻烦。