My program is suppose to store the e-mail of a user and allow them to login in if it's a new e-mail or if the e-mail has already been used to login, the program is to alert the user that the e-mail has already been logged in. My issue is the e-mail does not seem to be stored in the table in order to validate if it's been used before. Every time I log in with the same credentials, I get the congratulations statement instead of the Error statement. Does anyone see where I am incorrect? Perhaps my syntax (I've been starring at this for hours and I can not seem to pick out the issue). Thanks in advanced! Any input is appreciated!
dbparams.txt
username=sdev_owner
password=sdev300
host=localhost
db=sdev
Disc_7.php
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Discussion 7 </title>
</head>
<body OnLoad="document.createlogin.uname.focus();">
<?php
if(isset($_POST["CreateSubmit"])) {
validate_form();
} else {
$messages = array();
show_form($messages);
}
?>
<?php
function show_form($messages) {
// Assign post values if exist
$uname="";
$email="";
if (isset($_POST["uname"]))
$uname=$_POST["uname"];
if (isset($_POST["email"]))
$email=$_POST["email"];
echo "<p></p>";
echo "<h2> Enter Login Information</h2>";
echo "<p></p>";
?>
<form name="createlogin" method="POST" action="Disc_7.php">
<table border="1" width="100%" cellpadding="0">
<tr>
<td width="157">Username:</td>
<td><input type="text" name="uname" value='<?php echo $uname ?>' size="30"></td>
</tr>
<tr>
<td width="157">Email:</td>
<td><input type="text" name="email" value='<?php echo $email ?>' size="30"></td>
</tr>
<tr>
<td width="157"><input type="submit" value="Submit" name="CreateSubmit"></td>
<td> </td>
</tr>
</table>
</form>
<?php
}
?>
<?php
function validate_form(){
$messages = array();
$redisplay = false;
$uname = $_POST["uname"];
$email = $_POST["email"];
$login = new LoginClass($uname,$email);
$count = countLogin($login);
if ($count==0) {
$res = insertLogin($login);
echo "<h3>Congratulations, you've logged in!</h3> ";
} else {
echo "<h3>Error: A login with that e-mail has already logged in.</h3> ";
}
}
function countLogin ($login){
$mysqli = connectdb();
$uname = $login->getUname();
$email = $login->getEmail();
$mysqli = connectdb();
$Myquery = "SELECT count(*) as count from LoginData where eMail='$email'";
if ($result = $mysqli->query($Myquery)) {
while( $row = $result->fetch_assoc()) {
$count=$row["count"];
}
$result->close();
}
$mysqli->close();
return $count;
}
function insertLogin ($login){
$mysqli = connectdb();
$uname = $login->getUname();
$email = $login->getEmail();
$Query = "INSERT INTO LoginData (userName,eMail) VALUES ('$uname', '$email')";
$Success=false;
if ($result = $mysqli->query($Query)) {
$Success=true;
}
$mysqli->close();
return $Success;
}
function getDbparms(){
$trimmed = file('parms/dbparms.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$key = array();
$vals = array();
foreach($trimmed as $line){
$pairs = explode("=",$line);
$key[] = $pairs[0];
$vals[] = $pairs[1];
}
$mypairs = array_combine($key,$vals);
$myDbparms = new DbparmsClass($mypairs['username'],$mypairs['password'], $mypairs['host'],$mypairs['db']);
return $myDbparms;
}
function connectdb() {
$mydbparms = getDbparms();
$mysqli = new mysqli($mydbparms->getHost(), $mydbparms->getUsername(),$mydbparms->getPassword(),$mydbparms->getDb());
if ($mysqli->connect_error) {
die('Connect Error (' . $mysqli->connect_errno . ') '
. $mysqli->connect_error);
}
return $mysqli;
}
class DBparmsClass
{
private $username="";
private $password="";
private $host="";
private $db="";
public function __construct($myusername,$mypassword,$myhost,$mydb)
{
$this->username = $myusername;
$this->password = $mypassword;
$this->host = $myhost;
$this->db = $mydb;
}
public function getUsername ()
{
return $this->username;
}
public function getPassword ()
{
return $this->password;
}
public function getHost ()
{
return $this->host;
}
public function getDb ()
{
return $this->db;
}
// Set methods
public function setUsername ($myusername)
{
$this->username = $myusername;
}
public function setPassword ($mypassword)
{
$this->password = $mypassword;
}
public function setHost ($myhost)
{
$this->host = $myhost;
}
public function setDb ($mydb)
{
$this->db = $mydb;
}
} // End DBparms class
// Class to construct Students with getters/setter
class LoginClass
{
// property declaration
private $uname="";
private $email="";
// Constructor
public function __construct($uname,$email)
{
$this->uname = $uname;
$this->email = $email;
}
// Get methods
public function getUname ()
{
return $this->uname;
}
public function getEmail ()
{
return $this->email;
}
// Set methods
public function setUname ($value)
{
$this->uname = $value;
}
public function setEmail ($value)
{
$this->email = $value;
}
}
?>
</body>
</html>