Page 1 of 1

problem with rows being added on refresh

Posted: Sun Jun 01, 2003 5:18 pm
by CoreLEx
Hello,

I've got a simple script that adds data to a table and then retrieves it on the same page. The problem is that when I refresh the page, the last row gets re-added to the table. Here's the script:

Code: Select all

<html>
<head><title>MySQL Test</title></head>
<body>

<form action="<? echo($PHP_SELF); ?>" method="post">
    Author: <input type="text" name="author" size="10"><br>
    Title: <input type="text" name="title" size="10"><br>
    Art: <input type="text" name="art" size="30"><br>
    <input type="submit" value="Submit!">
</form>

<?php

#--------------
# submit data
#--------------

if($title and $art)
&#123;
    #connect to mysql
    $connection = @mysql_connect("localhost","user","pass")
        or die("Sorry, unable to connect to MySQL");

    #select db
    $result = @mysql_select_db(asciiart,$connection)
        or die("Sorry, unable to to get database.  Maybe it's because you have big head");

    #create query
    $sql="insert into asciiart(author,title,art) values("$author", "$title", "$art")";

    #execute query (result is bool)
    $result=mysql_query($sql,$connection);

    #success?
    if(result)&#123;
        echo("Record added successfully!  $id $title $art");
    &#125;
    else&#123;
        echo("Record was not added successfully!");
    &#125;
&#125;

#--------------
# retrieve data
#--------------

#connect to mysql
$connection = @mysql_connect("localhost","cyberiapc","pass123")
    or die("Sorry, unable to connect to MySQL");

#select db
$result = @mysql_select_db(asciiart,$connection)
    or die("Sorry, unable to to get database.");

#create query
$sql="select author,title,art from asciiart";

#execute query (result is bool)
$result=mysql_query($sql,$connection);

#write the data
while($currentRow = mysql_fetch_array($result))
&#123;
    echo("<b><font size="1" face="verdana"");
    echo("<br><br>Posted by " .$currentRow&#1111;"author"] ."<br>");
    echo("Title " .$currentRow&#1111;"title"] ."<br>");
    echo($currentRow&#1111;"art"] ."<br>");
    echo("</font></b>");
&#125;



?>

</body>
</html>
Would appreciate any help.

Posted: Mon Jun 02, 2003 12:58 am
by delorian
It will always add new record to the database when you refresh the page. When you are refreshing it after sendig a post data, the browser still has the form data in its cache and tries to send it second time. The simplest thing to do is to create a php script that will control the record addition. After posting your data you should use javascript history.back(), or oridinary link to return to the page with form and retrive data section. But it isn't neccessary, you can allways redirect (but not refresh) to the same page after a few seconds.

The other thing is that you are using register globals. Try to switch it off, and use global variables tables like $_POST http://www.php.net/manual/en/language.v ... efined.php. They are a lot better and more secure.

Posted: Mon Jun 02, 2003 2:35 am
by []InTeR[]
I using headers for that, redirect.

Code: Select all

if(result){ 
  header("Location: script.php?msg=Record+added+succesfuly!");
} 
else{ 
  header("Location: script.php?msg=Record+was+not+added+successfully");
}
if(isset($_GET["msg"])){
  echo $msg;
}
Something like this will solve your problem.