I'm having a few problems with a script I'm coding.
<?php
// Project: Reward Hits
// File: Configuration
// -edit-
// Started on November 2nd, 2005 at 9:08PM
/* database */
$sql_username = "-edit-";
$sql_password = "-edit-";
$sql_hostname = "localhost";
$sql_database = "-edit-";
mysql_connect($sql_hostname, $sql_username, $sql_password);
mysql_select_db($sql_database);
/* functions */
function install() {
mysql_query("CREATE TABLE members(
id INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY(id),
firstname VARCHAR(30),
username VARCHAR(30),
password VARCHAR(30),
email VARCHAR(30),
cash INT)") or die(mysql_error());
mysql_query("CREATE TABLE logs(
id INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY(id),
user VARCHAR(30),
action VARCHAR(30),
date VARCHAR(30))") or die(mysql_error());
return "Success.";
}
function secure($input) {
$input = htmlspecialchars($input);
$input = addslashes($input);
$input = htmlentities($input);
$input = str_replace("'", "", $input);
$input = str_replace('"', '', $input);
$input = str_replace("&", "", $input);
$input = str_replace(":", "", $input);
$input = str_replace(";", "", $input);
$input = str_replace("*", "", $input);
$input = strip_tags($input);
$input = utf8_decode($input);
return $input;
}
function userexists($user) {
$username = secure($user);
$curr = mysql_query("SELECT * FROM members WHERE username='$username'") or die(mysql_error());
$data = mysql_fetch_array($curr) or die(mysql_error());
if ( $data[username] !== "" ) {
return true;
}else{
return false;
}
}
function newuser($firstname, $username, $password, $email) {
mysql_query("INSERT INTO members (firstname, username, password, email, cash) VALUES('$firstname', '$username', '$password', '$email', '0')");
}
/* test */
$firstname = "Mike";
$user = "user2";
$pass = "mypassword";
$email = "my@email.com";
if ( userexists($user) ) {
echo "Username is already taken. Please go back and choose a different username.";
}else{
newuser($firstname, $user, $pass, $email);
}
?>
I've yet to finish, though I'm testing certain functions as I go along.
As you can see at the bottom, it checks if the user already exists in the database, and if it does, it returns an error. If the user is not found already in the database, it creates the account, and does whatever. Now, if the user does in fact exist in the database, it DOES return the error. However, if the user does NOT in fact exist in the database, it does not return the error (as it shouldn't), however it does not add the user to the database with the newuser() function. I've tested the newuser() function on it's own, and it works fine.. I just don't see where the problem is!
Any help is appriciated, as I have deadlines.