我正在构建一个私人消息传递网络,它似乎没有将它们插入我指定的表中。消息将发布到表中,然后以其他脚本进行访问。我的查询有什么问题?
$new_of_id = $_SESSION['user_login'];
$send_msg = mysql_query("INSERT INTO pvt_messages VALUES ('','$new_of_id','$username','$msg_title','$msg_body','$date','$opened','$deleted')");
echo "Your message has been sent!";
}
}
echo "
<form action='send_msg.php?u=$username' method='POST'>
<h2>Compose a Message: ($username)</h2>
<input type='text' name='msg_title' size='30' onClick=\"value=''\" value='Enter the message title here ...'><p />
<textarea cols='50' rows='12' name='msg_body'>Enter the message you wish to send ...</textarea><p />
<input type='submit' name='submit' value='Send Message'>
</form>
";
}
答案 0 :(得分:3)
这里有很多事情。
目前,您在未检查INSERT查询的返回值的情况下发布成功消息。您可以通过在打印消息之前检查$ send_msg的值来解决此问题:
$send_msg = mysql_query("
INSERT INTO pvt_messages
VALUES(
'',
'$new_of_id',
'$username',
'$msg_title',
'$msg_body',
'$date',
'$opened',
'$deleted'
)
");
//$send_msg will be TRUE if the row was inserted. FALSE if it wasn't.
if($send_msg){
echo "Your message has been sent!";
}
else{
echo "We were unable to send your message!";
}
使用trigger_error(mysql_error()如下:
$send_msg = mysql_query("
INSERT INTO pvt_messages
VALUES(
'',
'$new_of_id',
'$username',
'$msg_title',
'$msg_body',
'$date',
'$opened',
'$deleted'
)
") or trigger_error(mysql_error());
...任何阻止INSERT正常运行的MySQL错误都会吐出到页面上。这样,你可以解决任何语法错误和诸如此类的错误。我的猜测是你试图在主键列中插入一个空字符串。
Please, don't use
mysql_*
functions in new code。它们不再被维护,deprecation process已经开始了。请参阅red box?转而了解prepared statements,并使用PDO或MySQLi - this article将帮助您确定哪个。如果您选择PDO here is a good tutorial。
答案 1 :(得分:0)
尝试这样的事情 - 只是为了检查你是否搞乱列字段(可能是一个可能的原因)
$send_msg = mysql_query("
INSERT INTO pvt_messages (
id,
name
)
VALUES(
'',
'$name'
)
");