Jump to content

[SOLVED] How to stop messages from posting over and over...?


Demont

Recommended Posts

I created a chat script, used with an Iframe...

 

Here is the output of the sumbitted information.

<?php
$con = mysql_connect("localhost","username","password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("my_db", $con);

$result = mysql_query("SELECT poster,chat FROM chat");
while($row = mysql_fetch_array($result))
  {
  echo $row['poster'] ." said ". $row['chat'];
  echo "<br />";
  }
?>

 

And here is the input

 

<iframe 
src ="more_loria.php"
width="100%"
height="80%">
</iframe>

<?php 

// Connects to your Database 
mysql_connect("*****", "*****", "******") or die(mysql_error()); 
mysql_select_db("joshar16_loria") or die(mysql_error()); 

//This code runs if the form has been submitted
if (isset($_POST['submit'])) { 

//This makes sure they did not leave any fields blank
if (($_POST['poster'] == "")||($_POST['chat']=="")) {
die('You did not complete all of the required fields');
}

$itemcheck = $_POST['chat'];
$check = mysql_query("SELECT chat FROM chat WHERE chat = '$chatcheck'") 
or die(mysql_error());
$check2 = mysql_num_rows($check);

//if the name exists it gives an error
if ($check2 != 0) {
die('Sorry, the post '.$_POST['chat'].' already exists.');
}


// now we insert it into the database
$insert = "INSERT INTO chat (chat, poster)
VALUES ('".$_POST['chat']."', '".$_POST['poster']."')";
$add_member = mysql_query($insert);
?>





<?php 
} 
else 
{ 
?>

<?php
}
?> 

<center>
<form action="more.php" method="post">
<table border="0">
<tr><td>Username:</td><td>
<input type="text" name="poster" maxlength="60">
</td></tr>
<tr><td>Message:</td><td>
<input type="text" name="chat" maxlength="99999">
</td></tr>
<tr><th colspan=2><input type="submit" name="submit" value="submit"></th></tr> </table>
</form>
</center>
</body>
</html>

 

It works, but what I don't understand is that each time one of my users refreshes, thier last message shows up again.

 

 

(edited by kenrbnsn to remove users information)

I recommend using mysql_real_escape_string() on all your variables.

 

Also, instead of just checking if the same "words" have been said before, check for the same words said by the same person as well. If you keep it your way, two people can never say the same thing someone else already said.

 

So I would change this

$check = mysql_query("SELECT chat FROM chat WHERE chat = '$chatcheck'") 
or die(mysql_error());

 

To

$poster = mysql_real_escape_string($_POST['poster']);
$check = mysql_query("SELECT chat FROM chat WHERE chat = '$chatcheck' AND poster='$poster'") 
or die(mysql_error());

 

Even then it isn't a great system as the same person wouldn't be able to say something they said in the past.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.