5 Replies - 3308 Views - Last Post: 28 January 2009 - 11:48 AM Rate Topic: -----

#1 jstephens  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 214
  • Joined: 07-November 05

Page refresh = duplicate record insertion

Posted 28 January 2009 - 11:06 AM

Hey guys, I have a script that enters data into mysql. When someone hits refresh the page inserts the data into the database again. How can I stop this? some people suggest that I link them to the Id but I want the user to be able to continuously add records. I also found a reference to add the following but that did not work either. header('Cache-Control: private'); was the reference. Does anyone have any good ideas? I thought I might just redirect them back to the page but then I was not sure How I could show them the record was added.

Is This A Good Question/Topic? 0
  • +

Replies To: Page refresh = duplicate record insertion

#2 BenignDesign  Icon User is offline

  • holy shitin shishkebobs
  • member icon




Reputation: 6180
  • View blog
  • Posts: 10,681
  • Joined: 28-September 07

Re: Page refresh = duplicate record insertion

Posted 28 January 2009 - 11:25 AM

Dream.In.Code has a policy by which we prefer to see a good faith effort on your part before providing source code for homework assignments. Please post the code you have written in an effort to resolve the problem, and our members would be happy to provide some guidance. Be sure to include a description of any errors you are encountering as well.

Post your code like this: :code:

Thanks.
Was This Post Helpful? 0
  • +
  • -

#3 jstephens  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 214
  • Joined: 07-November 05

Re: Page refresh = duplicate record insertion

Posted 28 January 2009 - 11:28 AM

View PostBenignDesign, on 28 Jan, 2009 - 12:25 PM, said:

Dream.In.Code has a policy by which we prefer to see a good faith effort on your part before providing source code for homework assignments. Please post the code you have written in an effort to resolve the problem, and our members would be happy to provide some guidance. Be sure to include a description of any errors you are encountering as well.

Post your code like this: :code:

Thanks.


I am not really looking for code. I am looking for ideas. But here you go.
<?php
session_start();
include('common/common.php');
include('library/corefunctions.php');

if(!isset($_SESSION['auth'])) {header('Location: index.php');}

if(array_key_exists('name', $_POST))
{
	if($_POST['name'] != '' && $_POST['username'] !='' && $_POST['password'] != '')
	{
		$name = trim($_POST['name']);
		$username = trim($_POST['username']);
		$password = trim($_POST['password']);
		$desc = trim($_POST['desc']);
		$notes = trim($_POST['notes']);
		$id = $_SESSION['id'];
		
		if(InsertRecord($name, $desc, $username, $password, $notes, $id))
		{
			
		}
		else
		{
			$error = '<br /><span class="error">Record could not be added to database. Please try again later.<br />';
		}
	}
	else {
		$error = '<br /><span class="error">You must fill in the Name, Username, and Password fields.</span><br />';
	}
}
?>
<html>
 <head>
  <title>Globalink Password Manager</title>
  <link rel='stylesheet' href='common/cs/formstyle.css' media='all' type='text/css' />
 </head>
 <body>
 <h1>Globalink Solutions Password Database New Record Form</h1>
 <h4>* Denotes Required Field.</h4>
  <p>
	Notes: If the username or password needs to be blank please enter none
	<?php if(isset($error)) { echo $error; } ?>
  </p>
  <form id='add_form' method='post' action=''>
	 <fieldset class='fieldset'>
	  <legend>Add Record</legend>
		<p>
		 <label for='name'>* Name / URL</label>
		 <input type='text' name='name' id='name' class='textfield' />
		</p>
		<p>
		 <label for='description'>Description</label>
		 <input type='text' name='desc' id='desc' class='textfield' />
		</p>
		<p>
		 <label for='username'>* Username</label>
		 <input type='text' name='username' id='username' class='textfield' />
		</p>
		<p>
		 <label for='password'>* Password</label>
		 <input type='text' name='password' id='password' class='textfield' />
		</p>
		<p>
		 <label for='notes'>Notes</label>
		 <textarea rows='8' cols='55' id='notes' name='notes' class='textarea'></textarea>
		</p>
		<p>
		 <label for='submit'>&nbsp;</label>
		 <input type='submit' name='add' id='add' value='Add Record' class='submit' />
		</p>
	</fieldset>
	</form>
	| <a href='logout.php'>Logout</a> | 
	<a href='search.php'>Search Records</a> |
	<a href='add.php'>Add Record</a> | 
 </body>
</html>



and yes I know about dream in codes policies.
Was This Post Helpful? 0
  • +
  • -

#4 rmccarter721  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 8
  • View blog
  • Posts: 139
  • Joined: 10-September 07

Re: Page refresh = duplicate record insertion

Posted 28 January 2009 - 11:34 AM

send the form to another page, do the code there, redirect it back to the main page again with a url such as

index.php?mysql=done

then use $_GET['mysql'] and if it is equal to done show a message saying it was edited
Was This Post Helpful? 0
  • +
  • -

#5 jstephens  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 214
  • Joined: 07-November 05

Re: Page refresh = duplicate record insertion

Posted 28 January 2009 - 11:39 AM

View PostFurnfield, on 28 Jan, 2009 - 12:34 PM, said:

send the form to another page, do the code there, redirect it back to the main page again with a url such as

index.php?mysql=done

then use $_GET['mysql'] and if it is equal to done show a message saying it was edited


That is what I was thinking I was going to have to do. I was trying to search for other alternatives before that but I really is starting to look like that is my only choice.
Was This Post Helpful? 0
  • +
  • -

#6 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3034
  • Posts: 10,590
  • Joined: 08-August 08

Re: Page refresh = duplicate record insertion

Posted 28 January 2009 - 11:48 AM

View Postjstephens, on 28 Jan, 2009 - 10:28 AM, said:

I am not really looking for code. I am looking for ideas. But here you go.
<?php
session_start();
include('common/common.php');
include('library/corefunctions.php');

if(!isset($_SESSION['auth'])) {header('Location: index.php');}

if(array_key_exists('name', $_POST))
{
	if($_POST['name'] != '' && $_POST['username'] !='' && $_POST['password'] != '')
	{
		$name = trim($_POST['name']);
		$username = trim($_POST['username']);
		$password = trim($_POST['password']);
		$desc = trim($_POST['desc']);
		$notes = trim($_POST['notes']);
		$id = $_SESSION['id'];
		
		if(InsertRecord($name, $desc, $username, $password, $notes, $id))
		{
			
		}
		else
		{
			$error = '<br /><span class="error">Record could not be added to database. Please try again later.<br />';
		}
	}
	else {
		$error = '<br /><span class="error">You must fill in the Name, Username, and Password fields.</span><br />';
	}
}
?>
<html>
 <head>
  <title>Globalink Password Manager</title>
  <link rel='stylesheet' href='common/cs/formstyle.css' media='all' type='text/css' />
 </head>
 <body>
 <h1>Globalink Solutions Password Database New Record Form</h1>
 <h4>* Denotes Required Field.</h4>
  <p>
	Notes: If the username or password needs to be blank please enter none
	<?php if(isset($error)) { echo $error; } ?>
  </p>
  <form id='add_form' method='post' action=''>
	 <fieldset class='fieldset'>
	  <legend>Add Record</legend>
		<p>
		 <label for='name'>* Name / URL</label>
		 <input type='text' name='name' id='name' class='textfield' />
		</p>
		<p>
		 <label for='description'>Description</label>
		 <input type='text' name='desc' id='desc' class='textfield' />
		</p>
		<p>
		 <label for='username'>* Username</label>
		 <input type='text' name='username' id='username' class='textfield' />
		</p>
		<p>
		 <label for='password'>* Password</label>
		 <input type='text' name='password' id='password' class='textfield' />
		</p>
		<p>
		 <label for='notes'>Notes</label>
		 <textarea rows='8' cols='55' id='notes' name='notes' class='textarea'></textarea>
		</p>
		<p>
		 <label for='submit'> </label>
		 <input type='submit' name='add' id='add' value='Add Record' class='submit' />
		</p>
	</fieldset>
	</form>
	| <a href='logout.php'>Logout</a> | 
	<a href='search.php'>Search Records</a> |
	<a href='add.php'>Add Record</a> | 
 </body>
</html>



and yes I know about dream in codes policies.


The problem is with this line:
if(InsertRecord($name, $desc, $username, $password, $notes, $id))
Instead of inserting a record using data that may already exist in the database, you should be checking to see if it exists there first!

I hope you now see why it's important to post code when looking for ideas. ;)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1