ultramarine11
05-13-2006, 09:22 AM
Hi,
I am new to PHP and mysql and could do with some help. I am trying to set up a bookshare site. Its gunna be basic while i learn. I can get my users to register and store the data in the database. However I am having trouble getting them to log in!
My database consists of three tables, Students, Books, and Studentsbooks.
Students has basic info like StudentID (Primary Key) and Email, Username etc.
Books contain BookID and data such as Title, Author etc Studentsbooks consists of BookID and StudentID with PRIMARY KEY (StudentID, BookID).
Here is my code, but it just wont work can somebody PLEASE :) point out where i am going wrong?
// Enables the use of session
session_start();
// Verifies if user has signed
$signed = ($_SESSION['signin']=="YES");
if($signed) {
header( "Location: index.php" );
exit();
}
$error =""; // error variable will be used later on.
// For safety, we empty it now.
// If we are processing the form...
if($_POST["submit"]=="Login") {
$dbcnx = @mysql_connect('localhost', 'christopher-foster_com', 'exorcist1');
if (!$dbcnx) {
exit ('<p>Unable to connect to the ' .
'database server at this time.</p>');
}
// Select the Book Share database
mysql_select_db('christopher-foster_com_1');
if (!@mysql_select_db('christopher-foster_com_1')) {
exit('<p>Unable to locate the ' .
'Book Share database at this time.</p>');
$query = "SELECT Username, Password FROM Students WHERE Username='" . $_POST['$Username'] . "' AND " .
"Password='" . $_POST['$Password'] ."'";
$result = mysql_query($query) or die("ERROR: " . mysql_error());
if ($row = mysql_fetch_array($result)) {
// If login and password are correct...
$_SESSION['signin'] = "YES";
$_SESSION['username'] = $_POST['Username'];
$_SESSION['time'] = date("r");
header("Refresh: 4;URL=index.php");
print "<h1>Welcome, ". $_SESSION['username'] . "!</h1>";
print "You'll be redirected to the index page!";
exit();
}
else {
$error = "<font color='red'>Invalid Login and Password!</font><br />";
}
}
</PHP>
<PHP>include ('header.html');</PHP>
<h1>Login</h1>
<form action="<?php echo $PHP_SELF; ?>" method="post">
<fieldset>
<p><b>Username:</b> <input type="text" name="Username" size="15" maxlength="15" /></p>
<p><b>Password:</b> <input type="password" name="Password" size="15" maxlength="15"/></p>
</fieldset>
<br/>
<div align="left"><input type="submit" name="submit" value="Login" /></div>
<input type="hidden" name="submitted" value="TRUE" />
</form>
<PHP> include ('footer.html');
I am new to PHP and mysql and could do with some help. I am trying to set up a bookshare site. Its gunna be basic while i learn. I can get my users to register and store the data in the database. However I am having trouble getting them to log in!
My database consists of three tables, Students, Books, and Studentsbooks.
Students has basic info like StudentID (Primary Key) and Email, Username etc.
Books contain BookID and data such as Title, Author etc Studentsbooks consists of BookID and StudentID with PRIMARY KEY (StudentID, BookID).
Here is my code, but it just wont work can somebody PLEASE :) point out where i am going wrong?
// Enables the use of session
session_start();
// Verifies if user has signed
$signed = ($_SESSION['signin']=="YES");
if($signed) {
header( "Location: index.php" );
exit();
}
$error =""; // error variable will be used later on.
// For safety, we empty it now.
// If we are processing the form...
if($_POST["submit"]=="Login") {
$dbcnx = @mysql_connect('localhost', 'christopher-foster_com', 'exorcist1');
if (!$dbcnx) {
exit ('<p>Unable to connect to the ' .
'database server at this time.</p>');
}
// Select the Book Share database
mysql_select_db('christopher-foster_com_1');
if (!@mysql_select_db('christopher-foster_com_1')) {
exit('<p>Unable to locate the ' .
'Book Share database at this time.</p>');
$query = "SELECT Username, Password FROM Students WHERE Username='" . $_POST['$Username'] . "' AND " .
"Password='" . $_POST['$Password'] ."'";
$result = mysql_query($query) or die("ERROR: " . mysql_error());
if ($row = mysql_fetch_array($result)) {
// If login and password are correct...
$_SESSION['signin'] = "YES";
$_SESSION['username'] = $_POST['Username'];
$_SESSION['time'] = date("r");
header("Refresh: 4;URL=index.php");
print "<h1>Welcome, ". $_SESSION['username'] . "!</h1>";
print "You'll be redirected to the index page!";
exit();
}
else {
$error = "<font color='red'>Invalid Login and Password!</font><br />";
}
}
</PHP>
<PHP>include ('header.html');</PHP>
<h1>Login</h1>
<form action="<?php echo $PHP_SELF; ?>" method="post">
<fieldset>
<p><b>Username:</b> <input type="text" name="Username" size="15" maxlength="15" /></p>
<p><b>Password:</b> <input type="password" name="Password" size="15" maxlength="15"/></p>
</fieldset>
<br/>
<div align="left"><input type="submit" name="submit" value="Login" /></div>
<input type="hidden" name="submitted" value="TRUE" />
</form>
<PHP> include ('footer.html');