Click here to Skip to main content
15,881,715 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have tried this but mysqli_num_rows is not counting my rows and not cheacking my email and password

What I have tried:

<?php
require ('conn.php');
if (isset($_POST['login'])) {
    $email = mysqli_real_escape_string($con, $_POST['email']);
    $password = mysqli_real_escape_string($con, $_POST['password']);
    $query="SELECT * FROM `users` WHERE `email`='$email' and `password`=$password";
    echo $query;
    $check_user =mysqli_num_rows(mysqli_query($con, $query));
    if ($check_user) {
        $insertquery = "INSERT INTO `admin_users`(`email`, `password`) VALUES ('$email','$password')";
        $query = mysqli_query($con, $insertquery);
        header("location:../login.php");
    } else {
    ?>
        <script>
            alert('please register first');
        </script>
    <?php
    }
}
Posted
Updated 3-Jul-21 23:03pm
Comments
Richard MacCutchan 4-Jul-21 4:37am    
Do not store passwords in clear text. PHP contains a hash function to secure it.
Richard MacCutchan 4-Jul-21 12:12pm    
Look at your SQL statements: you search the users table, and if the entry is found you add it to the admin_users table, and go to the login page. But if the entry is not found you bypass the login. Your code logic is totally confusing.

1 solution

Oh dear ... so many problems, so little code ...

Let's ignore the problem you have found - because it's trivial - and concentrate t=oneth serious ones:

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.[^] - it's in C#, but the code is pretty obvious.

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 your other problem may vanish at the same time...
 
Share this answer
 
Comments
hurab4144 4-Jul-21 5:31am    
ok i will secure the password but how can i solve this login problem because this is not checking the email or password .it's logging in directly what sql code will be compatible for this type of issue

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