Activation Code Problems

Post here is you are having problems with any of the tutorials.
Post Reply
User avatar
FrederickGeek8
Posts: 148
Joined: Wed Nov 30, 2011 10:31 pm

Activation Code Problems

Post 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}')");
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: Activation Code Problems

Post 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.
User avatar
FrederickGeek8
Posts: 148
Joined: Wed Nov 30, 2011 10:31 pm

Re: Activation Code Problems

Post 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}')");
}

?>
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: Activation Code Problems

Post by Temor »

I can't find anything wrong with the code.
What is the datatype set as in the table?
User avatar
FrederickGeek8
Posts: 148
Joined: Wed Nov 30, 2011 10:31 pm

Re: Activation Code Problems

Post by FrederickGeek8 »

int(10)

Between this and my other problem, maybe there is a problem with MySQL 0_o
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: Activation Code Problems

Post 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.
User avatar
FrederickGeek8
Posts: 148
Joined: Wed Nov 30, 2011 10:31 pm

Re: Activation Code Problems

Post by FrederickGeek8 »

Thanks :) That worked :)
Post Reply