• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 257
  • Last Modified:

Database tries to add last entry upon page refresh

Hi,

I am trying out the script given in the solution here shown here:

http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_24005839.html?sfQueryTermInfo=1+10+30+abl+add+edit+want

I noticed that after selecting a button then when refreshing a page it will try to recall the last command.  However, this doesn't happens on inital page load.

For example, once I loaded up the page and add a record then when I refreshed the page it will try to add the record again.

I cannot seem to pinpoint why this is happening and how to correct it.  Hope someone can shed some light on this and provide a solution.

Thank you.
0
Wayne88
Asked:
Wayne88
  • 2
2 Solutions
 
hieloCommented:
try:
<?php
session_start();
mysql_connect('localhost','****','****');
mysql_select_db('****');
$tablename = 'arraysg2008';
$msg=''; 
function get_field($fld) {
  if(!isset($_POST[$fld])) return '';         # field not posted
  $value = $_POST[$fld];
  if(get_magic_quotes_gpc())                  # did php add slashes?
    $value = stripslashes($value);            # yes, remove them
  $value = mysql_real_escape_string($value);  # add mysql slashes
  return $value;
}
 
$row_id = get_field('row_id'); 
$firstname = get_field('firstname');
$lastname = get_field('lastname');
$department = get_field('department');

if($_POST['user']=='Search') {
  $res = mysql_query(
    "select * from $tablename where
     firstname like '$firstname%' and
     lastname like '$lastname%' and
     department like '$department%'
     order by id");
  if($res) {
    $row = mysql_fetch_assoc($res);
    if($row) {
      $row_count = mysql_num_rows($res);
      if($row_count > 1) $msg = $row_count.' rows found, showing the first';
      $row_id=$row['id'];
      $firstname=$row['firstname'];
      $lastname=$row['lastname'];
      $department=$row['department'];
    } else $msg = 'No rows found';
  } else $msg = 'query failed: '.mysql_error();
}
 
switch ($_POST['user']) {        
  case 'Add':
    $res = mysql_query(
      "insert into $tablename set 
       firstname='$firstname',
       lastname='$lastname',
       department='$department'");
     $msg = $res ? 'Record was added' : 'Error: '.mysql_error();
     break;
        
  case 'Update':
    $res = mysql_query(
      "update $tablename set 
       firstname='$firstname',
       lastname='$lastname',
       department='$department'
       where id=$row_id");
    $msg = $res ? 'Record was updated' : 'Error: '.mysql_error();
    break;
 
  case 'Delete':
    $res = mysql_query(
      "delete from $tablename 
       where id=$row_id");
    $msg = $res ? 'Record was removed' : 'Error: '.mysql_error();
    break;
}
if(!empty($_POST) && !empty($msg))
{
	$_SESSION['msg']=$msg;
	header($_SERVER['PHP_SELF']);
	exit();
}
?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Employee editing</title>
</head>
<body>
<form action="<?php echo $_SERVER['SCRIPT_NAME']?>" method="post"> 
<input type="hidden" name="row_id" value="<?php echo $row_id; ?>" />
First Name: <input type="text" name="firstname" value="<?php echo $firstname; ?>" size="15"> <br>
Last Name: <input type="text" name="lastname" value="<?php echo $lastname; ?>" size="15"> <br>
Department: <input type="text" name="department" value="<?php echo $department; ?>" size="15"> <br>
<input type="submit" name="user" value="Add">
<input type="submit" name="user" value="Update">
<input type="submit" name="user" value="Search">
<input type="submit" name="user" value="Delete">
 </form>
 
<?php
if(isset($_SESSION['msg']) && !empty($_SESSION['msg']))
{
  echo '<p style="color:red">'.$_SESSION['msg'].'</p>';
  $_SESSION['msg']='';
} 
?>
</body>
</html>

Open in new window

0
 
Dave BaldwinFixer of ProblemsCommented:
Because you're telling it to.  Refreshing a page causes the browser to do ALL of the things it did to load the page the first time including sending data to the server.  It doesn't just 'refresh' the display.
0
 
Wayne88Author Commented:
thanks Hielo, I used  your solution with some modifications.
0
 
hieloCommented:
You are welcome Wayne. Glad to help.

Regards,
Hielo
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now