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.
Page refresh = duplicate record insertion
Page 1 of 15 Replies - 2170 Views - Last Post: 28 January 2009 - 11:48 AM
Replies To: Page refresh = duplicate record insertion
#2
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:
Thanks.
Post your code like this:
Thanks.
#3
Re: Page refresh = duplicate record insertion
Posted 28 January 2009 - 11:28 AM
BenignDesign, 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:
Thanks.
Post your code like this:
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'> </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.
#4
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
index.php?mysql=done
then use $_GET['mysql'] and if it is equal to done show a message saying it was edited
#5
Re: Page refresh = duplicate record insertion
Posted 28 January 2009 - 11:39 AM
Furnfield, 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
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.
#6
Re: Page refresh = duplicate record insertion
Posted 28 January 2009 - 11:48 AM
jstephens, on 28 Jan, 2009 - 10:28 AM, said:
I am not really looking for code. I am looking for ideas. But here you go.
and yes I know about dream in codes policies.
<?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.
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote









|