More than 1 record inserted

Codes here !

Moderators: macek, egami, gesf

User avatar
wmdrumaizi
New php-forum User
New php-forum User
Posts: 40
Joined: Thu Jan 23, 2003 5:15 pm
Location: Malaysia
Contact:

More than 1 record inserted

Postby wmdrumaizi » Mon Mar 31, 2003 12:14 am

Hi everybody,

I'm developing a registration form using php and mysql.
I have no problem with insert command(it works, the record can be inserted) but the problem is 2 same records will be inserted when I press submit button. When I press refresh button, that problem also happen, could someone tell me what is wrong with my form.

Anyhelp and advice would be appreciated....
Bye

User avatar
mammal
New php-forum User
New php-forum User
Posts: 37
Joined: Wed Apr 02, 2003 1:31 am
Location: Hythe, UK

Forms do that

Postby mammal » Wed Apr 02, 2003 3:10 am

There's nothing wrong with your form, this is a common problem that can occur. The best way round it is, before inserting the data into the database do a SELECT query to see if the registration information already exists:

i.e.

<?

$query = "SELECT FROM table_name WHERE username='$username' OR first_name = '$first_name'";
// Find whether username and firstname already exists (you may want to avoid this search on the password as some people may use the same password but have different usernames)
$result = mysql_query ($query);
$num_rows = mysql_num_rows ($result);

if ($num_rows > 0) { // i.e if matches were found

echo "User details already exist!";

} else {

$query2 = "INSERT INTO table_name (username, password) VALUES ('$username', '$password')";
if ($result2 = mysql_query ($query2)) {

echo "Information inserted!";

} else {

echo "Database Error!";

}
}

?>

I recommend doing this anyway as its good practice when inserting information into a database.

--------------------------------------------------

In addition, you can disable the form button using javascript after they have submitted it once.

This link may help an inpatient user pressing it twice:

http://javascript.internet.com/forms/di ... ubmit.html


Hope this helps a bit!!

User avatar
wmdrumaizi
New php-forum User
New php-forum User
Posts: 40
Joined: Thu Jan 23, 2003 5:15 pm
Location: Malaysia
Contact:

This happened when press "Refresh/Reload" button

Postby wmdrumaizi » Wed Apr 02, 2003 5:06 pm

Thanks mammal but what I mean is:

I have a registration form--> Fill in the name, organization, e-mail, contact no..etc then press Submit when finish.

A page will appear with message "Your record has been added to the database, thank you for registering"

When I check the database, '1' record has beed added (ok it's work), but when I press "Refresh/Reload" button on second page (Page with confirmation message) the record in hte database will duplicate as many Refresh button I press...Is this what they called "cookies" could U tell me how to avoid this...
Thanks a lot

User avatar
mammal
New php-forum User
New php-forum User
Posts: 37
Joined: Wed Apr 02, 2003 1:31 am
Location: Hythe, UK

Hi

Postby mammal » Thu Apr 03, 2003 1:52 am

You cannot avoid this unless you do what I said earlier, your PHP script does its thing when the page loads so when you refresh the page, it will do it again. To avoid duplicate entries do a SELECT query to check whether the users details are already in there.

Also, you could put this in the head tags to automatically redirect the user to another page after 5 seconds so they dont get a proper chance to refresh the page.

<meta http-equiv="Refresh"
content="5;url=http://www.yoururl.com">

Your page is only as smart as your PHP, it will only do what you tell it to do. If you tell it to insert records then it will do that and only that. Check out my code again, it will save you some problems with duplicate entries.

Let me know how you get on...

User avatar
wmdrumaizi
New php-forum User
New php-forum User
Posts: 40
Joined: Thu Jan 23, 2003 5:15 pm
Location: Malaysia
Contact:

Thanks a lot, but need a little help...

Postby wmdrumaizi » Thu Apr 03, 2003 11:35 pm

Thanks a lot mammal, could you tell me how to insert record that is not in pull down menu?

Ok, I have a pull down menu named organization with values as follow:
-UN
-EU
-ASEAN
-NATO
-Ministry
-Other

If user select 'other' they are required to specify the organization in the textbox named organization2...how to do this? And what is 'Other' value should be?

Thanks in advance[/code]


Return to “mySQL & php coding”

Who is online

Users browsing this forum: No registered users and 3 guests