I am 12 and need help with my code, when I press the refresh button it reposts the data into the database and utmately reposts the comment. This could be bad because if someone (Don't Even Think About Doing This) presses the refresh button a bunch (And I Mean A Bunch) it could fill up my server then bring down my bandwidth. Here is my code
[CODE=php]<html>
<?php
class formValidation{
function checkLength($st ring, $min, $max, $awnser) {
$length = strlen($string) ;
if($length < $min) {
die("The $awnser is to short");
}
if($length >$max) {
die("The $awnser is to long");
}
}
}
//////////////////////////////////////////////////////////////////////////
include('loginS QL.php');
$connection = mysql_connect($ db_host, $db_username, $db_password);
if(!connection) {
die ("Could not connect to the database:<br>") ;
};
$select = mysql_select_db ($db_database);
if(!$select){
die("Could not select the database.<br./>");
};
$query = "SELECT * FROM comments";
$result = mysql_query($qu ery);
if(!$result){
die("Could not execute the query <br>".mysql_err or());
};
if(!is_null($_P OST['name'])) {
formValidation: :checkLength($_ POST['name'],2,50,name);
$name = $_POST['name'];
formValidation: :checkLength($_ POST['comment'],0,500,comment) ;
$comment = $_POST['comment'];
$date = date("F/j/Y");
$time = date("g:i:A");
$query = "INSERT INTO comments VALUES ('$name', '$comment', '$time', '$date');";
$result = mysql_query($qu ery);
if(!$result){
die("Could not insert the comment".mysql_ error());
};
$query = "SELECT * FROM comments";
$result = mysql_query($qu ery);
if(!$result){
die("Could not execute the query <br>".mysql_err or());
};
};
$_POST['name'] = NULL;
?>
<font size = "5" color = "Grey"> Comments </font>
<hr>
<?php
while ($result_row = mysql_fetch_row (($result))){
echo $result_row[0];
echo ' Left this comment on ';
echo $result_row[3];
echo ' at ';
echo $result_row[2];
echo '<br>';
echo $result_row[1];
echo '<hr>';
};
?>
<hr>
<font size ="5" color = "Grey"> Leave A Comment!</font><br>
<form action = "comments.p hp" method = "post">
Name<br>
<input = "text" name = "name"><br>
<textarea cols = "40" rows = "5" name = "comment">
Type your comment here!
</textarea><br>
<input type = "submit">
</form>
<hr>
</html>
<!--
Problems
When Pressing The Refresh Button It Posts The Comment Again
-->[/CODE]
Thanks for all the help, and by the way the link is here link
It also does not do the date correctly but I can fix that.
Thanks,
Matt
[CODE=php]<html>
<?php
class formValidation{
function checkLength($st ring, $min, $max, $awnser) {
$length = strlen($string) ;
if($length < $min) {
die("The $awnser is to short");
}
if($length >$max) {
die("The $awnser is to long");
}
}
}
//////////////////////////////////////////////////////////////////////////
include('loginS QL.php');
$connection = mysql_connect($ db_host, $db_username, $db_password);
if(!connection) {
die ("Could not connect to the database:<br>") ;
};
$select = mysql_select_db ($db_database);
if(!$select){
die("Could not select the database.<br./>");
};
$query = "SELECT * FROM comments";
$result = mysql_query($qu ery);
if(!$result){
die("Could not execute the query <br>".mysql_err or());
};
if(!is_null($_P OST['name'])) {
formValidation: :checkLength($_ POST['name'],2,50,name);
$name = $_POST['name'];
formValidation: :checkLength($_ POST['comment'],0,500,comment) ;
$comment = $_POST['comment'];
$date = date("F/j/Y");
$time = date("g:i:A");
$query = "INSERT INTO comments VALUES ('$name', '$comment', '$time', '$date');";
$result = mysql_query($qu ery);
if(!$result){
die("Could not insert the comment".mysql_ error());
};
$query = "SELECT * FROM comments";
$result = mysql_query($qu ery);
if(!$result){
die("Could not execute the query <br>".mysql_err or());
};
};
$_POST['name'] = NULL;
?>
<font size = "5" color = "Grey"> Comments </font>
<hr>
<?php
while ($result_row = mysql_fetch_row (($result))){
echo $result_row[0];
echo ' Left this comment on ';
echo $result_row[3];
echo ' at ';
echo $result_row[2];
echo '<br>';
echo $result_row[1];
echo '<hr>';
};
?>
<hr>
<font size ="5" color = "Grey"> Leave A Comment!</font><br>
<form action = "comments.p hp" method = "post">
Name<br>
<input = "text" name = "name"><br>
<textarea cols = "40" rows = "5" name = "comment">
Type your comment here!
</textarea><br>
<input type = "submit">
</form>
<hr>
</html>
<!--
Problems
When Pressing The Refresh Button It Posts The Comment Again
-->[/CODE]
Thanks for all the help, and by the way the link is here link
It also does not do the date correctly but I can fix that.
Thanks,
Matt
Comment