I'm not sure even a professional cleaning firm could clean up this mess :)
See updated code below, you have a couple of typo's which has been commented and replaced with a new line.
When learning from Youtube, pause sometimes and go over your code, try to understand how it works. Seems like you're just rushing to copy all of it and never even looks at the code.
Also read the error messages, they're there for a reason.
Parse error: syntax error, unexpected ';' in C:\xampp\htdocs\Hamza\login.php on line 9
Unexpected ';' on line 9 - take a look at line 9, you've forgot to close the die() bracket, so the ; was unexpected.
<?php
$username = $_POST['username'];
$password = $_POST['password'];
if ($username&&$password)
{
//$connect = mysql_connect("localhost","root","root") or die("Couldn't connect!";
$connect = mysql_connect("localhost","root","root")or die("Couldn't connect!");
mysql_select_db("phplogin") or die ("Couldn't find db");
//$query = mysql_query("SELECT * FROM "); users WHERE username='$username'");
$query = mysql_query("SELECT * FROM users WHERE username='$username'");
//$numrows = mysqal_num_rows($query);
$numrows = mysql_num_rows($query);
echo $numrows;
}
else
die("Please enter username and a password!");
?>
A few (very important) side notes, which you must learn sooner or later:
Passwords should not be stored as plain text in your database (or any other place for that matter).
If using PHP 5.5.0 or later you can use the php function password_hash()
which creates a very secure hash. You can then use password_verify()
for the login function.
See http://php.net/manual/en/function.password-hash.php for more info.
Older hashing algorithms like md5 and sha1 should not be used for storing passwords. They are unsafe and can easily be cracked.
If you can't use password_verify()
, please also read up on salts. Salts can make the hashes even more secure by adding a unique salt to every hashed password.
Your current code only does a database query for the username. Needless to say, you should also check submitted password before logging in the user :)
Input sanitizing. It's important to sanitize all user input to avoid security breaches. Think of all users as hackers who are constantly attacking your website. Read the answer here for a few quick tips, but I recommend you to google and read more about input sanitizing.
Don't use mysql_* functions. You'd be better of starting right at mysqli (syntax is almost identical to mysql) or PDO. Read up on them and learn them, as you will need to at some point. (mysql_* is deprecated and will stop working in a future PHP version)
Both MySQLi and PDO supports prepared statements. It basically means the database is preparing and optimizing the query in advance, then binding all values into the query right before execution. This will also avoid SQL injections to some degree because the query is already prepared by the time you're binding the values (which may be user input) to the query.