Click here to Skip to main content
15,354,641 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
PHP
<pre>
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

include 'includes/session.php';

if (isset($_POST['signup'])) {
	$firstname = $_POST['firstname'];
	$lastname = $_POST['lastname'];
	$email = $_POST['email'];
	$password = $_POST['password'];
	$repassword = $_POST['repassword'];

	$_SESSION['firstname'] = $firstname;
	$_SESSION['lastname'] = $lastname;
	$_SESSION['email'] = $email;

	if ($password != $repassword) {
		$_SESSION['error'] = 'Passwords did not match';
		header('location: signup.php');
	} else {
		$conn = $pdo->open();

		$stmt = $conn->prepare("SELECT COUNT(*) AS numrows FROM users WHERE email=:email");
		$stmt->execute(['email' => $email]);
		$row = $stmt->fetch();
		if ($row['numrows'] > 0) {
			$_SESSION['error'] = 'Email already taken';
			header('location: signup.php');
		} else {
			$now = date('Y-m-d');
			$password = password_hash($password, PASSWORD_DEFAULT);

			//generate code
			$set = '123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
			$code = substr(str_shuffle($set), 0, 12);

			try {
				$stmt = $conn->prepare("INSERT INTO users (email, password, firstname, lastname, activate_code, created_on) VALUES (:email, :password, :firstname, :lastname, :code, :now)");
				$stmt->execute(['email' => $email, 'password' => $password, 'firstname' => $firstname, 'lastname' => $lastname, 'code' => $code, 'now' => $now]);
				$userid = $conn->lastInsertId();

				$message = "
						Registered!
					";

				//Load phpmailer
				require 'vendor/autoload.php';

				$mail = new PHPMailer(true);
				try {
					//Server settings
					$mail->isSMTP();
					$mail->Host = 'smtp.gmail.com';
					$mail->SMTPAuth = true;
					$mail->Username = 'test@gmail.com';
					$mail->Password = 'aaaaaa';
					$mail->SMTPOptions = array(
						'ssl' => array(
							'verify_peer' => false,
							'verify_peer_name' => false,
							'allow_self_signed' => true
						)
					);
					$mail->SMTPSecure = 'ssl';
					$mail->Port = 465;

					$mail->setFrom('test@gmail.com');

					//Recipients
					$mail->addAddress($email);
					$mail->addReplyTo('test@gmail.com');

					//Content
					$mail->isHTML(true);
					$mail->Subject = 'ECommerce Site Sign Up';
					$mail->Body    = $message;

					$mail->send();

					unset($_SESSION['firstname']);
					unset($_SESSION['lastname']);
					unset($_SESSION['email']);

					$_SESSION['success'] = 'Account created. Check your email to activate.';
					header('location: signup.php');
				} catch (Exception $e) {
					$_SESSION['error'] = 'Message could not be sent. Mailer Error: ' . $mail->ErrorInfo;
					header('location: signup.php');
				}
			} catch (PDOException $e) {
				$_SESSION['error'] = $e->getMessage();
				header('location: register.php');
			}

			$pdo->close();
		}
	}
} else {
	$_SESSION['error'] = 'Fill up signup form first';
	header('location: signup.php');
}


What I have tried:

The email checking query is working normally as well as other queries such as login.

The problem is with the insert query.
Posted
Updated 27-Oct-21 15:04pm
v2
Comments
Richard MacCutchan 28-Oct-21 4:27am
   
"The problem is with the insert query."
What problem?
Member 15413331 31-Oct-21 6:56am
   
Why are you using : for the variables and not $? When i tried your code it didn't work with
:email, :password, :firstname, :lastname, :code, :now
but
$email, $password, $firstname, $lastname, $code, $now
did.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900