Jump to content

error in select command in pdo php


maideen

Recommended Posts

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
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';
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.