Link to home
Start Free TrialLog in
Avatar of ace5342
ace5342

asked on

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?
Avatar of geir_andersen
geir_andersen
Flag of Norway image

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

-Geir
ASKER CERTIFIED SOLUTION
Avatar of WoodyRoundUp
WoodyRoundUp

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
Avatar of ace5342
ace5342

ASKER

as i have an id in there with auto incresment would this all code work if everything was the same except the auto id
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.
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
Also:
Have a look at this thread: https://www.experts-exchange.com/questions/21481938/POSTed-Variables.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