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]
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
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.