无法将数据插入数据库php/msql
我正在创建一个聊天系统,人们可以发送一条消息,该消息将存储在数据库中,可以检索到该消息并显示在收件箱中。现在,当我试图保存消息时,我无法将信息插入数据库。请帮忙 代码:无法将数据插入数据库php/msql,php,mysql,Php,Mysql,我正在创建一个聊天系统,人们可以发送一条消息,该消息将存储在数据库中,可以检索到该消息并显示在收件箱中。现在,当我试图保存消息时,我无法将信息插入数据库。请帮忙 代码: include("inc/header.inc.php"); if (isset($_GET['u'])) { $username = mysql_real_escape_string($_GET['u']); if (ctype_alnum($username)) { //check use
include("inc/header.inc.php");
if (isset($_GET['u'])) {
$username = mysql_real_escape_string($_GET['u']);
if (ctype_alnum($username)) {
//check user exists
$check = mysql_query("SELECT username FROM users WHERE username='$username'");
if (mysql_num_rows($check) === 1) {
$get = mysql_fetch_assoc($check);
$username = $get['username'];
//Check user isn't sending themself a private message
if ($username != $user) {
if (isset($_POST['submit'])) {
$msg_title = strip_tags($_POST['msg_title']);
$msg_body = strip_tags($_POST['msg_body']);
$date = date("Y-m-d");
$opened = "no";
$deleted = "no";
if ($msg_title == "Enter the message title here ...") {
echo "Please give your message a title.";
}
else if (strlen($msg_title) < 3) {
echo "Your message title cannot be less than 3 characters in length!";
}
else if ($msg_body == "Enter the message you wish to send ...") {
echo "Please write a message.";
}
else if (strlen($msg_body) < 3) {
echo "Your message cannot be less than 3 characters in length!";
}
else {
$send_msg = mysql_query("INSERT INTO pvt_messages (id, user_from, user_to, msg_body, date, opened) VALUES ('','$user','$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 to $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' style='margin-left: 0px; margin-right: 0px; width: 305px;'>Enter the message you wish to send ...</textarea><p />
<input type='submit' name='submit' class='btn btn-primary' value='Send Message'>
</form>
";
}
else {
header("Location: $user");
}
}
}
}
包括(“inc/header.inc.php”);
如果(isset($\u GET['u'])){
$username=mysql\u real\u escape\u字符串($\u GET['u']);
if(ctype_alnum($username)){
//检查用户是否存在
$check=mysql_query(“从username='$username'的用户中选择用户名”);
if(mysql_num_rows($check)==1){
$get=mysql\u fetch\u assoc($check);
$username=$get['username'];
//检查用户是否未向自己发送私人消息
如果($username!=$user){
如果(isset($_POST['submit'])){
$msg_title=strip_标签($_POST['msg_title']);
$msg_body=strip_标签($_POST['msg_body']);
$date=日期(“Y-m-d”);
$opened=“否”;
$deleted=“no”;
如果($msg_title==“在此处输入消息标题…”){
echo“请给您的邮件一个标题。”;
}
else if(strlen($msg_title)<3){
echo“您的邮件标题长度不能少于3个字符!”;
}
else if($msg_body==“输入您希望发送的消息…”){
echo“请写封信。”;
}
else if(strlen($msg_body)<3){
echo“您的邮件长度不能少于3个字符!”;
}
否则{
$send_msg=mysql_query(“插入到pvt_消息(id、用户_from、用户_to、消息正文、日期、打开)值(“''$user'、'$username'、'$msg_title'、'$msg_body'、'$date'、'$opened'、'$deleted');
回显“您的信息已发送!”;
}
}
回声“
向$username撰写消息
输入您希望发送的消息…
";
}
否则{
标题(“位置:$user”);
}
}
}
}
有什么问题吗?可能是错误的,但是你不应该用“mysql\U query”(“INSERT…”)代替“$send\U msg=mysql\U query”(“INSERT…”)插入到“id”列中-这不是错误的吗?使用maybe try{}catch{}检查是否有错误。不要使用strip\U标记(而不是filter\U var或htmlentities)。阅读Sql注入。我发现了我的错误,我要插入的值比数据库中的实际列多,因此我删除了values()部分中不存在的列
感谢您的选择,我们将试用。您从未设置过
$user
。而且您不需要$username!=$user
如果您得到一个结果,您知道用户名等于提交的用户名。@chris85太多“可能”下面。Facepalm。请查阅以下链接并将其应用于您的代码。这并不重要,除非使用条件语句,否则以后不需要访问变量赋值。猜测最好留在注释中。请等待,直到您能够获得足够的代表。flag谢绝。主要Facepalm。