Nick Nick - 6 months ago 39
MySQL Question

Connecting my php Login screen to my SQL DB

I don't really do much php myself but am in the process of creating a php login screen for a UI I am building to pull data to a table from a mysql db. I have the table connecting to the db fine and the general screen working but am having trouble with the login screen.

All I want achieve at the moment is for the login.php page to accept the correct credentials and direct the user to the index.php page.

The index.php page does contain the

include('session.php'); //include login session


For example I keep getting the following two errors


  1. Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\chaterFinanceTest\login.php on line 26

  2. Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\chaterFinanceTest\login.php on line 29



Here is the code for my login.php page

<?php
include("config.php");
session_start();

if($_SERVER["REQUEST_METHOD"] == "POST") {
// username and password sent from form

$myusername = mysqli_real_escape_string($db,$_POST['username']);
$mypassword = mysqli_real_escape_string($db,$_POST['password']);

$sql = "SELECT id FROM admin WHERE username = '$myusername' and passcode = '$mypassword'";

$result = mysqli_query($db,$sql) or die("Error: ".mysqli_error($db));
$row = mysqli_fetch_array($result,MYSQLI_ASSOC);
$active = $row['active'];

$count = mysqli_num_rows($result);

// If result matched $myusername and $mypassword, table row must be 1 row

if($count == 1) {
session_register("myusername");
$_SESSION['login_user'] = $myusername;

header("location: index.php");
}else {
$error = "Your Login Name or Password is invalid";
}
}
?>
<html>

<head>
<title>Login - EtherFile</title>
<link rel="icon" type="image/x-icon" href="img/favicon1.png">
<link rel="stylesheet" type="text/css" href="css/login.css">
</head>

<body bgcolor = "#FFFFFF">

<div style="margin-top:10%;" align = "center">
<div class="login-box" style = "width:460px; border: solid 1px #333333; background-color:#fff; " align = "left">
<div class="login-header" style ="background-color:#003466; color:#FFFFFF; padding:3px;"><b> &nbsp; LOGIN</b></div>

<div style = "margin:30px">
<img style="margin-bottom:30px;" class="logo" title="Logo" src="img/logo.jpg" alt="Logo">
<form style="margin-left:15px; margin-right:15px;" action = "" method = "post">
<label>Username : </label><input type = "text" name = "username" class = "box"/><br /><br />
<label>Password : </label><input type = "password" name = "password" class = "box" /><br/><br />
<input type = "submit" value = " Submit "/><br />
</form>

<div style = "font-size:11px; color:#cc0000; margin-top:10px"><?php echo $error; ?></div>

</div>

</div>

</div>

</body>
</html>


Also here are the details for my config.php

$mysql_hostname = "localhost"; //your mysql host name
$mysql_user = "adminTest"; //your mysql user name
$mysql_password = "badgerBadger"; //your mysql password
$mysql_database = "mushroom"; //your mysql database

$db = new mysqli($mysql_hostname, $mysql_user, $mysql_password, $mysql_database);
//mysql_select_db($mysql_database, $bd) or die("Error on database connection");
if($db->connect_errno > 0){
die('Unable to connect to database [' . $db->connect_error . ']');
}


Any help would be greatly appreciated, thanks in advance :)

#

CHANGES

#

Thanks for the suggestions with the error handling. I made the addition and I now get the error telling me that table adminTest.admin does not exist

I have now created the admin_tbl in the sql db with 7 columns
I now get the error Error: Unknown column 'id' in 'field list'

I thought I would only have to query the username and password from the db to start the session. Confused why at this time I need to query all columns

Answer

make sure that your database field name are same as your query field and atleast one record is there in your database... And just check your database connection

     $result = mysqli_query($db,$sql) or die("Error: ".mysqli_error($db));

do it and check where is the error