0

Every time I try to refresh the page I get a new row. I tried to read many posts regarding to this problem, but I couldn't do anything since I'm new in database programming.

I don't know where the value come from, because the same value is repeated over and over.

My code.

<?php
require('connect.php');

$sql="CREATE TABLE test(id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
user VARCHAR(25),message LONGTEXT)";

if($sql==true){
$res=mysql_query( $sql);

}
?>

<?php

$user=null;
$message=null;

if(isset($_POST['user'])){
$user=$_POST['user'];
}
if(isset($_POST['message'])){
$message=$_POST['message'];
}


 if(!empty($_POST)){

if($user&&$message){
$insert=mysql_query("INSERT INTO test(user,message)VALUES('$user','$message')");
}
else{
echo "please fill out the fields";
}
}
?>



<html>
<body>
<form action="database.php" method="post">
<p><label for="user">Name:</label><br/>
<input type="text" name="user" id="user"/></p>
<p><label for="message">Message:</label><br/>
<textarea ="message" name="message"> </textarea></p>
<button type="submit" name="submit" value="send">Send Message:</button>
</form>
 <br/><br/><tr><td>The Users Comments:</td><td><br/><br/>
</html>


<?php
$query=mysql_query("SELECT * FROM test ORDER BY id DESC");
while($row=mysql_fetch_ASSOC($query)){

$name=$row["user"];
$message=$row["message"];

echo "username:",$name,'<br/>'," Messages: ",$message,'<br/>','<br/>';
}

?>
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
user3763103
  • 111
  • 3
  • 13
  • And that "same value" is...? – Paul Roub Jun 21 '14 at 15:11
  • Is this the answer you're searching for : http://stackoverflow.com/questions/4327236/stop-browsers-asking-to-resend-form-data-on-refresh ? – Aurélien Grimpard Jun 21 '14 at 15:14
  • Thank you very much, I'll read it. – user3763103 Jun 21 '14 at 15:17
  • 2
    Sidenote: Your `` contains a space and should read as `` - This will result in a space being inserted into DB right before the text that will be written in the textarea, and may cause problems later on. Plus, it's better to use a header to redirect to another page after submission, including a seperate file for both form and PHP/SQL, which may solve the issue. – Funk Forty Niner Jun 21 '14 at 15:25
  • I noticed a space each time I try to write in textarea window, but I couldn't figure out the problem. Thank you very much user3763103 – user3763103 Jun 21 '14 at 15:30
  • 1
    There ya go, one problem solved ;-) (You're welcome). – Funk Forty Niner Jun 21 '14 at 15:30

4 Answers4

0

Problem is that everytime you refresh the page your browser is re-posting the same data. To workaround it you should consider implementing the Post, Redirect, Get pattern in your page.

Fundamentally this means that upon a successful POST (i.e. row was inserted) you should redirect to another page. This effectively stops the user from having the ability to re-post the same data.

The link above has a good overview of how to implement...

Dean Ward
  • 4,793
  • 1
  • 29
  • 36
0

I don't find a problem in your code. Probably when you post data first time and then refresh the page, data is posted again. Most of the browsers like firefox gets confirmation if browser is re-posting data. Edit: To avoid this you must use redirect to GET method. see this

Shahrzad
  • 1,062
  • 8
  • 26
0

try to edit this

Blockquote

 if(!isset($_POST)){

     if($user && $message){

     $insert=mysql_query("INSERT INTO test(user,message)VALUES($user,$message)");

}

and be sure ctrl+f5 then refresh page

Ashouri
  • 906
  • 4
  • 19
0

Before insert check database if those values are present in database or not like

if(!empty($_POST)) {
    if($user && $message) {
        //check if this user and message in present database or not 
        $query = mysql_query("SELECT * FROM test WHERE user='".$user."' AND message='".$message."'");
        $count = mysql_num_rows($query);

        if ($count > 0 ){ // if $count is greater than 0 means values exists in database then 
            echo "Data exists";
        }
        else {
            // insert 
            $insert=mysql_query("INSERT INTO test(user, message) VALUES ('$user','$message')");
        }
    }
    else {
        echo "please fill out the fields";
    }

it is just a example you can modified it with your requirement :)

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
M.chaudhry
  • 651
  • 1
  • 6
  • 13