Click here to Skip to main content
15,789,776 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
<?php
    include_once'connectdb.php';
    session_start();
    
    if(isset($_POST['btn_login']))
    {
    $username = $_POST['txt_username'];
    $password = $_POST['txt_password'];
            
    //echo $username."-".$password;
    $select= $pdo->prepare("select * from tbl_user 
            
    where username='$username' AND 
    password='$password'");
            
    $select->execute();
            
    $row=$select->fetch(PDO::FETCH_ASSOC);
            
    if($row['username']==$username AND 
    $row['password']==$password){
                
    echo $success='Login Successfull';
    header('refesh:1;dashboard.php');  
         
    }else{
        
    echo'login Fail';
    }
}
?>


What I have tried:

Guys please i need help. am very new to php and have been trying to develop a web based app for my mums pharmacy. I got the code below from a tutorial am using to learn on udemy.

but when i run query i get login fail on the user interface after i enter the correct credentials from db  but when if i leave the places empty and click on login it says login successful but doesn't refresh the page and run dashboard.php

i hope i have explain my problem clear enough 
Posted
Updated 21-Feb-22 6:09am

1 solution

So little code, so many problems ...

1) Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

2) Never store passwords in clear text - it is a major security risk. There is some information on how to do it here: Password Storage: How to do it.[^]

And remember: if this is web based and you have any European Union users then GDPR applies and that means you need to handle passwords as sensitive data and store them in a safe and secure manner. Text is neither of those and the fines can be .... um ... outstanding. In December 2018 a German company received a relatively low fine of €20,000 for just that.

Fix those through your whole app and you may find your original problem is gone. But bear in mind that a pharmacy is not a place where you want strangers to get access to anything - and that includes patient information as well as medications. Unless you know what you are doing this is a task that is much better left to people who do understand security, and who won't leave gaping holes you could drive a truck through!
 
Share this answer
 
v2
Comments
Richard MacCutchan 21-Feb-22 12:19pm    
See points 2 and 3.
OriginalGriff 21-Feb-22 12:48pm    
It bears repeating ... :laugh:

Fixed.
Qwame West 22-Feb-22 7:55am    
Thanks for your concern and the advice @OriginaGriff.

First of all what am trying to do is more of an inventory and pos app. Am not going to take or store patients information. The app is going to keep inventory of drugs in the pharmacy and then process purchases and print out a receipt.

Also am hosting it locally on the system and as i said earlier am trying to learn on the job so i already bought course materials from udemy and other places but am stuck with the code posted.
I taught thats what this forum is about, that's helping and sharing ideas. So please if someone can help me the issue above i will be very grate full. Thanks

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