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

Post here is you are having problems with any of the tutorials.
Post Reply
scdogas321
Posts: 5
Joined: Sat Jun 29, 2013 10:54 am

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

Post 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:
		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();
Thanks :).
User avatar
Helx
Posts: 350
Joined: Thu May 17, 2012 6:45 am
Location: Auckland, New Zealand

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

Post by Helx »

At line 13 of the code, replace:
mysql_query($sql);
with
mysql_query($sql) or die(mysql_error());
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. :)
scdogas321
Posts: 5
Joined: Sat Jun 29, 2013 10:54 am

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

Post 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?
User avatar
Helx
Posts: 350
Joined: Thu May 17, 2012 6:45 am
Location: Auckland, New Zealand

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

Post by Helx »

You can set a variable with time();

Eg.
$thisIsTheTime = time();
scdogas321
Posts: 5
Joined: Sat Jun 29, 2013 10:54 am

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

Post by scdogas321 »

So, I've done like this:
$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}')";
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 :/.
User avatar
FrederickGeek8
Posts: 148
Joined: Wed Nov 30, 2011 10:31 pm

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

Post 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.
scdogas321
Posts: 5
Joined: Sat Jun 29, 2013 10:54 am

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

Post 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


:/
ExtremeGaming
Posts: 205
Joined: Mon Jul 09, 2012 11:13 pm

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

Post by ExtremeGaming »

What is the value of $conversation_id and session id?
<?php while(!$succeed = try()); ?>
scdogas321
Posts: 5
Joined: Sat Jun 29, 2013 10:54 am

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

Post by scdogas321 »

All my code is in the top.
Post Reply