Current location: Hot Scripts Forums » Programming Languages » PHP » php and mysql


php and mysql

Reply
  #1 (permalink)  
Old 08-30-05, 09:50 AM
javiadas javiadas is offline
Newbie Coder
 
Join Date: Jun 2003
Location: Mexico
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
php and mysql

Hi,

I have a database with:

id
username
password
email

the id is the primary key and autoincrement, the username and the email are unique.

my problem is when i submit a form with a username that already exist or the email already exist my insert causes an error.
how can i handle error, how can i know if the error is cause by the email or the username that already exist.

hope i explain well my problem.

please help me.

thanks
Reply With Quote
  #2 (permalink)  
Old 08-30-05, 11:51 AM
alexweb alexweb is offline
Newbie Coder
 
Join Date: May 2004
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Hello!

You should use two queries: first for checking and second for inserting:

$login=(isset($_POST['login']))? $_POST['login']: '';
$passwd=(isset($_POST['passwd']))? $_POST['passwd']: '';
$email=(isset($_POST['email']))? $_POST['email']: '';
if (empty($login) || empty($passwd) || empty($email)){
die('Please, fill all fields');
}
if (!$result=mysql_query('SELECT id FROM table_name WHERE username='.$login.' OR password='.$passwd.' OR email='.$email)) {
die('Wrong query');
}
if (mysql_num_rows($result)>0) {
die('Your data already exists in our database');
} else {
----- write your insert query here ------
}

Last edited by alexweb; 08-30-05 at 11:54 AM.
Reply With Quote
  #3 (permalink)  
Old 08-30-05, 12:38 PM
wheezy360's Avatar
wheezy360 wheezy360 is offline
Newbie Coder
 
Join Date: Nov 2003
Location: Toronto, ON
Posts: 64
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by alexweb
Hello!

You should use two queries: first for checking and second for inserting:

$login=(isset($_POST['login']))? $_POST['login']: '';
$passwd=(isset($_POST['passwd']))? $_POST['passwd']: '';
$email=(isset($_POST['email']))? $_POST['email']: '';
if (empty($login) || empty($passwd) || empty($email)){
die('Please, fill all fields');
}
if (!$result=mysql_query('SELECT id FROM table_name WHERE username='.$login.' OR password='.$passwd.' OR email='.$email)) {
die('Wrong query');
}
if (mysql_num_rows($result)>0) {
die('Your data already exists in our database');
} else {
----- write your insert query here ------
}
Most everything looks ok here except for the check to see if the password is unique. This is unneccessary and creates a security hole. Stick with checking the username and e-mail.
Reply With Quote
  #4 (permalink)  
Old 08-30-05, 02:33 PM
javiadas javiadas is offline
Newbie Coder
 
Join Date: Jun 2003
Location: Mexico
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
ok i will check it, hope this works.

i will just validate the username and the email, the password i dont think is important because a lot of people can use the same password.

but the question here is how can i know which of the fields are the one that is wrong.
Reply With Quote
  #5 (permalink)  
Old 08-30-05, 02:48 PM
wheezy360's Avatar
wheezy360 wheezy360 is offline
Newbie Coder
 
Join Date: Nov 2003
Location: Toronto, ON
Posts: 64
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by javiadas
ok i will check it, hope this works.

i will just validate the username and the email, the password i dont think is important because a lot of people can use the same password.

but the question here is how can i know which of the fields are the one that is wrong.
You could separate the check into two separate queries:
PHP Code:

if (mysql_num_rows(mysql_query('SELECT * FROM users WHERE email="'.$_POST['email'].'"'))>0) {

// Invalid e-mail
} else if (mysql_num_rows(mysql_query('SELECT * FROM users WHERE username = "'.$_POST['username'].'"'))>0) {
// Invalid user
} else {
// Continue with registration

Note the above code should work logically but it's not exactly secure.. Make sure you check the POST variables and what not which I'm sure you're already doing.
Reply With Quote
  #6 (permalink)  
Old 08-30-05, 04:55 PM
javiadas javiadas is offline
Newbie Coder
 
Join Date: Jun 2003
Location: Mexico
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
thanks alot i will try it.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP and MySQL ? rob2132 Hot Scripts Forum Questions, Suggestions and Feedback 4 08-29-08 02:22 AM
IIS MYSQL and PHP nommiiss PHP 6 01-31-06 04:30 PM
RESELLER WEB HOSTING - $9.99/MONTH For 4GB HD & 30GB BW! CPanel, PHP, MySQL & MORE! IncognitoNet General Advertisements 2 03-08-05 06:47 PM
RESELLER WEB HOSTING - $9.99/MONTH For 4GB HD & 30GB BW! CPanel, PHP, MySQL & MORE! IncognitoNet General Advertisements 0 02-20-05 11:51 AM
RESELLER WEB HOSTING - $9.99/MONTH For 4GB HD & 30GB BW! CPanel, PHP, MySQL & MORE! IncognitoNet General Advertisements 0 01-30-05 10:51 PM


All times are GMT -5. The time now is 07:05 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.