Thread: Php Mysql

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    16
    Rep Power
    0

    Php Mysql


    Do you have any idea why my login doesnt work ?

    login_process.php
    PHP Code:
    <?php

    session_start
    (); 
    include 
    "./global.php"
    ?>         
    <?php


    echo "<title>Login</title>\n";

          if(isset(
    $_SESSION['uid'])) 
        echo 
    "You are already logged in, if you wish to log out, please <a href=\"./logout.php\">click here</a>!\n";
    else {

        if (!
    $_POST['submit'])
        {
            
    $user mss($_POST['username']);
            
    $pass= ($_POST['password']);

        }
           
            
                if(
    $user && $pass){
                    
    $sql "SELECT id FROM `users` WHERE `username`='".$user."'";
                    
    $res mysql_query($sql) or die(mysql_error());
                    if(
    mysql_num_rows($res) > 0){
                        
    $sql2 "SELECT id FROM `users` WHERE `username`='".$user."' AND `password`='".md5($pass)."'";
                        
    $res2 mysql_query($sql2) or die(mysql_error());
                        if(
    mysql_num_rows($res2) > 0){
                            
    $row mysql_fetch_assoc($res2);
                            
    $_SESSION['uid'] = $row['id'];
                            
                            echo 
    "You have successfully logged in as " $user "<br><br><a href=\"./index.php\">Proceed to the Forum Index</a>\n";
                        }else {
                            echo 
    "Username and password combination are incorrect!\n";
                        }
                    }else {
                        echo 
    "The username you supplied does not exist!\n";
                    }
                }else {
                    echo 
    "You must supply both the username and password field!\n";
                }
        }



    ?>

    global.php
    PHP Code:
    <?php

     
    //Connects to Mysql or displays  error
    $con mysql_connect("127.0.0.1","root","") or die(mysql_error());
     
    //"my_db" is the name of the database.
    $db mysql_select_db("my_db",$con);
      
    function 
    mss($value){
        return 
    mysql_real_escape_string(trim(strip_tags($value)));
        }
    ?>
  2. #2
  3. Come play with me!
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,754
    Rep Power
    9397
  4. #3
  5. JavaScript is not spelt java
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2011
    Location
    Landan, England
    Posts
    743
    Rep Power
    165
    PHP Code:
    if (!$_POST['submit']) 
    should be
    PHP Code:
    if ($_POST['submit']) 

    Comments on this post

    • johnadamos agrees
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    16
    Rep Power
    0
    Thanks now I can see the echo but still is not working, it displays
    Unknown column 'username' in 'where clause
    when Im trying to login with a user account.
  8. #5
  9. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,301
    Rep Power
    7170
    That means your users table doesn't have a username column.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    16
    Rep Power
    0
    Thank you!!!!! I changed it to username , I had it user. I just start reading about php and I dont know many things.One last question , when im trying to login with a user account it displays wrong echo
    "Username and password combination are incorrect!\n";
    and when I am trying to login without a password & username it displays the correct echo
    "You must supply both the username and password field!\n";
    .I think the problem is in this part of the code
    PHP Code:
      if($user && $pass){ 
  12. #7
  13. JavaScript is not spelt java
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2011
    Location
    Landan, England
    Posts
    743
    Rep Power
    165
    Originally Posted by E-Oreo
    That means your users table doesn't have a username column.
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    16
    Rep Power
    0
    haha I had 2 errors in the code and I changed one part of login.process.php from
    PHP Code:
    if ($_POST['submit']) 
    i changed it to
    PHP Code:
    if (isset($_POST['submit']) ) 
    but there is one more error .
    it says
    Notice: Undefined variable: user in C:\wamp\www\Site\login_process.php on line 23
    the line 23 is
    PHP Code:
     if($user && $pass){ 
    but i have defined before the these 2 variables
    PHP Code:
         $user mss($_POST['username']); 
            
    $pass= ($_POST['password']); 
  16. #9
  17. JavaScript is not spelt java
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2011
    Location
    Landan, England
    Posts
    743
    Rep Power
    165
    Simple question, are username and password the correct names of your form inputs? Perhaps you could show your form-code.
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    16
    Rep Power
    0
    global.php connects to Mysql ,database etc
    PHP Code:
    <?php
    // Replace the variable values 
      
    $host "127.0.0.1";
      
    $root "root";
      
    $pass "";
      
    $database "my_db";
      
     
    //Connects to Mysql or displays  error
    $con mysql_connect($host,$root,$pass) or die(mysql_error());
     
    //"my_db" is the name of the database.
    $db mysql_select_db($database ,$con);
      
    function 
    mss($value){
        return 
    mysql_real_escape_string(trim(strip_tags($value)));
        }
    ?>
    login.php contains css and the form of login.php
    PHP Code:
    <html>
    <
    head>
    <
    body>
    <
    html
            <
    head
             <
    titleLogin Page </title
                
                <
    style type="text/css"
                            
    body 
                                    
    background-color:#3b5999; 
                                    
    color:#999; 
                                    
    font-family:Arial
                                    
    font-size:9pt
                            } 
                            
    #holder { 
                                    
    width:80%; 
                            
    background-color:#CCC; 
                                    
    color:#F00; 
                                    
    font-family:Arial
                                    
    border:1px solid #003; 
                                    
    padding:12px
                                    
    text-align:right
                            } 
                            
                            
    #userInfo { 
                                          
                                            
    color:#000; 
                                            
    font-family:Arial
                                            
    border:1px solid #CCC; 
                                            
    text-aling:right
                                            
    padding:2px
                            } 
                </
    style
          </
    head
          <
    body
             <
    center
             
             <
    table border="0" cellspacing="3" cellpadding="3">
            <
    form id="login-form" method="post" action="login_process.php">
            <
    tr><td>Username</td><td><input type="text" name="username"></td></tr>
            <
    tr><td>Password</td><td><input type="password" name="password"></td></tr>
            <
    tr><td colspan="2" align="right"><input type="submit" name="submit" value="Login"></td></tr>
        
            </
    form></table>
         </
    body>
         </
    html
    login_process.php
    PHP Code:
    <?php

    session_start
    (); 
    include 
    "./global.php"
    ?>         
    <?php


    echo "<title>Login</title>\n";

          if(isset(
    $_SESSION['uid'])) 
        echo 
    "You are already logged in, if you wish to log out, please <a href=\"./logout.php\">click here</a>!\n";
    else {

        if (isset(
    $_POST['submit']) ) //if ($_POST['submit']) 
        
    {
            
    $user mss($_POST['username']); 
            
    $pass= ($_POST['password']);

        }
           
            
                if(
    $user && $pass){
                    
    $sql "SELECT id FROM `users` WHERE `username`='".$user."'";
                    
    $res mysql_query($sql) or die(mysql_error());
                    if(
    mysql_num_rows($res) > 0){
                        
    $sql2 "SELECT id FROM `users` WHERE `username`='".$user."' AND `password`='".md5($pass)."'";
                        
    $res2 mysql_query($sql2) or die(mysql_error());
                        if(
    mysql_num_rows($res2) > 0){
                            
    $row mysql_fetch_assoc($res2);
                            
    $_SESSION['uid'] = $row['id'];
                            
                            echo 
    "You have successfully logged in as " $user "<br><br><a href=\"./index.php\">Proceed to the Forum Index</a>\n";
                        }else {
                            echo 
    "Username and password combination are incorrect!\n";
                        }
                    }else {
                        echo 
    "The username you supplied does not exist!\n";
                    }
                }else {
                    echo 
    "You must supply both the username and password field!\n";
                }
        }



    ?>
    sql code of the database
    -- phpMyAdmin SQL Dump
    -- version 3.5.1
    -- Host: localhost
    -- Generation Time: Dec 02, 2012 at 01:38 AM
    -- Server version: 5.5.24-log
    -- PHP Version: 5.3.13

    SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
    SET time_zone = "+00:00";




    --
    -- Database: `my_db`
    --

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

    --
    -- Table structure for table `users`
    --

    CREATE TABLE IF NOT EXISTS `users` (
    `ID` int(11) NOT NULL AUTO_INCREMENT,
    `username` varchar(32) NOT NULL,
    `password` varchar(32) NOT NULL,
    `email` varchar(255) NOT NULL,
    `firstName` varchar(64) NOT NULL,
    `lastName` varchar(64) NOT NULL,
    `city` varchar(30) NOT NULL,
    `admin` varchar(30) NOT NULL,
    `time` varchar(30) NOT NULL,
    PRIMARY KEY (`ID`)
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;

    --
    -- Dumping data for table `users`
    --

    INSERT INTO `users` (`ID`, `username`, `password`, `email`, `firstName`, `lastName`, `city`, `admin`, `time`) VALUES
    (0, 'root', 'admin', 'root@root.com', 'john', 'adamos', 'volos', '', ''),
    (5, 'adamos', 'adamos', 'adamos@gmail.com', 'adamos', 'adamos', 'adamos', '', '');

IMN logo majestic logo threadwatch logo seochat tools logo