/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] login.php / config.php

Im looking for help with the following code, I think i done it right but im getting errors on line 13 and line 16, My data in the mysql is under userName and userPass (encrypted) but when i hit the submit button im getting an error, Here is the original code, can anyone shed any light on it for me please. my database table name is ‘users’ once submitted i need it to redirect to display_data.php i have already set the confip.php file up.

[code=php]
<?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);
$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: welcome.php”);
}else {
$error = “Your Login Name or Password is invalid”;
}
}
?>
[/code]

Do i change the following line to this

[code=php] $sql = “SELECT id FROM users WHERE username = ‘$userName’ and passcode = ‘$userPass'”; [/code]

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@rootAug 30.2017 — Posting the actual error is helpful as well as that, what are lines 13 and 16...
Copy linkTweet thisAlerts:
@grierslauthorAug 30.2017 — The error displayed is

[code=php] Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in /home/Folder/public_html/folder/members_area/login/login.php on line 13

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in /home/Folder/public_html/folder/members_area/login/login.php on line 16 [/code]
Copy linkTweet thisAlerts:
@rootAug 30.2017 — Then [B]$result[/B] must be returning a [B]boolean[/B] value and [B]not[/B] a result, this could be because there's something wrong with your query string.

I think its something like these two lines added to the top of your script
[code=php]error_reporting(E_ALL);
ini_set('display_errors', 1);[/code]
you could create them in a .php file of their own and just include them in the offending script that isn't playing ball.

I would also say that you need to examine the data you are trying to gather from the [B]query[/B] vs [B]the results[/B] you are expecting, you have a reference to a field in a result set that won't contain that field, so change [B]id[/B] to a [B]*[/B] so that all fields are returned in the results, as well as that, in your query, I suggest that you [B]LIMIT[/B] your resilts to 1 result so that rather than testing if [B]$count==1[/B], you change the routine so that your testing [B]!$count[/B] which if false will evaluate to true so that your [B]if()[/B] clause can then be used to kill the script or send the user to another page like an error page.
Copy linkTweet thisAlerts:
@NogDogAug 30.2017 — [code=php]
$result = mysqli_query($db,$sql);
# make sure it worked!
if($result == false) {
die("<pre>Query failed: ".$db->error."n$sql</pre>");
}
[/code]

This is a quick-and-dirty way to find out what the error is, but in the live code you'd want it to log that error info and display some "nice" message to the user, not dump your database info to anyone who might see it. ?
×

Success!

Help @griersl spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.30,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...