簡體   English   中英

不會插入到mysql表中

[英]Won't Insert into mysql table

我正在構建一個私人消息傳遞網絡,它似乎沒有將它們插入到我指定的表中。 消息被發布到表中,然后在不同的腳本中訪問。 我的查詢有什么問題?

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

    ";
    }

這里有幾件事。

您的代碼不處理失敗的可能性。

目前,您正在發布一條成功消息,而沒有檢查您的 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 錯誤都將被吐出到頁面上。 通過這種方式,您可以消除任何語法錯誤等等。 我的猜測是您正在嘗試將一個空字符串插入到主鍵列中。

mysql_* 函數:

請不要在新代碼中使用mysql_*函數 它們不再被維護,棄用過程已經開始。 看到紅框了嗎? 了解准備好的語句,並使用PDOMySQLi -本文將幫助您決定哪個。 如果您選擇 PDO,這里有一個很好的教程

嘗試這樣的事情 - 只是為了檢查你是否弄亂了列字段(可能是一個可能的原因)

$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