I've been writing a php code for my project but I'm having trouble reading data from my xampp database. Every time I put my username & password in the the required fields, an error comes up. I want the code to display the next form after I put in the username and the password. The database I'm using is called sales and has a table which has the fields, id, username and password. I'm new to php and if anyone out there can help me correct the code, I'll really appreciate it.
<?php
//Start session
session_start();
//Array to store validation errors
$errmsg_arr = array();
//Validation error flag
$errflag = false;
//Connect to mysql server
$link = mysqli_connect('localhost','root',"");
if(!$link) {
die('Failed to connect to server: ' . mysqli_error());
}
//Select database
$db = mysqli_select_db( $link,'sales');
if(!$db) {
die("Unable to select database");
}
//Function to sanitize values received from the form. Prevents SQL injection
function clean($str) {
$str = @trim($str);
if(get_magic_quotes_gpc()) {
$str = stripslashes($str);
}
return mysqli_real_escape_string($str);
}
//Sanitize the POST values
$login = clean($_POST['username']);
$password = clean($_POST['password']);
//Input Validations
if($login == '') {
$errmsg_arr[] = 'Username missing';
$errflag = true;
}
if($password == '') {
$errmsg_arr[] = 'Password missing';
$errflag = true;
}
//If there are input validations, redirect back to the login form
if($errflag) {
$_SESSION['ERRMSG_ARR'] = $errmsg_arr;
session_write_close();
header("location: index.php");
exit();
}
//Create query
$qry="SELECT * FROM user WHERE username='$login' AND password='$password'";
$result=mysqli_query($qry);
//Check whether the query was successful or not
if($result) {
if(mysqli_num_rows($result) > 0) {
//Login Successful
session_regenerate_id();
$member = mysqli_fetch_assoc($result);
$_SESSION['SESS_MEMBER_ID'] = $member['id'];
$_SESSION['SESS_FIRST_NAME'] = $member['name'];
$_SESSION['SESS_LAST_NAME'] = $member['position'];
//$_SESSION['SESS_PRO_PIC'] = $member['profImage'];
session_write_close();
header("location: main/index.php");
exit();
}else {
//Login failed
header("location: index.php");
exit();
}
}else {
die("Query failed");
}
?>
Afterwards,it should redirect to this form (index.php) which is located on a different folder. You an download the file here (https://drive.google.com/file/d/1vIuKOtG0v9eZmKEnJQk3LR3-0p6wqtnd/view?usp=sharing)
<form>it's being submitted from.