0

I have hit a brick wall on trying to figure out why this code will insert sometimes and other times it won't. There are no error messages or error logs.

The code logic is if the email record is in the email table then it then it will get the id and insert into the customer table. If email is not found then it will insert into email table then customer.

<?php

/* Default */

$terms               =  $_POST['terms'];
$ipAddress        = $_SERVER['REMOTE_ADDR'];

/* Database conections */

include 'config.php';

try
{
    $conn   =   new PDO('mysql:host=localhost;dbname='.$config['dataBaseName'], $config['userName'], $config['password']);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $e)
{
    echo 'Connection failded' . $e->getMessage();
}


/* Post data */


if (isset($_POST["firstName"]))
{
       $firstName = trim(ucfirst($_POST["firstName"]));
}

if (isset($_POST["email"]))
{
       $email = trim(ucfirst($_POST["email"]));
}

if (isset($_POST["postCode"]))
{
       $postCode = trim(strtoupper($_POST["postCode"]));
}

if (isset($_POST["url"]))
{
        $url      =  trim(ucfirst($_POST["url"]));
}
if (isset($_POST["terms"]))
{
        $terms     =  trim(ucfirst($_POST["terms"]));
}


/* Check if email is in database returns array */

try
{

echo 'Finding Email ->' .$email;

$stmt   = $conn->prepare("SELECT id,email FROM email WHERE email = :email");
$stmt->bindParam(':email',$email);
$stmt->execute();
$result = $stmt->fetch(PDO::FETCH_ASSOC);

}

catch (PDOException $e)
{
    echo 'Check if email is in table' . $e->getMessage();
}


/* Insert Logic + functions */


try
{

if(empty($result)  or is_null($result))
{
    /* Insert into email table */
    $stmt   = $conn->prepare("INSERT INTO email (`email`) values(:email)");
    $stmt->bindParam(':email',$email);
    $stmt->execute();
    /* grab id from email table */

    $stmt   = $conn->prepare("SELECT id FROM email WHERE email = :email");
    $stmt->bindParam(':email',$email);
    $stmt->execute();
    $result = $stmt->fetch(PDO::FETCH_ASSOC);
    $result =  $result["id"];   

    /* insert into customer table */

    $stmt   = $conn->prepare(
        "INSERT INTO customer (`emailid`, `firstname`, `postcode`, `terms`, `browser`, `ipaddress`)
         VALUES(:emailid, :firstname, :postcode,  :terms, :browser, :ipaddress)");
        $stmt->bindParam(':emailid',$result);
        $stmt->bindParam(':firstname',$firstName);
        $stmt->bindParam(':postcode',$postCode);
        $stmt->bindParam(':terms',$terms);
        $stmt->bindParam(':browser',$url);
        $stmt->bindParam(':ipaddress',$ipAddress);
        $stmt->execute();
}
else
{
    /* grabs id from email table */

    $stmt   = $conn->prepare("SELECT id FROM email WHERE email = :email");
    $stmt->bindParam(':email',$email);
    $stmt->execute();
    $result = $stmt->fetch(PDO::FETCH_ASSOC);
    $result =  $result["id"];   

    /* insert into customer table */

    $stmt   = $conn->prepare(
        "INSERT INTO customer (`emailid`, `firstname`, `postcode`,  `terms`, `browser`, `ipaddress`)
         VALUES(:emailid, :firstname, :postcode, :terms, :browser, :ipaddress)");
        $stmt->bindParam(':emailid',$result);
        $stmt->bindParam(':firstname',$firstName);
        $stmt->bindParam(':postcode',$postCode);
        $stmt->bindParam(':terms',$terms);
        $stmt->bindParam(':browser',$url);
        $stmt->bindParam(':ipaddress',$ipAddress);
        $stmt->execute();     
}


$conn->commit();


}
catch (PDOException $e)
{
    echo 'Insert error -->' . $e->getMessage();
}


?>

0 Answers0