Dear Members,
I am trying to come up with a system whereby people can register and then log in with their usernames and passwords.
However, when I tried using the code to register, I got the following error message “Error: User not added to database.” See below. I decided to enter the username, password, and e-mail address directly to the database dbUsers. But when I tried to login, I got the following error message “Sorry, could not log you in. Wrong login information.” See below.
Please could you tell me what I am doing wrong? Find the codes that I am using below.
Thank you.
--Frank
======================
Register.php
Username:
Password:
Email Address:
“Error: User not added to database.”
==================================
Login.php
Username:
Password:
“Sorry, could not log you in. Wrong login information.”
=========================================
This is the connection file:
<?php
$dbh=mysql_connect ("localhost", "lhsnycne_joe", "<password>") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("lhsnycne_mydatabase");
?>
==============================
This is register.php:
<?php
// dbConfig.php is a file that contains your
// database connection information. This
// tutorial assumes a connection is made from
// this existing file.
include ("dbConfig.inc");
$rs = mysql_select_db("lhsnycne_mydatabase",$dbh)
or die("error connecting to the database");
// ==== Input validation and PHP dBase code ===============================
//
// ====================================================
if ( $_GET["op"] == "reg" )
{
$bInputFlag = false;
foreach ( $_POST as $field )
{
if ($field == "")
{
$bInputFlag = false;
}
else
{
$bInputFlag = true;
}
}
// If we had problems with the input, exit with error
if ($bInputFlag == false)
{
die( "Problem with your registration info. "
."Please go back and try again.");
}
// Fields are clear, add user to database
// Setup query
$q = "INSERT INTO `dbUsers` (`username`,`password`,`email`) "
."VALUES ('".$_POST["username"]."', "
."PASSWORD('".$_POST["password"]."'), "
."'".$_POST["email"]."')";
// Run query
$r = mysql_query($q);
// Make sure query inserted user successfully
if ( !mysql_insert_id() )
{
die("Error: User not added to database.");
}
else
{
// Redirect to thank you page.
Header("Location: register.php?op=thanks");
}
} // end if
// ==== Thank you page ====================================================
//
// ======================================================
elseif ( $_GET["op"] == "thanks" )
{
echo "<h2>Thanks for registering!</h2>";
}
// ==== Main Form =======================================================
//
// ========================================================
else
{
echo "<form action=\"?op=reg\" method=\"POST\">\n";
echo "Username: <input name=\"username\" MAXLENGTH=\"16\"><br />\n";
echo "Password: <input type=\"password\" name=\"password\" MAXLENGTH=\"16\"><br />\n";
echo "Email Address: <input name=\"email\" MAXLENGTH=\"25\"><br />\n";
echo "<input type=\"submit\">\n";
echo "</form>\n";
}
// EOF
?>
=======================================
This is login.php:
<?php
session_start();
// dBase file
include "dbConfig.inc";
$rs = mysql_select_db("lhsnycne_mydatabase",$dbh)
or die("error connecting to the database");
if ($_GET["op"] == "login")
{
if (!$_POST["username"] || !$_POST["password"])
{
die("You need to supply a username and password.");
}
// Create query
$q = "SELECT * FROM `dbUsers` "
."WHERE `username`='".$_POST["username"]."' "
."AND `password`=PASSWORD('".$_POST["password"]."') "
."LIMIT 1";
// Run query
$r = mysql_query($q);
if ( $obj = @mysql_fetch_object($r) )
{
// Login O.K., create session variables
$_SESSION["valid_id"] = $obj->id;
$_SESSION["valid_user"] = $_POST["username"];
$_SESSION["valid_time"] = time();
// Redirect to member page
Header("Location: members.php");
}
else
{
// Login not successful
die("Sorry, could not log you in. Wrong login information.");
}
}
else
{
echo "<form action=\"?op=login\" method=\"POST\">";
echo "Username: <input name=\"username\" size=\"15\"><br />";
echo "Password: <input type=\"password\" name=\"password\" size=\"8\"><br />";
echo "<input type=\"submit\" value=\"Login\">";
echo "</form>";
}
?>
=================================
This is members.php:
<?php
session_start();
if (!$_SESSION["valid_user"])
{
// User not logged in, redirect to login page
Header("Location: login.php");
}
// Display Member-only stuff
// ...
// ...
// ...
// Display Member information
echo "<p>User ID: " . $SESSION["valid_id"];
echo "<p>Username: " . $SESSION["valid_user"];
echo "<p>Logged in: " . date("m/d/Y", $_SESSION["valid_time"]);
// Display logout link
echo "<p><a href=\"logout.php\">Click here to logout!</a></p>";
?>
=============================