i have created a shoutbox that has two fields...author & message. i have no problem inserting the data into the table but getting it to print is another story. can someone take a look at this and tell me the error(s) in my ways???

<?

// Define post fields into simple variables
$author = $_POST['author'];
$message = $_POST['message'];



/* Let's strip some slashes in case the user entered
any escaped characters. */

$author = stripslashes($author);
$message = stripslashes($message);


/// Enter info into the Database.
$sql = mysql_query("INSERT INTO shoutbox (author, message, date)
		VALUES('$author', '$message', now())") or die (mysql_error());
mysql_query($sql); 

// Show thanks message and take them back to the main shoutbox 
echo "Thanks for your post<BR>"; 


// Set the query as $query, and get the last 10 posts. 
    $query = "SELECT author, message FROM shoutbox order by date"; 
    $result = mysql_query($query); 
   print "<iframe>\n";
 print "$result";
print "</iframe>\n";
/* Results */
mysql_free_result($result);



?>

    You've not 'looped' through the $result array

    // Set the query as $query, and get the last 10 posts. 
        $query = "SELECT author, message FROM shoutbox order by date";  
    /* Results */ $result = mysql_query($query);
    while($obj = mysql_fetch_object($result)){ print "<iframe>\n"; print $obj->author; print "<br>\n"; print $obj->message ; print "</iframe>\n"; } mysql_free_result($result);

      this made there be about 5 five iframe windows. is there anything else I could do???

        Put the iframe outside of the loop...

        // Set the query as $query, and get the last 10 posts. 
            $query = "SELECT author, message FROM shoutbox order by date";   
        /* Results */
        $result = mysql_query($query);
        print "<iframe>\n"; while($obj = mysql_fetch_object($result)){
        print $obj->author;
        print "<br>\n";
        print $obj->message ;
        } print "</iframe>\n";
        mysql_free_result($result);

        If you don't have to use iframes, I wouldn't...

          one more thing...my script will insert the data into the table but it isn't printing it????

            can someone answer this one for me...it's not printing from the query.

              Sorry, not sure what you mean by 'its not printing it'.

                The INSERT works fine but at the point where it queries and prints it doesn't happen. I've verified the table. Do you have any suggestions on what the problem could be or another way of accomplishing this?

                  Looking at your original code, it looks like you need to fetch the information from the result.

                  // Set the query as $query, and get the last 10 posts. 
                     $query = "SELECT author, message FROM shoutbox order by date";  
                  $result = mysql_query($query);
                  print "<iframe>\n"; while($row=mysql_fetch_row($result)){ //If the subject is the 2nd field in your table, //you will use $row[1] $subject=$row[1]; print "$subject"; } print "</iframe>\n";

                    Make it have id's, then do this:

                    
                    <?php
                    $sql = 'SELECT * FROM shoutbox ORDER BY id DESC LIMIT 0,10';
                    $query = mysql_query($sql) or die("Error with mysql_query!");
                    echo mysql_error();
                    
                    while($info = mysql_fetch_array($query)) {
                    echo $info[author].': '.$author[message].'<BR>';
                    }
                    ?>
                    

                      Thanks everyone for your input. I went with Koopatropa's idea and it works. I did change how it printed by making it print in a textarea. I have two questions to finish it off

                      1. How can I make the textarea bigger?
                      print "<textarea>\n"; 
                         while($info = mysql_fetch_array($query)) { 
                      
                      while($info = mysql_fetch_array($query)) { 
                      echo $info[author].': '.$info[message]."\n"; 
                      	}		
                      	print "</textarea>\n";
                      
                      1. How can I make it refresh or actually not print your comment again if you refresh?
                        1. <textarea rows="5" cols="19">

                        you mean if a user hits the refresh button after they posted something it will repost another shout? I always thought the page would expire so it would not post another.

                          You can use the same trick that this forum is using. You should POST your data to a 3rd page and after writing it into the db, you redirect your user to the display page. This way he can not refresh the 3rd page, because all the process happens too fast!

                            yeah. it will post multiple times if someone refreshes their screen. I figured it would expire too but it isn't.

                            so how does something like that happen? all my php is in my html page. that way it's easier to print the results to my page.

                              It's easy, as you see whenever you post a new message in here, it sends you to a page that say "Thank you for your post and ...." then it redirects you to the show page. So, you can do the same thing. Write a 3rd page for your writing purposes, then submit all your forms to that page and from that page you can redirect to the relevant show page. If you have multiple pages, you can also put a hidden field in your forms that holds the name of the relevant show page that your 3rd page can redirect to the specified show page.

                                if i just want it to kill the process after posting is there no way of doing that?

                                  Of course there are millions of ways! You can set up a session for him when you get his info, the next time you can simply check for the session and if it's set, it means he sent that info. You can also book the time that he submitted the form, and if it's less than a specified time, you don't process his new data. You can also use JS and disable the submit button (via HTML) if it's the 2nd time that he is visiting your form.

                                  But I still think that using a 3rd page is the easiest and of course the best way of doing it. You know, using sessions, your pages get expired and .... I use a 3rd page if I wana code this, me thinks!

                                    with this being a shoutbox/chatbox on an html page that seems like overkill. i wouldn't want it to send them to a page then redirect them back to another one. it should just post then stop. i'm wanting them to be able to post their comment in the shoutbox then if someone else wanted to add something they could...this is more real time....chat back-and-forth without going to another page...the way this forum works is great for that purpose but it doesn't seem very a feasible solution for a chatbox.

                                      Ok, here it's another one: you're writing your data in the database, right? I mean if I come to your shoutbox, you ask my name and my message and then write them in your database. So, I might come to your program and enter "bijan" for my name and then "hello" for my message, so, you take it and put it in your db. Now if I come 3 days later and enter "bijan" and "hello" again, you should write it in your db again, because it's right that I'm writing the same thing, but it's 3 days passed and there are other messages after my old message, so, I'm aloud to enter the same thing again. Now you can do the same thing in here, whenever you want to write something in your db, check for the last record, if the last record contains the same info as what you're trying to enter, then you can be sure that the person hit F5 or something like that.

                                      Of course by doing this you are also preventing ppl to enter something twice delibrately, but then I think it's the easiest way possible (feasible!)

                                        Originally posted by littlab
                                        with this being a shoutbox/chatbox on an html page that seems like overkill. i wouldn't want it to send them to a page then redirect them back to another one. it should just post then stop. i'm wanting them to be able to post their comment in the shoutbox then if someone else wanted to add something they could...this is more real time....chat back-and-forth without going to another page...the way this forum works is great for that purpose but it doesn't seem very a feasible solution for a chatbox.

                                        You need to realize that implementing this 3rd page is virtually transparent. The user would not even be able to tell that there was a middle page, it happens so fast. So don't think that simply because there has to be a 3rd page that you have to say REDIRECTING........blah blah.........

                                        You don't. I say go with the separate 3rd page form as Bijan explained countless times.

                                          Write a Reply...