twilitegxa Posted September 12, 2008 Share Posted September 12, 2008 I have this script for a chat, but it's supposed to display the last 20 messages, but an error pops up after the twenty messages, instead of letting more message be submitted. Any suggestions on how I can change it to where it shows all messages, without a limit? Here is the script: <?php include "connect.php"; $getnummessages="SELECT COUNT(*) as messagecount from chatmessages"; $getnummessages2=mysql_query($getnummessages) or die("blah"); $getnummessages3= mysql_result($getnummessages2, 0); if($getnummessages3>21) { $startrow=$getmessages3-20; } else { $startrow=1; } $getmsg="SELECT name, message from chatmessages order by postime ASC limit $startrow,$getnummessages3"; $getmsg2=mysql_query($getmsg) or die(mysql_error()); while($getmsg3=mysql_fetch_array($getmsg2)) { $message=Smiley($message); //Smiley faces print "<font color='red'><b>$getmsg3[name]:</b></font> $getmsg3[message]<br>"; } function Smiley($texttoreplace) { $smilies=array( '' => "<img src='images/smile.gif'>", ':blush' =>"<img src='images/blush.gif'>", ':angry' =>"<img src='images/angry.gif'>", ''=> "<img src='images/shocked.gif'>", 'fuck'=>"$#$%", 'Fuck'=>"&$#@" ); $texttoreplace=str_replace(array_keys($smilies), array_values($smilies), $texttoreplace); return $texttoreplace; } ?> <script> setTimeout("window.location.replace('chatlog.php')",2000); </script> Link to comment https://forums.phpfreaks.com/topic/123879-php-chat/ Share on other sites More sharing options...
twilitegxa Posted September 16, 2008 Author Share Posted September 16, 2008 Or just fix the code so that is doesn't give an error after 20 messages are posted? Link to comment https://forums.phpfreaks.com/topic/123879-php-chat/#findComment-642484 Share on other sites More sharing options...
burn1337 Posted September 16, 2008 Share Posted September 16, 2008 Well I do see a few syntax errors with the sql, or at least from what I've found, starting it in caps and not continuing in caps can lead to errors, or so I've found with my site(same with visa-versa) Link to comment https://forums.phpfreaks.com/topic/123879-php-chat/#findComment-642487 Share on other sites More sharing options...
twilitegxa Posted September 16, 2008 Author Share Posted September 16, 2008 But I would think it would generate an error right from the beginning if this were the problem. It only generates errors after someone enters more than 20 messages. Any other suggestions? I will change the code to lowercase as well for consistency. Link to comment https://forums.phpfreaks.com/topic/123879-php-chat/#findComment-642553 Share on other sites More sharing options...
aschk Posted September 19, 2008 Share Posted September 19, 2008 Just to make clear what Burn is saying, column names can be upper/lower or any combo when referring to them (mysql doesn't care). However table names MUST be the same case as they are set up. i.e. myTable must be written "myTable", NOT "mytable" or "MYTABLE". What you really need twil is to use the LIMIT clause of MySQL, to grab the last 20 messages, or alternatively, you can have a TIMESTAMP column in your table, and do. SELECT * FROM <table name here> ORDER BY <timestamp column name> DESC LIMIT 0,20 The above will select the last 20 messages from the database. Link to comment https://forums.phpfreaks.com/topic/123879-php-chat/#findComment-645498 Share on other sites More sharing options...
twilitegxa Posted September 19, 2008 Author Share Posted September 19, 2008 I am looking at my chatmessages table in my database and it looks liek I have a 'postime' field, but it doesn't appear to be set to a timestamp. In my submit.php page, this is what is pulling the date, from my understanding: $thedate = date("U"); //grab date and time of the post Here is the full code: <?php include "connect.php"; if(isset($_POST['submit'])) //if submit button push has been detected { $message=$_POST['message']; $name=$_POST['name']; if(strlen($message)<1) { print "You did not input a message"; } else if(strlen($name)<1) { print "You did not enter a name, please try again."; } else { $message=strip_tags($message); $IP=$_SERVER["REMOTE_ADDR"]; //grabs poster's IP $checkforbanned="SELECT IP from ipbans where IP='$IP'"; $checkforbanned2=mysql_query($checkforbanned) or die("Could not check for banned IPS"); if(mysql_num_rows($checkforbanned2)>0) //IP is in the banned list { print "Your IP is banned from posting."; } else { $thedate = date("U"); //grab date and time of the post $insertmessage="INSERT into chatmessages (name,IP,postime,message) values('$name','$IP','$thedate','$message')"; mysql_query($insertmessage) or die("Could not insert message"); } } } print "<form action='submit.php' method='post' name='form'>"; print "Your name:<br>"; print "<input type='text' name='name' size='20'><br>"; print "Your message:<br>"; print "<textarea name='message' cols='40' rows='2'></textarea><br>"; print "<a onClick=\"addSmiley(':-)')\"><img src='images/smile.gif'></a> "; //replace images/smile.gif with the relative path of your smiley print "<a onClick=\"addSmiley(':-(')\"><img src='images/sad.gif'></a> "; print "<a onClick=\"addSmiley(';-)')\"><img src='images/wink.gif'></a> "; print "<a onClick=\"addSmiley('>:-(')\"><img src='images/angry.gif'></a> "; print "<a onClick=\"addSmiley('???')\"><img src='images/confused.gif'></a> "; print "<a onClick=\"addSmiley('0:-)')\"><img src='images/angel.gif'></a> "; print "<a onClick=\"addSmiley('8-)')\"><img src='images/cool.gif'></a> "; print "<a onClick=\"addSmiley('X-(')\"><img src='images/dead.gif'></a> "; print "<a onClick=\"addSmiley(':-D')\"><img src='images/grin.gif'></a> "; print "<a onClick=\"addSmiley('(o_o)')\"><img src='images/shocked.gif'></a> "; print "<a onClick=\"addSmiley('')\"><img src='images/kiss.gif'></a> "; print "<a onClick=\"addSmiley(':-P')\"><img src='images/tongue.gif'></a> "; print "<a onClick=\"addSmiley(':-/')\"><img src='images/hmm.gif'></a> "; print "<a onClick=\"addSmiley('`(o_o)')\"><img src='images/tear.gif'></a> "; print "<input type='submit' name='submit' value='submit'></form>"; print "<script language=\"Java Script\" type=\"text/javascript\">\n"; print "function addSmiley(textToAdd)\n"; print "{\n"; print "document.form.message.value += textToAdd;"; print "document.form.message.focus();\n"; print "}\n"; print "</script>\n"; print "<br><br>"; ?> Link to comment https://forums.phpfreaks.com/topic/123879-php-chat/#findComment-645739 Share on other sites More sharing options...
twilitegxa Posted September 19, 2008 Author Share Posted September 19, 2008 This is my chatlog.php: <?php include "connect.php"; $getnummessages="SELECT COUNT(*) as messagecount from chatmessages"; $getnummessages2=mysql_query($getnummessages) or die("blah"); $getnummessages3= mysql_result($getnummessages2, 0); if($getnummessages3>21) { $startrow=$getmessages3-20; } else { $startrow=1; } $getmsg="SELECT name, message from chatmessages order by postime ASC limit $startrow,$getnummessages3"; $getmsg2=mysql_query($getmsg) or die(mysql_error()); while($getmsg3=mysql_fetch_array($getmsg2)) { $message=Smiley($message); //Smiley faces print "<font color='red'><b>$getmsg3[name]:</b></font> $getmsg3[message]<br>"; } function Smiley($texttoreplace) { $smilies=array( '' => "<img src='images/smile.gif'>", ':blush' =>"<img src='images/blush.gif'>", ':angry' =>"<img src='images/angry.gif'>", ''=> "<img src='images/shocked.gif'>", 'fuck'=>"$#$%", 'Fuck'=>"&$#@" ); $texttoreplace=str_replace(array_keys($smilies), array_values($smilies), $texttoreplace); return $texttoreplace; } ?> <script> setTimeout("window.location.replace('chatlog.php')",2000); </script> Where would I put the code you suggested, aschk? SELECT * FROM <table name here> ORDER BY <timestamp column name> DESC LIMIT 0,20 Or what would have to be changed in my script to do this? Link to comment https://forums.phpfreaks.com/topic/123879-php-chat/#findComment-645914 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.