+ Reply to Thread
Results 1 to 3 of 3
Like Tree2Likes
  • 2 Post By as4s1n

Thread: PHP inbox script returns 1 row

  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 inbox script returns 1 row

    My PHP script for a PM (private message) inbox only returns one row and I am not sure what is wrong.

    PHP Code:
    <a href="index.php?p=createMessageForm">Create Message</a>
    <table border="0" cellpadding="2" cellspacing="2" id="inbox">
    <tr>
    <th width="300">From</th><th width="500">Subject</th><th width="200">Date</th>
    </tr>
    ?>
    <?php
    if(!isset($_SESSION['loggedin'])) 
        echo 
    "<td colspan=\"3\">You must first <a href=\"index.php?p=signInForm\">log in</a> to view your messages</td>\n";
    else {
        
    $curUser $_SESSION['loggedin'];
        
    $query "SELECT id FROM users WHERE username = '$curUser'";
        
    $result mysql_query($query);
        
        if(!
    $result
            
    writeError(mysql_error());
        else {
            while(
    $row=mysql_fetch_array($resultMYSQL_ASSOC)) {
                
    $curUserId $row['id'];
            }
        }
        
        
    $query "SELECT * FROM mail WHERE toUser = '$curUserId'";
        
    $result mysql_query($query);
                
        if(!
    $result
            
    writeError(mysql_error());
        else {
            while(
    $row=mysql_fetch_array($resultMYSQL_ASSOC)) {
                
    $id $row['id'];
                
    $from $row['from'];
                echo 
    "<title>$curUser's Inbox</title>";
                
    $sbj substr($row['subject'],0100);
                
    $date $row['date'];            
        
        
    $query "SELECT username FROM users WHERE id = '$from'";
        
    $result mysql_query($query);
                
        if(!
    $result)
            
    writeError(mysql_error());
        else {
            while(
    $row=mysql_fetch_array($resultMYSQL_ASSOC)) {
                
    $from $row['username'];
    ?>
    <tr>
    <?php echo "<td><a href=\"index.php?p=viewMessage&&id=$id\">$from</a></td><td><a href=\"index.php?p=viewMessage&&id=$id\">$sbj</a></td><td><a href=\"index.php?p=viewMessage&&id=$id\">$date</a></td>"?>
    </tr>
    <?php
                    
    }
                }
            }
        }
    }
    ?>
    </table>
    Please help.
    dinomirt96 and karimirt47 like this.

  2. #2
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,267

    Re: PHP inbox script returns 1 row

    Quick answer:

    PHP Code:

    <a href="index.php?p=createMessageForm">Create Message</a>
    <table border="0" cellpadding="2" cellspacing="2" id="inbox">
    <tr>
    <th width="300">From</th><th width="500">Subject</th><th width="200">Date</th>
    </tr>
    ?>
    <?php
    if(!isset($_SESSION['loggedin'])) 
        echo 
    "<td colspan=\"3\">You must first <a href=\"index.php?p=signInForm\">log in</a> to view your messages</td>\n";
    else {
        
    $curUser $_SESSION['loggedin'];
        
    $query "SELECT id FROM users WHERE username = '$curUser'";
        
    $result mysql_query($query);
        
        if(!
    $result
            
    writeError(mysql_error());
        else {
            while(
    $row=mysql_fetch_array($resultMYSQL_ASSOC)) {
                
    $curUserId $row['id'];
            }
        }
        
        
    $query "SELECT * FROM mail WHERE toUser = '$curUserId'";
    $result mysql_query($query);    ### THIS $result IS THE MESSAGES
                
    if(!$result
     
    writeError(mysql_error());
     else {
      while(
    $row=mysql_fetch_array($resultMYSQL_ASSOC)) {
        
    $id $row['id'];
        
    $from $row['from'];
        echo 
    "<title>$curUser's Inbox</title>";
        
    $sbj substr($row['subject'],0100);
        
    $date $row['date'];            
        
        
    $query "SELECT username FROM users WHERE id = '$from'";
        
    $result mysql_query($query);  ### HEY, YOU JUST CHANGED 
                                           #### $result TO USERNAME(S)
                
        
    if(!$result)
          
    writeError(mysql_error());
        else {
          while(
    $row=mysql_fetch_array($resultMYSQL_ASSOC)) {  ### LOOPED THRU $result,
            
    $from $row['username'];    ### SO WHEN YOU GET TO THE OUTER
                                  ### LOOP, $result will be finished
    ?>                                               
    <tr>
    <?php echo "<td><a href=\"index.php?p=viewMessage&&id=$id\">$from</a></td><td><a href=\"index.php?p=viewMessage&&id=$id\">$sbj</a></td><td><a href=\"index.php?p=viewMessage&&id=$id\">$date</a></td>"?>
    </tr>
    <?php
                    
    }
                }
            }
        }
    }
    ?>
    </table>
    Moral of the story....reusing variable names can lead to problems.
    $result_messages $result_username etc.
    Last edited by descalzo; 03-15-2010 at 04:20 PM.
    Nothing is always absolutely so.

  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 inbox script returns 1 row

    Oh, OK. I get it now. Thank you.

+ Reply to Thread

Similar Threads

  1. Website Returns HTTP 400: Bad Request
    By nwucase in forum Free Hosting
    Replies: 3
    Last Post: 06-27-2009, 02:07 PM
  2. PHP Code returns blank page...
    By mattjs92 in forum Programming Help
    Replies: 8
    Last Post: 06-21-2009, 10:31 PM
  3. Replies: 1
    Last Post: 06-16-2008, 12:03 PM
  4. How big is your email inbox?
    By Mitch in forum Computers & Technology
    Replies: 21
    Last Post: 03-04-2008, 09:36 AM
  5. Replies: 11
    Last Post: 02-21-2006, 06:29 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