Retrieve some PHP from the DB

Discussion in 'PHP' started by D-Fraz, Jun 12, 2008.

  1. #1
    Ok I am trying to retrieave level from the database but I don't know how.

    I have a level session and I have it to echo it but I don't know how to get the level of each user because I don't know how to retrieve a users level.
    Do you?

    If so...

    
    <?php session_start(); include("config.php");
    $username=$_POST['username'];
    $password=$_POST['password'];
    $result = mysql_query("SELECT * FROM members WHERE username='$username' and password='$password'") or die(mysql_error());
    $result3=mysql_query($result);
    $count=mysql_num_rows($result);
    if($count==1){
    if($level==1){}
    $_SESSION['username'] = "$username";
    $_SESSION['password'] = "$password";
    $_SESSION['level'] = "$level";
    header("location:login_success.php");
    
    }
    
    else {
    echo "Wrong Username or Password";
    }
    ?>
    PHP:

    But on the login success page it's empty, when it should be whatever I typed in in phpmyadmin.

    Please help.
    Thanks
     
    D-Fraz, Jun 12, 2008 IP
  2. MakeThatDollar

    MakeThatDollar Notable Member

    Messages:
    4,451
    Likes Received:
    158
    Best Answers:
    0
    Trophy Points:
    225
    #2
    The only thing you have echo'ing is " Wrong Username or Password ".
     
    MakeThatDollar, Jun 12, 2008 IP
  3. php-lover

    php-lover Active Member

    Messages:
    261
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    58
    #3
    session_start(); 
    include("config.php");
    $username=$_POST['username'];
    $password=$_POST['password'];
    
    $query = "SELECT * FROM members WHERE username='$username' and password='$password'"; 
    if($result = mysql_query($query) or die(mysql_error())){
    
       if(is_resource($result)){  
       
          if(mysql_num_rows($result) > 0){
          
             $row = mysql_fetch_array($result);
             
             if($row['level'] == 1){
             
                $_SESSION['username'] = $username;
                $_SESSION['password'] = $password;   //?
                $_SESSION['level'] = $row['level'];
                header("location:login_success.php");
             
             }
          }else{
    
    echo "wrong username or password";
    }      
       }
    }
    PHP:
     
    php-lover, Jun 12, 2008 IP
  4. D-Fraz

    D-Fraz Peon

    Messages:
    234
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Parse error: syntax error, unexpected '{' in C:\wamp\www\checklogin.php on line 7
     
    D-Fraz, Jun 12, 2008 IP
  5. php-lover

    php-lover Active Member

    Messages:
    261
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    58
    #5
    sorry i forgot )
    retype the red line above
     
    php-lover, Jun 12, 2008 IP
  6. kmap

    kmap Well-Known Member

    Messages:
    2,215
    Likes Received:
    29
    Best Answers:
    2
    Trophy Points:
    135
    #6
    did that solve

    Regards

    Alex
     
    kmap, Jun 12, 2008 IP
  7. D-Fraz

    D-Fraz Peon

    Messages:
    234
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Ok theres no errors but it's like it won't redirect you to login_success or wrong password.

    Anyways I'm trying to get it right too so you're not the only one ;)
     
    D-Fraz, Jun 12, 2008 IP
  8. php-lover

    php-lover Active Member

    Messages:
    261
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    58
    #8
    :) great stuff, I guess that your table has a field level.
     
    php-lover, Jun 12, 2008 IP
  9. D-Fraz

    D-Fraz Peon

    Messages:
    234
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #9
    No it doesn't. It stays at checklogin.php and never gives you "Wrong Username or Password" and never redirects you to login_success.php

    Here lemme give you the full codes.

    checklogin.php
    (What you gave me)

    session_start();
    include("config.php");
    $username=$_POST['username'];
    $password=$_POST['password'];
    
    $query = "SELECT * FROM members WHERE username='$username' and password='$password'";
    if($result = mysql_query($query) or die(mysql_error())){
    
    if(is_resource($result)){
    
    if(mysql_num_rows($result) > 0){
    
    $row = mysql_fetch_array($result);
    
    if($row['level'] == 1){
    
    $_SESSION['username'] = $username;
    $_SESSION['password'] = $password; //?
    $_SESSION['level'] = $row['level'];
    header("location:login_success.php");
    
    }
    }else{
    
    echo "wrong username or password";
    }
    }
    }
    PHP:
    login_success.php
    <?php session_start(); require("config.php");  if(!session_is_registered(user)){ echo $_SESSION['username']; } ?><?php 
    if(!session_is_registered(level)){ echo $_SESSION['level']; }    ?>
    PHP:
    adduser.php
    <?php
        
        // Make sure a form with the name username and password are created.
        
        // Include some necesarry files.
        include('config.php');
        
        // Grab some variables
        $Username = mysql_real_escape_string($_POST['username']);
        $Password = mysql_real_escape_string($_POST['password']);
        $Password_Encrypted = md5($Password);
        
        // Setup Query
        $Query = "INSERT INTO `members` (`id`, `username`, `password`) VALUES (NULL, '$Username', '$Password_Encrypted')";
        // Execute Query
        mysql_query'($Query)' or die mysql_query();
        // Check if query did get inserted
        $Query_Check = mysql_affected_rows();
        if($Query_Check == 1)
        {
            $_SESSION['username'] = "$username";
            // The , used below are to keep the echo fluid and is often required and used in much more professional scripts.
            echo 'User ', $_SESSION['username'], ' was Inserted into database, Session Set.';
        }
        else
        {
            echo 'FAILURE!!!';
        }
        
    ?>
    PHP:
    login.php
    <?php session_start(); ?><form name="login" id="name" action="checklogin.php" method="post">
    Username: <input name="username" type="text" id="username"> <br>
    Password: <input name="password" type="text" id="password">
    <input type="submit">
    Code (markup):
     
    D-Fraz, Jun 12, 2008 IP
  10. kmap

    kmap Well-Known Member

    Messages:
    2,215
    Likes Received:
    29
    Best Answers:
    2
    Trophy Points:
    135
    #10
    Oh </form> is missing in login.php

    Regards

    Alex
     
    kmap, Jun 12, 2008 IP
  11. D-Fraz

    D-Fraz Peon

    Messages:
    234
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Thanks but that's not the prob. The prob is checklogin.php

    Let me make it more cleaer.


    In checklogin.php I need it to check the level from the database of whatever user. If I'm the admin the level should be 1. So admin levels are 1.
    When I click submit on login.php it will go to checklogin.php and checklogin.php checks the username, passwor and level. If the username and password are right it'll go to login_succes.php if it's wrong it should say "Wrong Username / Password" and while it's checking the username and password it should check the level of the user and then on login_success.php it should say your level is whatever level the user is.

    You get it now?
     
    D-Fraz, Jun 12, 2008 IP
  12. php-lover

    php-lover Active Member

    Messages:
    261
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    58
    #12
    I think you need to add a filed level to your members table, and when you add new member to your members table you need to add username,password and level.
     
    php-lover, Jun 12, 2008 IP
  13. D-Fraz

    D-Fraz Peon

    Messages:
    234
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #13
    I already have that stuff. All I need is checklogin.php to just simply retrieve the level of that user.

    Do you have a sample?
     
    D-Fraz, Jun 12, 2008 IP
  14. kmap

    kmap Well-Known Member

    Messages:
    2,215
    Likes Received:
    29
    Best Answers:
    2
    Trophy Points:
    135
    #14
    best way give me hosting details let me sort this for u

    U can change pass after that

    If its on local server

    I am helpless

    Regards

    Alex
     
    kmap, Jun 12, 2008 IP
  15. D-Fraz

    D-Fraz Peon

    Messages:
    234
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #15
    Perfect BUT I don't know if it's retrieveing the level. So in login_success.php I need it to echo the level. But I don't really know how exactly.
     
    D-Fraz, Jun 12, 2008 IP
  16. php-lover

    php-lover Active Member

    Messages:
    261
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    58
    #16
    I don't know where you trying to retrieve your level from.

    also your password is encripted with md5 function, which mean you need to compare your password like this

    $username=$_POST['username'];
    $password=md5($_POST['password']);
    "SELECT * FROM members WHERE username='$username' and password='$password'";
     
    php-lover, Jun 12, 2008 IP
  17. kmap

    kmap Well-Known Member

    Messages:
    2,215
    Likes Received:
    29
    Best Answers:
    2
    Trophy Points:
    135
    #17
    echo $row['level'];

    Add this and refresh

    Regards

    Alex

    yes u r not encrypting password as ur adduser says u r encrypting pass so pass comparisons will allways be wrong

    Regards

    Alex
     
    kmap, Jun 12, 2008 IP
  18. D-Fraz

    D-Fraz Peon

    Messages:
    234
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #18
    It's Empty.
     
    D-Fraz, Jun 12, 2008 IP
  19. kmap

    kmap Well-Known Member

    Messages:
    2,215
    Likes Received:
    29
    Best Answers:
    2
    Trophy Points:
    135
    #19
    lol checklogin.php is missing <?

    ?>
    these

    Regards

    Alex
     
    kmap, Jun 12, 2008 IP