Page 1 of 1

Activation Code Problems

Posted: Thu Jul 19, 2012 5:56 pm
by FrederickGeek8
I have followed this tutorial before and it has worked except I am now doing it again and everything is working except the activation code is not being inserted into the database. Whenever I register a user it goes into the database as 0. Help?
mysql_query("INSERT INTO `user_activations` (`user_id`, `activation_code`) VALUES ({$user_id}, '{$aid}')");

Re: Activation Code Problems

Posted: Thu Jul 19, 2012 6:45 pm
by Temor
One line of code is not going to help us solve this. Post all of your code and I'll take a look at it.

Re: Activation Code Problems

Posted: Thu Jul 19, 2012 6:55 pm
by FrederickGeek8
<?php
require_once "Mail.php";
// Checks if given username exits in db table
function user_exists($user){
	$user = mysql_real_escape_string($user);

	$total = mysql_query("SELECT COUNT(`user_id`) FROM `users` WHERE `user_name` = '{$user}'");

	return (mysql_result($total, 0) == '1') ? true : false;
}

// Checks if given email exits in db table
function email_exists($email){
	$user = mysql_real_escape_string($email);

	$total = mysql_query("SELECT COUNT(`user_id`) FROM `users` WHERE `user_email` = '{$email}'");

	return (mysql_result($total, 0) == '1') ? true : false;
}

// Checks if given username and password combo is valid
function valid_credentials($user, $pass){
	$user = mysql_real_escape_string($user);
	$pass = mysql_real_escape_string($pass);

	$total = mysql_query("SELECT COUNT(`user_id`) FROM `users` WHERE `user_name` = '{$user}' AND `user_password` = '{$pass}'");

	return (mysql_result($total, 0) == '1') ? true : false; 
}

// Adds user to db
function add_user($user, $email, $pass){
	$user   = mysql_real_escape_string(htmlentities($user));
	$email  = mysql_real_escape_string($email);
	$pass   = sha1($pass);
	
	$charset = array_flip(array_merge(range('a' ,'z'), range('A', 'Z'), range('0', '9')));
	$aid = implode('', array_rand($charset, 10));
	
	$from = "noreply@nerd.net";
    $subject = "Your new account at NERD.net!";
    $body = <<<EMAIL
	
    Hi,
    
    Thanks for registering at NERD.net. Before you login you need to activate your account.
    
    To do that simple click the link below.
    
    <a href='http://nerd.net/activate.php?aid={$aid} ... ={$aid}</a>

EMAIL;
    $host = "smtpout.secureserver.net";
    $port = "3535";
    $username = "noreply@nerd.net";
    $password = "<password>";
 
    $headers = array ('From' => $from,
   'To' => $to,
   'Subject' => $subject,
   'Content-type' => "text/html; charset=iso-8859-1");
    $smtp = Mail::factory('smtp',
            array ('host' => $host,
                'port' => $port,
                'auth' => true,
                'username' => $username,
                'password' => $password));
 
 $mail = $smtp->send($email, $headers, $body);
	

	mysql_query("INSERT INTO `users` (`user_name`, `user_password`, `user_email`) VALUES ('{$user}', '{$pass}', '{$email}')");
	
	$user_id = mysql_insert_id();
	
	mysql_query("INSERT INTO `user_activations` (`user_id`, `activation_code`) VALUES ({$user_id}, '{$aid}')");
}

?>

Re: Activation Code Problems

Posted: Thu Jul 19, 2012 7:11 pm
by Temor
I can't find anything wrong with the code.
What is the datatype set as in the table?

Re: Activation Code Problems

Posted: Thu Jul 19, 2012 7:31 pm
by FrederickGeek8
int(10)

Between this and my other problem, maybe there is a problem with MySQL 0_o

Re: Activation Code Problems

Posted: Thu Jul 19, 2012 7:52 pm
by Temor
the problem is that you're setting the datatype to int, and you try to insert a varchar string.
the random string you're producing will look something like this: dpwAGHOQY9 which is not only integers.

change the datatype to varchar(10) and it should work.

Re: Activation Code Problems

Posted: Thu Jul 19, 2012 8:30 pm
by FrederickGeek8
Thanks :) That worked :)