as4s1n
New Member
- Messages
- 174
- Reaction score
- 4
- Points
- 0
I am working on a PM system for my website, not unlike the one on the forums, and I ran into a big problem I just can't figure out what it is.
Form:
Script:
Sorry it's kind of sloppy.
Form:
HTML:
<form action="index.php" method="post">
<table border="0">
<tr>
<td>To:</td><td><input type="text" name="toUser" size="50" /> * Separate by commas (,)</td>
</tr><tr>
<td>Subject:</td><td><input type="text" name="subject" size="50" /></td>
</tr><tr>
<td colspan="2"><textarea rows="20" cols="50" name="message"></textarea></td>
</tr><tr>
<td><input type="submit" value="Send" /><input type="hidden" name="p" value="createMessageFinish" /></td>
</tr>
</table>
</form>
Script:
PHP:
<?php
$errors = array();
$to = (isset($_REQUEST['toUser'])) ? $_REQUEST['toUser'] : '';
if(!empty($to))
$toUsers = explode(",",$to);
else
$errors[] = 'To is empty.';
$msgS = isset($_REQUEST['subject']) ? $_REQUEST['subject'] : '';
$msgM = isset($_REQUEST['message']) ? nl2br($_REQUEST['message']) : '';
if (!$toUsers) {
$errors[] = 'No recipients.';
}
if (empty($msgS)) {
$errors[] = "Empty subject.";
}
if (empty($msgM)) {
$errors[] = "Empty message.";
}
if(!$errors){
$to = implode(",",$toUsers);
$from = $_SESSION['loggedin'];
$sth = $dbh->query("SELECT id FROM users WHERE username = '$from'");
while($row=$sth->fetch())
$fromID = $row['id'];
for($i=0;$i<sizeof($to);$i++) {
$query = $dbh->prepare("SELECT id FROM users WHERE username = '".$to[$i]."'");
while($row=$query->fetch()) {
$toID = $row['id'];
$send = $dbh->exec("INSERT INTO mail VALUES(0,'$toID','$fromID','$msgM','$msgS',0)");
}
}
if($send)
echo "Message(s) sent";
else
echo "Errors";
} else {
?>Errors: <ul><li><?php
echo implode('</li><li>', $errors);
?></li></ul><?php
}
?>
Sorry it's kind of sloppy.
Last edited: