PHP School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a PHP Expert!

Join 300,475 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,738 people online right now. Registration is fast and FREE... Join Now!




Username/Password fields to a database

 

Username/Password fields to a database

chrisp200

22 Nov, 2008 - 11:48 PM
Post #1

D.I.C Head
**

Joined: 18 Nov, 2008
Posts: 59


I am an independently attempting to learn php. I recently watched a tutorial on youtube on making basic User name and Password fields, once a user types a user name and password, the information is supposed to be saved in a database called Tutorial02. Everytime I submit a user name and pass, nothing is saved in the database.

I'm not sure if I have given enough information for anyone to help. If not, please inform on what information is needed and I will try to explain further what I know.

Thank you

Chris P crazy.gif


CODE

<?php

    mysql_connect("localhost","root") or die(mysql_error());
    mysql_select_db("tutorial02") or die(mysql_error());
    

    
    
    ?>
    <html>
    
    <form method="POST" action="index.php">
    <table border="0" style="font-size: 15px; font-family: Tahoma; "border:    solid black "">
    
        
        <td>
            Username:
        
        
        
                <input type="text" name="username" value="<?php echo $_POST['username']; ?>"
        
        </td>
        
        
        
        
            <td>
                Password:
                <input type="password" name="password" value="<?php echo $_POST['password']; ?>"
            <tr><td colspan="2" align="center">
            
                <input type="submit" name="submit" value="Register"/>
            </td>
        </tr>
        
    </table>
    </form>
    
    </html>
    <?php
    
    if($_POST['submit']){
    
        $username = $_POST['username'];
        $password = $_POST['password'];
        
        $curnum = 0;
        
        if(!$username)  {
            $curnum ++;
            echo "<font color='red'>" . $curnum .  ". You didn't enter a username!</font> <BR>";
        
        }
        
        if(!$password)  {
            $curnum ++;
            echo "<font color='red'>" .$curnum .  ". You didn't enter a password! </font><BR>";
            }
        }
        //checks if username already exsists
        $sql = "SELECT * FROM users WHERE username='".$username."'";
        $res= mysql_query($sql) or die (mysql_error());
        
        if(mysql_num_rows($res) > 0) {
        
            $curnum ++;
            echo $curnum .  ". The username ' ".$username."' already exists!<BR>\n";
        
            if ($curnum == 0){
            
                mysql_query("INSERT INTO users VALUES(`id`,'".$username."','".$password."')") or die(mysql_error());
            
            }
        }
        

    
?>


User is offlineProfile CardPM
+Quote Post


no2pencil

RE: Username/Password Fields To A Database

22 Nov, 2008 - 11:58 PM
Post #2

i R L33t Skiddie, k?
Group Icon

Joined: 10 May, 2007
Posts: 13,234



Thanked: 289 times
Dream Kudos: 2875
Expert In: Goofing Off

My Contributions
CODE

<?php
    $err = 0;  
    $curnum = 0;
    if(isset($_POST['username'])){
        $username = strip_tags($_POST['username']);
    }
    else $err=1;
    if(isset($_POST['password'])
        $password = md5(strip_tags($_POST['password']));
    }
    else $err=2;
    if($err==1) {
            $curnum ++;
            echo "<font color='red'>" . $curnum .  ". You didn't enter a username!</font> <BR>";
    }
    if($err=2) {    
            $curnum ++;
            echo "<font color='red'>" .$curnum .  ". You didn't enter a password! </font><BR>";
     }
    mysql_connect("localhost","root") or die(mysql_error());
    mysql_select_db("tutorial02") or die(mysql_error());

     //checks if username already exsists
     $sql = "SELECT * FROM users WHERE username='".$username."'";
     $res= mysql_query($sql) or die (mysql_error());
        
     if(mysql_num_rows($res) > 0) {
        
          $curnum ++;
          echo $curnum .  ". The username ' ".$username."' already exists!<BR>\n";
        
          if ($curnum == 0){            
              mysql_query("INSERT INTO users VALUES(`id`,'".$username."','".$password."')") or die(mysql_error());            
          }
     }
?>

    <html>
    
    <form method="POST" action="index.php">
    <table border="0" style="font-size: 15px; font-family: Tahoma; "border:    solid black "">
        <td>Username:
                <input type="text" name="username" value="<?php echo $_POST['username']; ?>"
        </td>    
        <td>
                Password:
                <input type="password" name="password" value="<?php echo $_POST['password']; ?>"
        </td>
        <tr><td colspan="2" align="center">
                <input type="submit" name="submit" value="Register"/>
          </td>
         </tr>      
    </table>
    </form>
    
    </html>


For the most part what you had was correct. Just needed to put the php code before the html. Also, even though MD5 isn't the best, it's better than nothing. You don't want to simply input user data into mysql, so that's why we striptags, & then at least md5 it before putting into the database.
User is online!Profile CardPM
+Quote Post

chrisp200

RE: Username/Password Fields To A Database

23 Nov, 2008 - 01:01 AM
Post #3

D.I.C Head
**

Joined: 18 Nov, 2008
Posts: 59


For the most part what you had was correct. Just needed to put the php code before the html. Also, even though MD5 isn't the best, it's better than nothing. You don't want to simply input user data into mysql, so that's why we striptags, & then at least md5 it before putting into the database.


Okay, thank you for your reply! I am going to need to study much more to understand though. If you don't mind, could you elaborate on exactly what MD5 and striptags are? or direct me to a tutorial that may help?
User is offlineProfile CardPM
+Quote Post

no2pencil

RE: Username/Password Fields To A Database

23 Nov, 2008 - 01:04 AM
Post #4

i R L33t Skiddie, k?
Group Icon

Joined: 10 May, 2007
Posts: 13,234



Thanked: 289 times
Dream Kudos: 2875
Expert In: Goofing Off

My Contributions
Strip Tags : Strip HTML and PHP tags from a string

MD5 : Calculate the md5 hash of a string
User is online!Profile CardPM
+Quote Post

ludjer

RE: Username/Password Fields To A Database

23 Nov, 2008 - 03:10 AM
Post #5

D.I.C Head
**

Joined: 31 Oct, 2008
Posts: 103



Thanked: 2 times
My Contributions
also make sure to escape SQL sensitive strings to prevent people from deleting your database
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/8/09 03:18AM

Live PHP Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month