Reputation: 1
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>
";
}
Upvotes: 0
Views: 385
Reputation: 19879
Couple of things here.
Currently, you're posting a success message without checking the return value from your INSERT query. You can fix this by checking the value of $send_msg before printing a message:
$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!";
}
By using trigger_error (mysql_error() like so:
$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. 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.
Please, don't use
mysql_*
functions in new code. 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. If you choose PDO, here is a good tutorial.
Upvotes: 3
Reputation: 392
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'
)
");
Upvotes: 0