Hello!
Hoping someone can help me, I'm slowly giving up, no doubt someone will point me in the direction of an answer straight away, but I have looked! Honest!
I'm very new at learning PHP, and am attempting to create a simple login script.
As you can see from my code below (assuming I've done it right!) I'm running a query on the database to ensure that the entered username/password returns '1' for one record in the database, then it should proceed to a successful logon page.
The query runs, but I am unsure how I get the count (of 1) onto the
if($sql==1){
line. I know it's not $sql, as this would just output the query, not the output of the query.
If anyone could shed some light on this I would be very happy!
<?php
ob_start();
$host="localhost"; // Host name
$username="username"; // Mysql username
$password="password"; // Mysql password
$db_name="databasename"; // Database name
$tbl_name="members"; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("Admin Error: cannot connect");
mysql_select_db("$db_name")or die(" Admin Error: cannot select DB");
// Define $myusername and $mypassword
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];
// Encrypt password
//$encrypted_mypassword=md5($mypassword);
// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
// The answer to this should be 1
//$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$encrypted_mypassword'";
$sql=("SELECT count(*) FROM $tbl_name WHERE username='$myusername' and password='$mypassword';");
// If result matched $myusername and $mypassword, table row must be 1 row
if($sql==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
session_register("myusername");
session_register("mypassword");
header("location:login_success.php");
}
else {
echo "User Error: Wrong Username or Password<br>";
}
ob_end_flush();
?>