0

I am new to PDO and keep getting a Fatal error. I am trying to first check for empty fields, then check for duplicate emails and then if that passes insert the user data into the database. After searching and searching I am absolutely lost as to where I am going wrong. Here is my code:

<?php
session_start();

require_once('includes/db_connect.php');
include('functions/email-inject-function.php');

$first_name = trim($_POST['first_name']);
$last_name = trim($_POST['last_name']);    
$email = trim($_POST['email']);
$company = trim($_POST['company']);
$phone = trim($_POST['phone']);
$password = trim($_POST['password']);

if ($_SERVER['REQUEST_METHOD'] == 'POST') {

 if(empty($_POST["first_name"])) {
   $first_name_err = "<p>What is your first name?</p>";
   $errorflag = 1;
 }
 if(empty($_POST["last_name"])) {
   $last_name_err = "<p>What is your last name?</p>";
   $errorflag = 1;
 }
 //checks email
 if(empty($_POST["email"])) {
   $email_err = "<p>What is your email address?</p>";
   $errorflag = 1;
 }
  if(empty($_POST["company"])) {
   $company_err = "<p>What is your company name?</p>";
   $errorflag = 1;
 }
   if(empty($_POST["phone"])) {
   $phone_err = "<p>What is your phone number?</p>";
   $errorflag = 1;
 }
   if(empty($_POST["password"])) {
   $pass_err = "<p>Please enter a password</p>";
   $errorflag = 1;
 }
  else {
   $injected = IsInjected($email);
   if ($injected == true) {
   $email_valid_err = "<p>Please enter a valid email.</p>";
   $errorflag = 1;
   }
 }
 try {
  // Check if email is taken
  $stmt = $dbh->prepare("SELECT * FROM `admins` WHERE `email` = :email");
  $stmt->execute(array('email' => $email));
  if ($stmt->fetchColumn() > 0) {
    throw new Exception("That email is already taken.");
    }
   $sql="INSERT INTO admins (first_name, last_name, email, company, phone, password, reg_date) VALUES (:first_name, :last_name, :email, :company, :phone, SHA1('$password'), NOW())";
   $query = $dbh->prepare($sql);
   $result->execute(array(':first_name'=>$first_name, ':last_name'=>$last_name, ':email'=>$email, ':company'=>$company, ':phone'=>$phone, ':password'=>$password ));
   echo $result;   

    //catch any errors from try()
    }
    catch(PDOException $e)
    {
    echo $e->getMessage();
    }
}
?>
tonjaggart
  • 81
  • 1
  • 1
  • 13

1 Answers1

3

Its a simple mistake:

replace $result with $query....

So:

$result->execute(array(':first_name'=>$first_name, ':last_name'=>$last_name, ':email'=>$email, ':company'=>$company, ':phone'=>$phone, ':password'=>$password ));
echo $result; 

should be:

$query->execute(array(':first_name'=>$first_name, ':last_name'=>$last_name, ':email'=>$email, ':company'=>$company, ':phone'=>$phone, ':password'=>$password ));
echo $query; 

the Query is also wrong:

$sql="INSERT INTO admins (first_name, last_name, email, company, phone, password, reg_date) VALUES (:first_name, :last_name, :email, :company, :phone, SHA1('$password'), NOW())";

should be

$sql="INSERT INTO admins (first_name, last_name, email, company, phone, password, reg_date) VALUES (:first_name, :last_name, :email, :company, :phone, SHA1(:password), NOW())";

Note the $password to :password

Langusten Gustel
  • 10,917
  • 9
  • 46
  • 59