Page 1 of 1

Private message error part[8][Problem Soloved]

Posted: Tue Sep 11, 2012 10:58 am
by Dyrsch178
Hallo i have an error and i didnt find any sulution for it here on the webiste.
The problem is that it dosnt add anything in to my db when i try to add a conversation. i link my code. Help please:)

Private_message.inc.php
<?php

function create_conversation($user_ids, $subject, $body){
	$subject	= mysql_real_escape_string(htmlentities($subject));
	$body		= mysql_real_escape_string(htmlentities($body));
	
	mysql_query("INSTERT INTO `conversations` (`conversation_subject`) VALUES ('{$subject}')");
	
	$conversation_id = mysql_insert_id();
	
	$sql = "INSERRT INTO `conversations_messages` (`conversation_id`, `anv_id`, `message_date`, `message_text`)
	VALUES ({$conversation_id}, {$_SESSION['anv_id']}, UNIX_TIMESTAMP(), '{$body}')";
	
	mysql_query($sql);
	
	$values = array();
	
	$user_ids[] = $_SESSION['anv_id'];
	
	foreach ($user_ids as $anv_id){
		$anv_id = (int) $anv_id;
		$values[] = "({$conversation_id}, {$anv_id}, 0, 0)";
	}
	
	$sql = "INSERT INTO `conversations_members` (`conversation_id`, `anv_id`, `conversation_last_view`, `conversation_deleted`)
	VALUES " . implode(", ", $values);
	
	mysql_query($sql);
}

?>
New_conversation.php
<?php
include ("core/init.inc.php");

if(isset($_POST['to'], $_POST['subject'], $_POST['body'])){
	$errors = array();
	
	if(empty($_POST['to'])){
		$errors[] = 'You must enter at least one name.';
	}else if (preg_match('#^[a-z,0-9, ]+$#i', $_POST['to']) === 0){
		$errors[] = 'The list of names you have does not look valid.';
	}else{
		$user_names = explode(",", $_POST["to"]);
		
		foreach ($user_names as &$name){
			$name = trim($name);
		}
		
		$user_ids = fetch_user_ids($user_names);
		
		if (count($user_ids) !== count($user_names)){
			$errors[] = 'The following users could not be found: ' . implode(', ', array_diff($user_names, array_keys($user_ids)));
	}
}
	
	if(empty($_POST['subject'])){
		$errors[] = 'The subject cannot be empty.';
	}
	
	if(empty($_POST['body'])){
		$errors[] = 'The body connot be empty.';
	}
	
	if (empty($errors)){
		create_conversation(array_unique($user_ids), $_POST['subject'], $_POST['body']);
	}
}

if (isset($errors)){
	if (empty($errors)){
		echo '<div class="msg success">Your message has been sent! <a href="index.php?page=index">Return to homepage</a></div>';
	}else{
		foreach ($errors as $error){
			echo '<div class="msg error">', $error, '</div>';
		}
	}
}

?>

	<form action="" method="post">
		<div>
        	<label for="to">To</label><br />
        	<input type="text" name="to" id="to" value="<?php if (isset($_POST['to'])) echo htmlentities($_POST['to']); ?>"/>
       	</div>
        <div>
        	<label for="subject">Subject</label><br />
        	<input type="text" name="subject" id="subject" value="<?php if (isset($_POST['subject'])) echo htmlentities($_POST['subject']); ?>"/>
        </div>
        <div>
        	<label for="messages">Message</label><br />
        	<textarea name="body" rows="20" cols="90" id="messages"><?php if (isset($_POST['body'])) echo htmlentities($_POST['body']); ?></textarea>
        </div>
        <div>
        	<a href="Conversations.php?page=inbox"><input type="button" value="Back"/></a>
        	<input type="submit" value="Send" />
        </div>
    </form>