Private Message System [part 08]: Nothing set in db?

Post here is you are having problems with any of the tutorials.
Post Reply
yss
Posts: 1
Joined: Tue May 08, 2012 10:27 am

Private Message System [part 08]: Nothing set in db?

Post by yss »

I have made my code, from the tut, but nothing is stored in the db?

Everthing is the same as in the tut..

My code from new_conversation:
<?php

# errors weergeven
ini_set('display_errors',1); // 1 == aan , 0 == uit
error_reporting(E_ALL | E_STRICT);

if(isset($_POST['to'], $_POST['subject'], $_POST['body'])){
	$error = array();
	if(isset($_POST['to'])){
		$errors[] = 'Je moet tenminste één naam invoeren.';	
	}else if(preg_match('#^[a-z, ]+$#i', $_POST['to']) == 0){
		$errors[] = 'De opgegeven namen bestaan niet.' ;
	}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[] = 'De volgende gebruikers kunnen niet gevonden worden:  ' . implode(',', array_diff($user_names, array_keys($user_ids)));
		}
	}
	
	if(isset($_POST['subject'])){
		$errors[] = 'Vul het onderwerp in.';
	}
	
	if(isset($_POST['body'])){
		$errors[] = 'Het bericht kan niet leeg zijn.';
	}
	
	if ( count( $errors ) == 0 ){
  create_conversation(array_unique($user_ids), $_POST['subject'], $_POST['body']);
  
  
}
}
	if(isset($errors)){
		echo '<div class="msg success">Je bericht is verzonden! <a href="index.php?page=inbox">Terug naar Inbox</a></div>';	
	}else{
		foreach ($errors as $error){
			echo '<div class="msg error">'. $error . '</div>';	
		}
	}

?>

<form action="" method="post">
	<div>
    <label for="to">Ontvanger:</label>
    	<input type="text" name="to" id="to" value="<?php if(isset($_POST['to'])) echo htmlentities($_POST['to']); ?>" />
    </div>
    <div>
    	<label for="subject">Onderwerp:</label>
    	<input type="text" name="subject" id="subject" value="<?php if(isset($_POST['subject'])) echo htmlentities($_POST['subject']); ?>" />
    </div>
    <div>
    	<label for="message">Bericht:</label>
    	<textarea name="body" rows="20" cols="95" id="message" ><?php if(isset($_POST['body'])) echo htmlentities($_POST['body']); ?></textarea>
    </div>
    <div>
    	<input type="submit" value="Verstuur" />
    </div>
</form>
And my code from private_message:
<?php

// create a new conversation, making a given users a member.
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('converstaion_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['user_id']}, UNIX_TIMESTAMP(), '{$body}')";

    mysql_query($sql); 
    
    $values = array();
    
    $user_ids[] = $_SESSION['user_id'];
    
    foreach($user_ids as $user_id){
        $user_id = (int) $user_id;
        
        $values[] = "({$conversation_id}, {$user_id}, 0, 0)";
    }
    
    $sql = "INSERT INTO conversation_members(conversation_id, user_id, conversation_last_view, conversation_deleted)
            VALUES" . implode(", ", $values);
    
    mysql_query($sql);
}   

?>
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Private Message System [part 08]: Nothing set in db?

Post by jacek »

        if(isset($errors)){
                echo '<div class="msg success">Je bericht is verzonden! <a href="index.php?page=inbox">Terug naar Inbox</a></div>';    
        }else{
                foreach ($errors as $error){
                        echo '<div class="msg error">'. $error . '</div>';     
                }
        }
This bit is wrong, you only want to show the success message if the errors array is empty not just if is it set.

One of the errors is probably happening and being hidden by this mistake.
Image
arunjoseph
Posts: 7
Joined: Thu Feb 23, 2012 11:25 am

Re: Private Message System [part 08]: Nothing set in db?

Post by arunjoseph »

It says that Dupicate entry 'num-num' for key unique
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Private Message System [part 08]: Nothing set in db?

Post by jacek »

That sounds like an SQL error, it would happen if you try to put a value that already exists into a column that is marked as unique. Not really sure where that would happen in your code though :? ?
Image
Post Reply