0

After days of trial and error, I finally replaced my standard mysql code with PDO. Everything seems to be working just fine except for the last part where the app needs to INSERT user (name, email and time of signup) into database. After clicking submit, page just turns blank.

I don't see what is wrong with the code, so I would appreciate if you could help me out.

<?php

////Database connection values
$dsn = 'mysql:host=host; dbname=name; charset=utf8';
$db_user = 'username';
$db_pass = 'password';

//Database connection
$db = new PDO($dsn, $db_user, $db_pass); 
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // Enable Exception error mode
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); // Use PDO safely = Turn off prepare emulation

// Databse connection check
if($db){
 print "connected to the db " . "<br />";
 }

//Declare values
$name = "";
$email = "";
$userMsg = "";


if ($_POST['name'] != "") {


    $name = $_POST['name'];
    $email = $_POST['email'];

//MySQL SELECT Query    
    $stmt = $db -> prepare ("SELECT * FROM newsletter WHERE email=?");
    $stmt-> bindValue(1, $email);
    $stmt -> execute ();


//Error - No email  
    if (!$email) {

        $userMsg  = '<br /><br /><h4><font color="FF0000">Please type an email address ' . $name . '.</font></h4>';

        } // End email-input check

//Error - Email already in the system       
    else if ($stmt -> rowCount() > 0) {

        $userMsg  = '<br /><br /><h4><font color="FF0000">' . $email . ' is already in the system.</font></h4>';

        } // End Row check

//OK - insert user into database        
     else {

        $insert = $db -> prepare ("INSERT INTO newsletter (name, email, dateTime) VALUES(:name, :email, ,NOW())");
        $insert -> execute(array(':name' => $name, ':email' => $email));                                            

        //Success! - Notify user
        $userMsg  = '<br /><br /><h4><font color="0066FF">Thanks ' . $name . ', you have been added successfully.</font></h4>';

        $name = "";
        $email = "";

            } // End INSERT


}

?>
4
  • 1
    Why do you think insert not working? Commented Aug 5, 2013 at 16:22
  • :name, :email, ,NOW() remove the extra comma to this :name, :email,NOW() Commented Aug 5, 2013 at 16:23
  • One suggestion, you could use php empty() in your code, for example if (!empty($_POST['name'])) { Commented Aug 5, 2013 at 16:26
  • Thanks @sbml it was the extra comma, can't believe I didn't see it. And thanks for the tip! Your Common Sense - the app should display a message that the user was successfully added ($userMsg), as it does now. Commented Aug 5, 2013 at 16:31

1 Answer 1

1
VALUES(:name, :email, ,NOW())"                            

should be

VALUES(:name, :email, NOW())"
Sign up to request clarification or add additional context in comments.

3 Comments

I happen to fall on this answer/question after seeing another question the OP posted, and this proved to be the fault. Why someone downvoted your answer is beyond me, however I gave you a +1 back. The OP should have accepted your answer, but seeing his track record, seems to be an impossible task. Cheers
@Fred-ii- Originaly, this answer had something completely different written in it. Someone else answered the question in the comments. Afterwards, this answer was updated to the solution from comments. That's why the answer was downvoted (not by me) but that's why I didn't choose this answer as accepted. Also, if you're referring to me not accepting any answers on other questions, it's because they were either answered in the comments or there wasn't a viable solution to my problem.
but as of now this answer should be the accepted one, since it does work. that way when searching unswered pdo questions this one won't appear. It's a bit childish to downvote an answer taken from a comment. The point of this site is to answer question, as a matter of fact it's the answers posted as comments that should be unappreciated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.