+ Reply to Thread
Results 1 to 4 of 4

Thread: PHP PM send message help

  1. #1
    as4s1n's Avatar
    as4s1n is offline x10 Sophmore as4s1n is an unknown quantity at this point
    Join Date
    Apr 2009
    Location
    Washington State
    Posts
    174

    PHP PM send message help

    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:
    HTML Code:
    <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 Code:
    <?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 by as4s1n; 04-05-2010 at 12:43 PM.
    There is no such thing as a "stupid question," there are only "stupid people" who don't ask them.

  2. #2
    AngusThermopyle is offline x10Hosting Member AngusThermopyle is an unknown quantity at this point
    Join Date
    Nov 2009
    Posts
    84

    Re: PHP PM send message help

    What is it supposed to do?
    What is the problem?
    Error messages?
    What input causes the problem?
    What have you tried already to find the problem?

  3. #3
    as4s1n's Avatar
    as4s1n is offline x10 Sophmore as4s1n is an unknown quantity at this point
    Join Date
    Apr 2009
    Location
    Washington State
    Posts
    174

    Re: PHP PM send message help

    1) This is supposed to insert a row in the database to simulate an email
    2) I do not know the problem, but my assumption is it has something to do with the 'to' input into the database
    3) I tried a try/catch but it didn't help
    4) My guess is the 'to'
    5) Try/catch
    Last edited by as4s1n; 04-05-2010 at 12:42 PM.
    There is no such thing as a "stupid question," there are only "stupid people" who don't ask them.

  4. #4
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: PHP PM send message help

    You still haven't clearly described the behavior you get, including whether there are any error messages and, if so, what they are. The code is also improperly indented, making it hard to read. However, there is one obvious error:

    PHP Code:
        $query $dbh->prepare("SELECT id FROM users WHERE username = '".$to[$i]."'"); 
    $to holds a string, not an array. You want $toUsers.

    Also, it's best to forget a good chunk of how you used to do DB access with the old mysql driver. PDO does it differently, and does it simpler.

    PHP Code:
    function getUserID($name$db) {
        static 
    $getUID;
        if (
    is_null($getUID)) {
            
    $getUID $db->prepare("SELECT id FROM users WHERE username=?"); 
        }
        
    $getUID->execute(array($from));
        return 
    $getUID->fetchColumn();
    }

    ...

    $toAll implode(",",$toUsers);
    $from $_SESSION['loggedin'];
    $msg = array(
        
    ':from' => getUserID($from$dbh)
        
    ':body' => $msgM,
        
    ':subject' => $msgS
    );
    $sent 0;
    if (
    FALSE === $msg[':from']) {
        
    $errors[] = "Unknown sender: $from";
    } else {
        
    // Always be explicit about which columns you're setting in an INSERT
        
    $sendQuery $dbh->prepare("INSERT INTO mail (..., to, from, ...) VALUES (0,toID,:from,:body,:subject,0) SELECT id AS toID FROM users WHERE username=:to");
        foreach (
    $toUsers as $to) {
            
    $msg[':to'] = $to;
            
    $sent += $send->execute($msg);  
        } 
    }
    if(
    $sent) {
        echo 
    "Message(s) sent"
    } else {
        echo 
    "Errors"
        ... 
    Prepared statement parameters are already mentioned in another thread of yours, so I won't bother going into the details here.

    I could swear we've gone over this before, but I can't find the thread.
    Last edited by misson; 04-05-2010 at 09:47 PM.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

+ Reply to Thread

Similar Threads

  1. PHP send mail - Unable to send to Yahoo! email addresses
    By royyuuki in forum Programming Help
    Replies: 7
    Last Post: 03-02-2010, 10:09 AM
  2. send a chat message for 5 credits
    By TPI007 in forum The Marketplace
    Replies: 9
    Last Post: 07-19-2008, 07:28 PM
  3. phpFreeChat You must be connected to send a message
    By tittat in forum Scripts & 3rd Party Apps
    Replies: 0
    Last Post: 05-10-2008, 01:19 PM
  4. Hey (a thank you message)
    By Paperman12 in forum Introductions
    Replies: 2
    Last Post: 04-11-2008, 12:50 PM
  5. Can somebody send me this...
    By trevor51590 in forum Scripts & 3rd Party Apps
    Replies: 9
    Last Post: 03-23-2006, 07:01 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers