repeat last item on refresh

this is my code


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Addvert Information</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<?php
$link = mysql_connect('localhost', '', '');
if (!$link) {
   die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
?>
<?php
mysql_connect("localhost", "", "") or die( "Unable to select database" . mysql_error());
mysql_select_db("orderform")or die("Could not select database");

if (isset($_POST['Submit'])) {
 $sql = "INSERT INTO test(date,media,add,edit,name) VALUES ('".$_POST['date']."','".$_POST['media']."','".$_POST['add']."','".$_POST['edit']."','".$_POST['name']."')";
 $result = mysql_query($sql) or die(mysql_error());
}
?>
</head>

<body>
<table width="100%" border="1">
  <tr>
    <td width="1056"><table width="100%" border="1">
      <tr>
        <td bgcolor="#0099FF"><form name="form1" method="post" action="">
          <div align="center">Date :
              <input name="date" type="text" id="date">
  Media Format :
  <input name="media" type="text" id="media">  
  Cost Of Add :
  <input name="add" type="text" id="add">
   Cost Of Edit :
   <input name="edit" type="text" id="edit">
   Name :
   <input name="name" type="text" id="name">
   <input type="submit" name="Submit" value="Submit">
          </div>
        </form></td>
      </tr>
      <tr>
        <td height="134" bgcolor="#0099FF"> <table width="100%" border="1">
          <tr>
            <td>ID</td>
            <td>Date</td>
            <td>Media Format </td>
            <td>Cost Of Add (&pound;) </td>
            <td>Cost Of Edit (&pound;) </td>
            <td>Name</td>
          </tr>
              <?php
              mysql_connect("localhost", "", "") or die( "Unable to select database" . mysql_error());
mysql_select_db("orderform")or die("Could not select database");
             
              $result = mysql_query("SELECT * FROM test")
                  or die(mysql_error());

        echo "<tr><td>";
        echo $row['id'];
        echo "</td><td>";
      echo $row['date'];
        echo "</td><td>";
        echo $row['media'];
        echo "</td><td>";
        echo $row['add'];
        echo "</td><td>";
        echo $row['edit'];
        echo "</td><td>";
        echo $row['name'];
        ?>
                  </td></tr>
        </table>          </td>
      </tr>
    </table></td>
  </tr>
</table>
</body>
</html>


the question is

when i refresh the page the last item that was submited is submited again every time i reload dose any one see what the problem is?
ace5342Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

geir_andersenSystems engineerCommented:
if you've submitted the form once, everytime you reload/refresh that page, you'll also resend the POSTDATA

-Geir
WoodyRoundUpCommented:
it's typical browser.
they will normally asked you whether you want to resubmit the form.
by doing so, that means that you are resubmitting the forms.
it will be better if we redirect the page to another page once the form is submitted.
or you could use a specific php file to process the request.
and then you redirect the page to another location (your main location)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
geir_andersenSystems engineerCommented:
you use:
if (isset($_POST['Submit'])) {
which is correct and since the page is reloaded the POSTDATA is caught everytime..

you could check if there exists such a row before inserting it:

if (isset($_POST['Submit'])) {
$result_check =  mysql_query("select date, media, add, edit, name from test where date = '{$_POST['date']}' and media = '{$_POST['media']}' and add = '{$_POST['add']}' and edit = {$_POST['edit']} and name = '{$_POST['name']}'") or die("Query error: " . mysql_error());
if (mysql_num_rows($result_check) == 0)) {
$sql = "INSERT INTO test(date,media,add,edit,name) VALUES ('".$_POST['date']."','".$_POST['media']."','".$_POST['add']."','".$_POST['edit']."','".$_POST['name']."')";
 $result = mysql_query($sql) or die(mysql_error());
}
}


Using this, the posted values will only be inserted if the record doesn't already exist.
NOTE! if you want to be able to have two similar records, you can't use this code...

-Geir
Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

geir_andersenSystems engineerCommented:
error in my code: (was an extra parenthesis..)

should be:
if (isset($_POST['Submit'])) {
$result_check =  mysql_query("select date, media, add, edit, name from test where date = '{$_POST['date']}' and media = '{$_POST['media']}' and add = '{$_POST['add']}' and edit = {$_POST['edit']} and name = '{$_POST['name']}'") or die("Query error: " . mysql_error());
if (mysql_num_rows($result_check) == 0) {
$sql = "INSERT INTO test(date,media,add,edit,name) VALUES ('".$_POST['date']."','".$_POST['media']."','".$_POST['add']."','".$_POST['edit']."','".$_POST['name']."')";
 $result = mysql_query($sql) or die(mysql_error());
}
}

-Geir
ace5342Author Commented:
as i have an id in there with auto incresment would this all code work if everything was the same except the auto id
WoodyRoundUpCommented:
yup. this code won't add your item again.

but, if you don't want it to be strict, like you can add same item again, then the code will never add the item once you refresh.
geir_andersenSystems engineerCommented:
If you're refering to the code I wrote, then no.
This code will not work if you use the auto_incremented ID field.
My code simply checks if such a record exists. If so... don't add a new one.

If you want to be able to add more rows later, but not right now... (best explanation I could come up with now)
I suggest adding a field call f.ex: ts (short for timestamp)
Then say: if ts of last similar row is older than 5minutes, OK add another row.

Best suggestion I can come up with now.

-Geir
geir_andersenSystems engineerCommented:
Also:
Have a look at this thread: http://www.experts-exchange.com/Web/Web_Languages/PHP/Q_21481938.html
Diablo84's last post is a simple and easy fix at this.
Basically what he says is:

After the form/POSTDATA has been processed once, redirect to the same page.
This will clear the POSTDATA, and stop addition of more records.

To account for this, your code will be:
if (isset($_POST['Submit'])) {
$sql = "INSERT INTO test(date,media,add,edit,name) VALUES ('".$_POST['date']."','".$_POST['media']."','".$_POST['add']."','".$_POST['edit']."','".$_POST['name']."')";
 $result = mysql_query($sql) or die(mysql_error());
  header("location: {$_SERVER['PHP_SELF']}");
}

-Geir
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.