Page 1 of 1

Private Message System Pt. [08]. Can't insert messages...

Posted: Sat Jun 29, 2013 11:04 am
by scdogas321
Well hello everyone :), I have this title problem. So when I type the message and press send then look in my database and conversations table is filled, conversations_members is filled, but conversations_message is empty :/. Can anyone help me?
Here's the code:

[syntax=php]
require("./connect.php");
function create_conversation($user_ids, $subject, $body) {

$subject = mysql_real_escape_string(htmlentities($subject));
$body = mysql_real_escape_string(htmlentities($body));

mysql_query("INSERT INTO conversations (conversation_subject) VALUES ('{$subject}')");
$conversation_id = mysql_insert_id();

$sql = "INSERT INTO conversations_messages (conversation_id, user_id, message_date, message_text)
VALUES ({$conversation_id}, {$_SESSION['id']}, UNIX_TIMESTAMP(), '{$body}')";

mysql_query($sql);

$values = array();

$user_ids[] = $_SESSION['id'];

foreach ($user_ids as $user_id) {
$user_id = (int) $user_id;

$values[] = "({$conversation_id}, {$user_id}, 0, 0)";
}

$sql = "INSERT INTO conversations_members (conversation_id, user_id, conversation_last_view, conversation_deleted)
VALUES " . implode(", ", $values);

mysql_query($sql);

}

create_conversation(array_unique($user_ids), $_POST['subject'], $_POST['body']);
mysql_close();
[/syntax]

Thanks :).

Re: Private Message System Pt. [08]. Can't insert messages..

Posted: Sat Jun 29, 2013 1:17 pm
by Helx
At line 13 of the code, replace:

[syntax=php]mysql_query($sql);[/syntax]
with

[syntax=php]mysql_query($sql) or die(mysql_error());[/syntax]
This will kill the script if any errors occur in that single query.
If there are no errors, the rest of the code AND query will be unaffected.

If any errors are presented, please post them in this thread.
Remember to replace line 13 back to what it was after debugging. :)

Re: Private Message System Pt. [08]. Can't insert messages..

Posted: Sat Jun 29, 2013 1:57 pm
by scdogas321
Well I get this error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' UNIX_TIMESTAMP(), 'werwe')' at line 2

I think that UNIX_TIMESTAMP is a little bit out of date :D. What can I use instead?

Re: Private Message System Pt. [08]. Can't insert messages..

Posted: Sat Jun 29, 2013 2:15 pm
by Helx
You can set a variable with time();

Eg.
[syntax=php]$thisIsTheTime = time();[/syntax]

Re: Private Message System Pt. [08]. Can't insert messages..

Posted: Sat Jun 29, 2013 2:56 pm
by scdogas321
So, I've done like this:

[syntax=php]
$conversation_id = mysql_insert_id();
$theTime = time();
$sql = "INSERT INTO conversations_messages ( conversation_id, user_id, message_date, message_text)
VALUES ({$conversation_id}, {$_SESSION['id']}, {$theTime}, '{$body}')";
[/syntax]

and it' says:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' 1372517282, '234')' at line 2

Hmmmm :/.

Re: Private Message System Pt. [08]. Can't insert messages..

Posted: Sat Jun 29, 2013 5:08 pm
by FrederickGeek8
scdogas321 wrote:I think that UNIX_TIMESTAMP is a little bit out of date :D. What can I use instead?

Try using DATETIME.

Re: Private Message System Pt. [08]. Can't insert messages..

Posted: Sat Jun 29, 2013 5:35 pm
by scdogas321
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' DATETIME, 'test')' at line 2


:/

Re: Private Message System Pt. [08]. Can't insert messages..

Posted: Sat Jun 29, 2013 9:36 pm
by ExtremeGaming
What is the value of $conversation_id and session id?

Re: Private Message System Pt. [08]. Can't insert messages..

Posted: Sun Jun 30, 2013 7:34 am
by scdogas321
All my code is in the top.