Log in

View Full Version : How to prevent duplicate record insertion while refresh the php page?



theteche
06-13-2008, 05:34 AM
Hi,

I am new to php. :confused: I have form(code is below). If you click the submit button,it adds one record to the database..working fine.but after submitting,if you hitting the refresh button,it adds the same record again to the database.How to avoid that duplicate data insertion.please help me to solve this issue.And provide some code example.
Note: after submit the form,it should display the empty form. Thanks in advance.
Here is my code


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>

<?php

if($_POST['name'] == '' || $_POST['id'] == '')
{


$message="page refresh";
echo $message;

}
else
{
$user = "root";
$host="localhost";
$pass = "admin";
$db = "mydatabase";
$table = "customer";
$con=mysql_connect($host,$user,$pass) or die( "DB connection error:".mysql_error());
$dbcon=mysql_select_db($db,$con) or die ( "Db Selection error:".mysql_error());


$name=$_POST['name'];
$id=$_POST['id'];
$sql="insert into $table values ('$name','$id')";
$result=mysql_query($sql) or die(" insert error :".mysql_error());
}
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>" name="form1">
<table width="100%" border="1">
<tr>
<td>Employee Name</td>
<td width="2%">:</td>
<td ><input type="text" name="name" id="textfield" /></td>
<td>Employee No</td>
<td width="2%">:</td>
<td><label>
<input type="text" name="id" id="textfield2" />
</label></td>
</tr>

<tr>
<td colspan="6" align="center"><input type="submit" name="name1" value="register" />
<input type="submit" name="id" value="cancel" /></td>
</tr>
</table>
</form>



</body>
</html>

subesh
06-13-2008, 06:01 AM
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>

<?php

if($_POST){
$user = "root";
$host="localhost";
$pass = "admin";
$db = "mydatabase";
$table = "customer";
$con=mysql_connect($host,$user,$pass) or die( "DB connection error:".mysql_error());
$dbcon=mysql_select_db($db,$con) or die ( "Db Selection error:".mysql_error());


$name=$_POST['name'];
$id=$_POST['id'];
$sql="insert into $table values ('$name','$id')";
$result=mysql_query($sql) or die(" insert error :".mysql_error());

if($result) echo "Successful"; else echo "Not Successful";
}
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>" name="form1">
<table width="100%" border="1">
<tr>
<td>Employee Name</td>
<td width="2%">:</td>
<td ><input type="text" name="name" id="textfield" /></td>
<td>Employee No</td>
<td width="2%">:</td>
<td><label>
<input type="text" name="id" id="textfield2" />
</label></td>
</tr>

<tr>
<td colspan="6" align="center"><input type="submit" name="name1" value="register" />
<input type="submit" name="id" value="cancel" /></td>
</tr>
</table>
</form>



</body>
</html>


This should help.. you if you have problem do post....

Moderator Edit: No self-promotion, please.

theteche
06-13-2008, 07:22 AM
hi subesh,

Thanks for your code.It inserts the record in to db.But if you refresh the page,it adds the same record again...How to solve this?

biggles1979
06-13-2008, 10:37 AM
Hi,

And a header redirect after the insertion:



<?php

if($_POST){
$user = "root";
$host="localhost";
$pass = "admin";
$db = "mydatabase";
$table = "customer";
$con=mysql_connect($host,$user,$pass) or die( "DB connection error:".mysql_error());
$dbcon=mysql_select_db($db,$con) or die ( "Db Selection error:".mysql_error());


$name=$_POST['name'];
$id=$_POST['id'];
$sql="insert into $table values ('$name','$id')";
$result=mysql_query($sql) or die(" insert error :".mysql_error());

if($result) {

header("Location: form.php");

}else{ echo "Not Successful"; }
}
?>


NOTE: php must be before the html as the header won't work if you have started output.



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>

etc...