Jump to content

Recommended Posts

Hi all,

I am trying to add data from 'form' to database using 'Insert statement'

 

I am using 'POST' method for form.

 

After hitting Submit button its add one record to database but as i refresh the page its adding same row again.

 

I use 'GET' instead of 'POST' but same prob.

my code is:

 

<html>

<head>

<title>

Contact Us

</title>

<link rel="stylesheet" type="text/css" href="style/myStyle.css"/>

 

<script language="JavaScript" src="style/formValidation.js" type="text/javascript"></script>

 

<script type="text/javascript">

</script>

</head>

<body>

<div id="pageSize">

<div id="contactusHeading">

Contact Us

</div>

<div id="contContainer">

<div id="contactusImage">

</div>

 

<div id="contactForm">

<form action="contactUs.php" name="myfrm" method="get" onsubmit="return validatefrm()">

<table id="forForm" cellpadding="2">

<tr>

<td>Name:</td>

<td><input type="text" name="name"></input></td>

</tr>

<tr>

<td>Company Name:</td>

<td><input type="text" name="companyName"></input></td>

</tr>

<tr>

<td>E-mail:</td>

<td><input type="text" name="email"></input></td>

</tr>

<tr>

<td>About:</td>

<td><input type="text" name="about"></input></td>

</tr>

<tr>

<td>Message:</td>

<td><input type="text" name="message"></input></td>

</tr>

<tr>

<td colspan="2" align="center">

<input type="submit" value="Submit" name="butSubmit"></input></td>

</tr>

</table>

</form>

</div>

</div>

 

<?php

 

 

$name=$_GET['name'];

$compantName=$_GET['companyName'];

$email=$_GET['email'];

$about=$_GET['about'];

$message=$_GET['message'];

 

echo "<script type=text/javascript>";

echo "document.myfrm.reset()";

echo "</script>";

 

$hostname="localhost";

$db_user="root";

$db_password="admin";

$database="myproject";

$db_table="contactus";

 

$con=mysql_connect($localhost,$db_user,$db_password);

if(!con)

{

die('Error in connection:'.mysql_error());

}

 

mysql_select_db($database,$con);

 

if($_GET['butSubmit']==true)

{

$sql="INSERT INTO $db_table (Name, Company_name, Email, About, Message) VALUES ('$name', '$compantName', '$email', '$about', '$message')";

if(!mysql_query($sql,$con))

{

die('Error:' .mysql_error());

}

echo "<div id=newRecord> New record added </div><br/>";

 

$result=mysql_query("SELECT * FROM $db_table ORDER BY id DESC ");

 

 

unset($_GET['name']);

unset($_GET['companyName']);

unset($_GET['email']);

unset($_GET['about']);

unset($_GET['message']);

 

}

$result=mysql_query("SELECT * FROM $db_table ORDER BY id DESC ");

$rowcount=mysql_num_rows($result);

$i=0;

echo "<br/>";

echo "<table id=contTable>

<tr id=tableHeading>

<th>Name</th>

<th>Company Name</th>

<th>Email</th>

<th>About</th>

<th>Message</th>

</tr>";

 

 

while($row=mysql_fetch_array($result))

{

 

if($i & 1)

{

echo "<tr id=oddRow>";

}

else

{

echo "<tr id=evenRow>";

}

$i++;

echo "<td>" . $row['Name']. "</td>";

echo "<td>" . $row['Company_name']. "</td>";

echo "<td>" . $row['Email'] . "</td>";

echo "<td>" . $row['About'] . "</td>";

echo "<td>" .$row['Message'] . "</td>";

echo "</tr>";

}

echo "</table>";

echo "<br/>";

 

if($result){

header("Location:contact.php");

}else

{

echo "Not Successful";

}

 

 

 

mysql_close($con);

?>

 

</div>

 

</body>

</html>

Couple of things you could do.

 

1. redirect the page back to itself after processing (without the GET params of course).

2. Hit the database prior to processing, to see if that exact row is already there, (query the database with every single bit of info you are fixing to put there, returns a row it is there).

 

Either/or, and I'm sure there are other fine examples out there, these are just the most common that I see.

On the processing page that receives the form submission you simply need to do a header redirect to a confirmation page or even back to the same page. That will "wipe out" the form data. So, if a user refreshes the page they will simply see the confirmation page and no processing takes place.

 

//Process the form data
//Processing logic goes here

//After processing logic completes redirect to confirmation page
header("Location: confirmation_page.php");

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.