Hello!
I really need some help. I have been struggling with my scripts for days now and I hope that someone can figure out the mistakes I have made.
register-exec
PHP Code:
<?php
//Start session
session_start();
//Include database connection details
require_once('config.php');
//Array to store validation errors
$errmsg_arr = array();
//Validation error flag
$errflag = false;
//Connect to mysql server
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$link) {
die('Failed to connect to server: ' . mysql_error());
}
//Select database
$db = mysql_select_db(DB_DATABASE);
if(!$db) {
die("Unable to select database");
}
//Function to sanitize values received from the form. Prevents SQL injection
function clean($str) {
$str = @trim($str);
if(get_magic_quotes_gpc()) {
$str = stripslashes($str);
}
return mysql_real_escape_string($str);
}
//Sanitize the POST values
$confirmation_code = md5(uniqid(rand()));
$fname = clean($_POST['fname']);
$lname = clean($_POST['lname']);
$login = clean($_POST['login']);
$password = clean($_POST['password']);
$cpassword = clean($_POST['cpassword']);
//Input Validations
if($fname == '') {
$errmsg_arr[] = 'First name missing';
$errflag = true;
}
if($lname == '') {
$errmsg_arr[] = 'Last name missing';
$errflag = true;
}
if($address == '') {
$errmsg_arr[] = 'Address missing';
$errflag = true;
}
if($city == '') {
$errmsg_arr[] = 'City missing';
$errflag = true;
}
if($postalcode == '') {
$errmsg_arr[] = 'Postalcode missing';
$errflag = true;
}
if($state == '') {
$errmsg_arr[] = 'State/Province missing';
$errflag = true;
}
if($country == '') {
$errmsg_arr[] = 'Country missing';
$errflag = true;
}
if($login == '') {
$errmsg_arr[] = 'Login ID missing';
$errflag = true;
}
if($password == '') {
$errmsg_arr[] = 'Password missing';
$errflag = true;
}
if($cpassword == '') {
$errmsg_arr[] = 'Confirm password missing';
$errflag = true;
}
if( strcmp($password, $cpassword) != 0 ) {
$errmsg_arr[] = 'Passwords do not match';
$errflag = true;
}
//Check for duplicate login ID
if($login != '') {
$qry = "SELECT * FROM temp WHERE login='$login'";
$result = mysql_query($qry);
if($result) {
if(mysql_num_rows($result) > 0) {
$errmsg_arr[] = 'Login ID already in use';
$errflag = true;
}
@mysql_free_result($result);
}
else {
die("Query failed");
}
}
//If there are input validations, redirect back to the registration form
if($errflag) {
$_SESSION['ERRMSG_ARR'] = $errmsg_arr;
session_write_close();
header("location: register-form.php");
exit();
}
//Create INSERT query
$qry = "INSERT INTO temp(code, firstname, lastname, address, city, postalcode, state, country, email, login, passwd) VALUES('$confirmation_code','$fname','$lname','$address','$city','$postalcode','$state','$country','$email','$login','".md5($_POST['password'])."')";
$result = @mysql_query($qry);
//Check whether the query was successful or not
if($result) {
$message="Your Confirmation link \r\n";
$message.="Click on this link to activate your account \r\n";
$message.="http://www.------------.com/confirmation.php?pass_key=$confirmation_code";
$sent_mail = mail("$email", "Registration Confirmation", "$message");
}
else
{
echo "Not found your e-mail in our database";
}
if($sent_mail)
{
echo "Your Confirmation link has been sent to your e-mail account";
}
else
{
echo "cannot send confirmation link to your e-mail adress";
}
?>
confirmation.php
PHP Code:
<?php
//Start session
session_start();
//Include database connection details
require_once('config.php');
$passkey=$_GET['pass_key'];
//Array to store validation errors
$errmsg_arr = array();
//Validation error flag
$errflag = false;
//Connect to mysql server
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$link) {
die('Failed to connect to server: ' . mysql_error());
}
//Select database
$db = mysql_select_db(DB_DATABASE);
if(!$db) {
die("Unable to select database");
}
//Function to sanitize values received from the form. Prevents SQL injection
function clean($str) {
$str = @trim($str);
if(get_magic_quotes_gpc()) {
$str = stripslashes($str);
}
return mysql_real_escape_string($str);
}
//Create INSERT query
$qry1="SELECT * FROM temp(code) VALUES('$confirmation_code')";
$result=@mysql_query($qry1);
if($result)
{
$count=mysql_num_rows($result);
if($count==1)
{
$rows=mysql_fetch_array($result);
$fnamex=$rows['firstname'];
$emailx=$rows['email'];
$passwordx=$rows['passwd'];
$fname=str_replace(' ','',$fnamex);
$email=str_replace(' ','',$emailx);
$password=str_replace(' ','',$passwordx);
$qry2 = "INSERT INTO members(firstname, lastname, address, city, postalcode, state, country, email, login, passwd) VALUES('$fname','$lname','$address','$city','$postalcode','$state','$country','$email','$login','".md5($_POST['password'])."')";
$result2=@mysql_query($qry2);
}
else
{echo "wrong confirmation code";}
if($result2){
header("Location:confirmation2.html");
}
}
?>
So what's my problem? When someone enters their information in my register form they receive a confirmation email with a link. When they click on the link their data should go from my mysql database called temp to the database called 'members'. However, this doesn't happen.
I am just a starter with php so hopefully you can help me!
Thank you so much in advance!
Bookmarks