Click here to Skip to main content
15,889,909 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
SQL
Hi

I am new in php. Below code works fine. But If I change Select Command Like this:

$sql = "SELECT login_id,password FROM user_right WHERE login_id = &$username& and password = $password'

It shows error recorde not found. Also I need to show the role of select user. Pls Help me


PHP
<?php
include_once'../inc/header.php';
if (isset($_POST['submit']) && $_POST['submit']  != "" )
{ 
       if(empty($_POST['username']))
            {
                $handleError = "User is empty!";
                $_SESSION["errormsg"] = $handleError;
                header("Location:../notification/errormsg.php"); 
                //echo $handleError;
                return false;
            }
    if(empty($_POST['password']))
            {
                $handleError = "Password is empty!";
                $_SESSION["errormsg"] = $handleError;  
                header("Location:../notification/errormsg.php"); 
                //echo $handleError;
                return false;
            }
        $username = trim($_POST['username']);
        $password = trim($_POST['password']);
        try 
            {
                $sql  = "SELECT * FROM user_right WHERE login_id = '$username' and password = '$password'";  
                $stmt = $dbh->prepare($sql); 
                $stmt->setFetchMode(PDO::FETCH_ASSOC);  
                if ($stmt = $dbh->query($sql)) {
                    if ($stmt->fetchColumn() > 0) 
                        {
                            $_SESSION["username"] = $username;
                            $_SESSION["password"] = $password;
                            header("Location:../admin/"); 
                            exit();
                        }
                    else 
                        {
                            $handleError="user name or password is wrong";
                            $_SESSION["errormsg"] = $handleError;  
                            header("Location:../notification/errormsg.php"); 
                            exit(); 
                        };
                }                
            }
         catch (PDOException $e) 
            {
              echo $e->getMessage() . "\n";
              file_put_contents('PDOErrors.txt', $e->getMessage(), FILE_APPEND);
            }
}
include '../inc/footer.php';
Posted
Comments
Peter Leow 22-Mar-14 8:55am    
Why do you want to do that, if the original code is fine?
Maideen Abdul Kader 22-Mar-14 20:52pm    
coz, i need some other column like role and access no to be in session.
I do not know how to do this. Let say I need to know the role of the user and email id how to do?
Maideen Abdul Kader 22-Mar-14 21:20pm    
and also i need to know if i change the code like
Select login_id,password,role from user_right where login_id='$username' and password='$password'
why the error prompt "login id and password wrong. If i run original code with same username and password, it fine
and also i need $role= how should i get value of role. pls

1 solution

see this.. :)

PHP
$con=mysqli_connect("example.com","peter","abc123","my_db");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$result = mysqli_query($con,"SELECT * FROM Persons");

while($row = mysqli_fetch_array($result))
  {
  echo $row['FirstName'] . " " . $row['LastName'];
  echo "<br>";
  }

mysqli_close($con);
?>
</br>


Reference.. :)

Php Select Query.. :)[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900