/    Sign up×
Community /Pin to ProfileBookmark

help with php not retrieving database info

Hi,

Can someone help me with this please, I’m setting up a login page for a new website, my following php code fails at the connection test and throws my error message. When I run the query straight through mysql explicitly typing the email and password the database retrieves fine, and to the best of my ability the html looks fine too but my code is below if anyone can give me a pointer.

[code=php]
<?php
session_start();
include(“dbconnect.php”);
$numrows=0;
$pword=trim($_POST[‘pword’]);
$email=trim($_POST[’email’]);
$query=”select email,fname from members where (password=’$pword’ && email=’$email’)”;
$link = @ mysql_query($query);
if (!$link) {
$result=”problem with your Query – check with your Web administrator”;
echo “<h3><a href=’../home.php’>Back to Home Page</a>”;
die($result);
}
$numrows=mysql_num_rows($link);
if ($numrows>0){
$row = mysql_fetch_array($link, MYSQL_ASSOC);
$_SESSION[‘user’]=$row[‘fname’];
$_SESSION[’email’]=$row[’email’];
echo ‘<script>window.location=”../home.php”</script>’;
//header(“Location:../home.php”);
} else {
echo “Login Failure retry with correct login credentials”;
echo “<h3><a href=’../home.php’>Back to Home Page</a>”;
}
?>

[/code]

and the HTML

[code=html]
<div id=”logUs”>
<form id=”logForm” method=”post” action=”scripts/login.php” >
<fieldset>
<legend>Log In</legend>
<label for=”email”>Enter Email: </label>
<input type=”text” name=”email” style=”width:120px” id=”email” /><br />
<label for=”pword”>Enter Password: </label>
<input type=”password” name=”pword” id=”pword” style=”width:120px” />
<input type=”submit” style=”width:80px” value=”Log In” style=”width:50px” />
</fieldset>
</form>
[/code]

What should I be looking for to fix this?

Many Thanks

PeteW

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@SrWebDeveloperMar 03.2010 — If it fails at the connection part, you need to show us dbconnect.php - just blank out sensitive information when posting here.


Beyond that, general stuff (no need to address each with me, just an FYI for your final production version, I know it's under construction now):

Find:

[code=php]$query="select email,fname from members where (password='$pword' && email='$email')"; [/code]

Optionally Change to (i.e. not a fix, merely a suggestion):

[code=php]$query="select email,fname from members where password='$pword' && email='$email'"; [/code]

Don't need the parenthesis in this situation. Optimize your SQL whenever possible.

Not wise tot use @ to suppress warnings/errors - you want to see that during development AND you want to add code to check for errors which is proper coding technique.

It's also an interesting choice to comment out the PHP header/location redirect and use Javascript instead. The cardinal rule I live by - if it can be done server side, do it server side - anyone who simply turns off JS in their browser break it, otherwise.

If possible, avoid inline styling - put all that in a stylesheet and centralize it, you'll appreciate that later on. If you already have a style sheet and are overriding via the inline styling, it's still a matter of improving the CSS selectors already there.

Please get back to us on the dbconnect.php which is key to helping you.

Thanks

-jim




[COLOR=#000000][COLOR=#007700]

[/COLOR]
[/COLOR]
Copy linkTweet thisAlerts:
@PeteWildauthorMar 03.2010 — Jim,

Thanks for some excellent advice there, and apologies for not posting dbconnect.

I have actually sorted it in the last half hour.

The site is still only a couple of days into development so the styling will be shunted to its own form. I also agree with your point over server side processing and I'll shift that to server side before its live, much appreciated you comments.

Many Thanks

PeteW
Copy linkTweet thisAlerts:
@SrWebDeveloperMar 03.2010 — Cheers. ?
×

Success!

Help @PeteWild 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 5.7,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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