Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Computer Forums > Programmers Lounge > Programming Discussions » PHP not sure how to compare 2 fields.
Closed Thread
Old 04-08-2009, 03:52 PM   #1 (permalink)
murdocsvan's Avatar
 
Ultra Techie

Join Date: Jun 2007

Location: Surrey, UK

Posts: 849

murdocsvan is on a distinguished road

Default PHP not sure how to compare 2 fields.

Basically, i have a form, for creating new users on my database. It just has 3 fields; one for the username; one for the password and one to confirm the user put the right password in. Everything in the form works except comparing the two password fields to make sure they match. I've tested and tried loads of things but if they don't match they still add the user to the database anyway

This is the code for the end of the script:

PHP Code:
if(empty($username) ||  empty($password) || empty($confirm_password))
    {
    echo 
"Fields are empty";
    }
else
    {
    
//?????????????????????????????????????????????????//
    //???Problem comparing first and second password???//
    //?????????????????????????????????????????????????//
    
if($password != $confirm_password)
        {
        echo 
"New passwords did not match'";
        }
    else
        {
        
//Turn posted fields into sha1 values
        
$password sha1($password);
        
$confirm_password sha1($confirm_password);
        
        
//Determine what the ID number will be
        
$sql "SELECT * FROM $tbl_name";
        
$result mysql_query($sql,$con);
        
$num_rows mysql_num_rows($result);
        
$num_rows ++;
        
        
//Enter new user into database
        
$create "INSERT INTO $tbl_name (id, username, password) 
        VALUES('$num_rows', '$username', '$password')"
;
        
mysql_query($create,$con);
        
        
//Take user to Success! page
        
header("location:****.php");
        }
    } 

__________________

murdocsvan is offline  
Old 04-08-2009, 10:09 PM   #2 (permalink)
office politics's Avatar
 
It's all just 1s and 0s

Join Date: Jan 2004

Location: in the lab

Posts: 4,389

office politics will become famous soon enough

Default Re: PHP not sure how to compare 2 fields.

try using strcmp() instead

PHP: strcmp - Manual
office politics is offline  
Old 04-09-2009, 09:04 AM   #3 (permalink)
 

Join Date: Jul 2005

Location: England

Posts: 2,040

kmote has a spectacular aura aboutkmote has a spectacular aura about

Default Re: PHP not sure how to compare 2 fields.

Your comparison looks fine to me. You might want to try echoing $password and $confirm_password to check that they are in fact the inputs you have given.
Couple of notes about the other parts of the code though; 1) you don't need to encrypt your $confirm_password. 2) The way you are getting your ID is dangerous. If a row is deleted from the table the row count will go down but your next ID never should (since it is not necessarily the last record that was deleted). Instead, use an autoincrementing value on the database and remove the ID from your insert statement.

EDIT: almost forgot, you should also check that the insert is successful
__________________
MSI P43 Neo|Enermax Pro82+ 425W|E5200|silent 8500GT|250GB Samsung spinpoint F1|Samsung SATA DVD RW|4GB Corsair|Antec SOLO|openSUSE11


There are in order of increasing severity: lies, darn lies, statistics, and computer benchmarks. - diskinfo man page

Last edited by kmote; 04-09-2009 at 09:08 AM.
kmote is offline  
Old 04-09-2009, 06:09 PM   #4 (permalink)
CrazeD's Avatar
 
Wizard Techie

Join Date: Feb 2006

Location: Maine

Posts: 3,682

CrazeD will become famous soon enough

Send a message via AIM to CrazeD Send a message via MSN to CrazeD
Default Re: PHP not sure how to compare 2 fields.

You should also be using mysql_escape_string() or mysql_real_escape_string() on ALL user input.
__________________

Need website help? PM me!
CrazeD is offline  
 
Closed Thread

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
A Guide to Installing Apache, PHP, MySQL, and PHPMyAdmin on Windows CrazeD Programming Discussions 17 06-17-2009 04:27 PM
How echo works in PHP Osiris Programming Discussions 0 03-04-2009 08:34 AM
PHP - what it does and what it doesn’t Osiris Programming Discussions 1 02-16-2009 04:09 PM
Wev Development: How does PHP work? Osiris Programming Discussions 1 01-08-2009 04:31 PM