1

I am having some problems with my insert.

I have this code below, and when I click on submit button to subscribe I don't get any error, but I don't receive the insert on my database.

It's a strange situation, because everything seems correct:

-> I don't have any error message

-> if I do echo echo $email; and echo $code; the variables have the right value

-> the name of the table is correct

Can you see something here that can be giving this issue?

My Php:

 <?php 

    if(isset($_POST['newsletter_subscription']) && $_POST['newsletter_subscription'] == 'register')
      {
        $email = $_POST['email'];
        $code = md5($email);

        try
          {                 
            $subscription = $pdo->prepare("INSERT into subscriptions (email,code,status) VALUES = :email,:code,:status");  
            $subscription->bindValue(":email", $email);  
            $subscription->bindValue(":code", $code); 
            $subscription->bindValue(":status", 'inactive'); 
            $subscription->execute();
           }
         catch(PDOException $e)
           {
            $e->getmessage();
           }                        
        }
    ?>

My Html form:

 <form action="" name="newsletter" method="post" enctype="multipart/form-data">
     <label>
         <input type="text"id="email2" name="email" placeholder="  e-mail"  required/>
     </label>
     <br />
     <label id="submit">
         <input type="hidden" name="newsletter_subscription" value="register" />
         <input type="submit" name="register" value="Register" src="" />
      </label>
</form>
larsAnders
  • 3,813
  • 1
  • 15
  • 19
OzzC
  • 815
  • 4
  • 20
  • 36

2 Answers2

3

Your INSERT syntax is slightly off:

"INSERT into subscriptions (email,code,status) VALUES = :email,:code,:status"

should be

"INSERT into subscriptions (email,code,status) VALUES (:email,:code,:status)"
larsAnders
  • 3,813
  • 1
  • 15
  • 19
  • Thank you! It´s that the correct answer. But dont you think its strange I didnt receive the error with my try-catch block? – OzzC Mar 28 '14 at 16:40
  • Yes, you might need to add `error_reporting(E_ALL);` to the top of your script in order to see the error. – larsAnders Mar 28 '14 at 16:41
  • @larsAnders You can also use the [PDO errorCode](http://www.php.net/manual/en/pdo.errorcode.php) and [PDO errorInfo](http://www.php.net/manual/en/pdo.errorinfo.php) from now on. – lxndr Mar 28 '14 at 16:44
2

You're using $codigo in the sql but then $code in the bind.

Also the INSERT is meant to look like this:

$subscription = $pdo->prepare("INSERT into subscriptions (email,code,status) VALUES (:email,:code,:status)")

Overall:

try
{                 
  $subscription = $pdo->prepare("INSERT into subscriptions (email,code,status) VALUES (:email,:code,:status)");  
  $subscription->bindValue(":email", $email);  
  $subscription->bindValue(":code", $code); 
  $subscription->bindValue(":status", 'inactive'); 
  $subscription->execute();
 }
 catch(PDOException $e)
 {
   $e->getmessage();
 }
Albzi
  • 15,431
  • 6
  • 46
  • 63