简体   繁体   English

不会插入到mysql表中

[英]Won't Insert into mysql table

I am building a private messaging network and it doesn't seem to be inserting them into my table specified.我正在构建一个私人消息传递网络,它似乎没有将它们插入到我指定的表中。 The messages are posted into the table and then accessed in a different script.消息被发布到表中,然后在不同的脚本中访问。 What is wrong my my query?我的查询有什么问题?

   $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>

    ";
    }

Couple of things here.这里有几件事。

Your code doesn't deal with the possibility of a failure.您的代码不处理失败的可能性。

Currently, you're posting a success message without checking the return value from your INSERT query.目前,您正在发布一条成功消息,而没有检查您的 INSERT 查询的返回值。 You can fix this by checking the value of $send_msg before printing a message:您可以通过在打印消息之前检查 $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!";
}

You're not debugging your query properly.您没有正确调试您的查询。

By using trigger_error (mysql_error() like so:通过使用 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());

... Any MySQL errors that are preventing your INSERT from running properly will be spit out onto the page. ... 任何阻止您的 INSERT 正常运行的 MySQL 错误都将被吐出到页面上。 This way, you can iron out any syntax errors and whatnot.通过这种方式,您可以消除任何语法错误等等。 My guess is that you're trying to insert an empty string into a primary key column.我的猜测是您正在尝试将一个空字符串插入到主键列中。

The mysql_* functions: mysql_* 函数:

Please, don't use mysql_* functions in new code .请不要在新代码中使用mysql_*函数 They are no longer maintained and the deprecation process has begun on it.它们不再被维护,弃用过程已经开始。 See the red box ?看到红框了吗? Learn about prepared statements instead, and use PDO , or MySQLi - this article will help you decide which.了解准备好的语句,并使用PDOMySQLi -本文将帮助您决定哪个。 If you choose PDO, here is a good tutorial .如果您选择 PDO,这里有一个很好的教程

try something like this - just to check in case you are messing up with column fields (could be a possible reason)尝试这样的事情 - 只是为了检查你是否弄乱了列字段(可能是一个可能的原因)

$send_msg = mysql_query("
    INSERT INTO pvt_messages (
         id,
         name
         )
    VALUES(
        '',
        '$name'
    )
");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM