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>
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>