Click here to Skip to main content
16,018,294 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
<?php
// Database connection parameters

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "seed_planting";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Process login
if ($_SERVER["REQUEST_METHOD"] == "POST") 
{
    $User_ID  = $_POST['User_ID'];
    $Pass_Word = $_POST['Pass_Word'];

    if (isset($_POST['User_ID']) && (isset($_POST['Pass_Word']))
    {

        // Validate username and password
    $sql = "SELECT * FROM user WHERE User_ID =" & " '$User_ID' " & " And Pass_Word = " & "'$Pass_Word'";
    $result = $conn->query($sql);

    if ($result->num_rows > 0) {
        // Login successful
        echo "Login successful!";
        // You can redirect or set session variables here
    } else {
        // Login failed
        echo "Invalid username or password.";
    }

}
else
{
	exi();
}
}
$conn->close();
?>


What I have tried:

I remove single quotes from user_id & Password.
Posted

Don't do that! 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?

Fix that through your whole app, and the chances are you problem will go away at the same time.

[edit]
Oh, any by the way ...
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 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.
[/edit]
 
Share this answer
 
v2
To add to Griff's already fantastic response, there are several problems with your code I can immediately see:

PHP
if (isset($_POST['User_ID']) && (isset($_POST['Pass_Word'])))
   ^1                           ^2    ^3

Missing an extra closing parenthesis on this line, you have 3 opening ones and only 2 closing ones at the end, which is invalid PHP.

PHP
exi();

This isn't a valid PHP Function, did you mean to write exit();? If you did mean to write exit(); then this will terminate the PHP script before the $conn->close(); will be called, which isn't good.
 
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