Here is the output of the echo's:
Connected to database!
inside create user
here is user ID: test1
This is the query: SELECT userID FROM USERS WHERE userID = 'test1'
This is the result of query: test1
this is userid: test1
in the else
You are a new user!
Thank you for joining our website.
Invalid new user insert Duplicate entry 'test1' for key 1
So here is my function's code:
Code: Select all
function create_user($userid, $password, $email, $security1, $answer1, $security2, $answer2){
//establish connection to database
$db = mysql_connect('localhost', 'username', 'password');
if($db):
mysql_select_db('PogoersckeyJ');
echo "Connected to database!<br />";
else:
die ("Could not connect to db! " . mysql_error());
endif;
echo "inside create user<br />";
echo "here is user ID: ".$userid."<br />";
//possible that user forget's password and doesn't realize their account already exists
//check to see if the user id already exists in the database
$sql= "SELECT userID FROM USERS WHERE userID = '$userid'";
echo "This is the query: $sql <br />";
#$sql= sprintf("SELECT userID FROM USERS WHERE userID = '%s'", $userid);
$result = mysql_query($sql) or die ("Error searching for user id's, Invalid: " . mysql_error());
while($row = mysql_fetch_array($result)){
echo "This is the result of query: ".$row['userID'];
echo "<br />";
}
echo "this is userid: $userid <br />";
//compare the id's if equal user exsits, false it is a new user to the database
if(rtrim(trim($row['userID'])) == rtrim(trim($userid))){
echo "not in the if";
#unset($GLOBALS[$_SESSION["option"]]);
#unset($_SESSION["info"]);
#unset($_SESSION["history"]);
session_unset();
//session_destroy();
mysql_close($db);
echo "The account already exists, please user a different User ID!<br />";
echo "You will be re-directed to New User page in 5 seconds... <br /><br />";
header( 'refresh: 10b; url=https://cs1520.cs.pitt.edu/~jap97/mysql/newuser.php');
#return false;
}
else{
echo "in the else<br />";
echo "You are a new user!<br />";
echo "Thank you for joining our website. <br />";
$md5password = md5($password);
$md5answer1 = md5($answer1);
$md5answer2 = md5($answer2);
$query = "INSERT INTO USERS VALUES ('$userid', '$md5password', '$email', '$security1' , '$md5answer1' , '$security2' , '$md5answer2')";
mysql_query($query) or die ("Invalid new user insert " . mysql_error());
#unset($GLOBALS[$_SESSION["option"]]);
# unset($_SESSION["info"]);
# unset($_SESSION["history"]);
session_unset();
//session_destroy();
login_user($userid, $password);
mysql_close($db);
echo "You will be re-directed to Login page in 5 seconds... <br />";
header( 'refresh: 5; url=http://cs1520.cs.pitt.edu/~jap97/mysql/textexchange.php');
#return true;
}
}//end of create_user