Jump to content

[SOLVED] New to MySQL, trying to add a fail error to simple login


ajsuk

Recommended Posts

Here there, I'm _very_ new to MySQL (as in started today) so please be gentle hehe.

I'm playing around with making a simple login system using a form to check login details and display results belonging to that user if login is correct. I've got this all working fine but ofcourse if the details are wrong it'd be nice if I could get it to print something rather than the user being left with a blank page.

I'm guessing I need to check the form results (user and pass) against the the database records. I've tried this with an if statement but it doesn't seem to want to know.

Any help would be great, thanks in advance! =)

Heres my poor n00b attempt:
[code]<?php
$nameresult = $_POST['name'];
$passresult = $_POST['pass'];

$con = mysql_connect("localhost","<dbnamehere>","<dbpasshere>");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("db_testing", $con);

$result = mysql_query("SELECT * FROM info WHERE name='$nameresult' && pass='$passresult'");

while($row = mysql_fetch_array($result))
  {
  echo "Welcome ".$row['name'] . " - " . $row['email'] . " - " . $row['pass'];
  echo "<br />";
 
if ($nameresult != $row['name'] && $passresult != $row['pass'])
  echo "login Bad";
  }

mysql_close($con);
?>[/code]
Link to comment
Share on other sites

[code=php:0]
if (mysql_num_rows($result) > 0) {
  $row = mysql_fetch_array($result) {
  echo "Welcome ".$row['name'] . " - " . $row['email'] . " - " . $row['pass'];
  echo "<br />";
} else {
  echo "login Bad";
}
[/code]
Link to comment
Share on other sites

if you just want to authenticate the user, ie. check to see if the un/pw is a valid pair:

$sql = "select count(*) from info where name='$nameresult' and pass='$passresult'";
$rs = mysql_query($sql);
$row = mysql_fetch_row($rs);
$exist = $row[0];
if ($exist == 1)
{
  // user is successfully authenticated.
}
else
{
  // failed to authenticate
}
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.