1

Thanks in advance for your help. I have a landing page with an AJAX email collection form in the first section which I have adapted from this Tutsplus tutorial.

However, the PHP section of the form is written in MySQL which is not compatible with my Heroku server as it is deprecated. Taking advice from others on stackoverflow, I tried converting the MySQL statements in my code to PDO. But have difficulties getting the form to work.

Here is the original working MySQL code

<?php
if($_GET['action'] == 'signup'){
// if(isset($_GET['action'])&& $_GET['action'] == 'signup'){

 mysql_connect('hostname','username','password');
 mysql_select_db('databasename');

 $email = mysql_real_escape_string($_POST['emailfield']);

 //validate email address - check if input was empty
 if(empty($email)){
  $status = "error";
  $message = "You did not enter an email address!";
 }
 else if(!filter_var($email, FILTER_VALIDATE_EMAIL)){ //validate email address - check if is a valid email address
   $status = "error";
   $message = "You have entered an invalid email address!";
 }
 else {
  $existingSignup = mysql_query("SELECT * FROM table WHERE signup_email_address='$email'");   
  if(mysql_num_rows($existingSignup) < 1){

   date_default_timezone_set('Asia/Singapore');
   $date = date('Y-m-d');
   $time = date('H:i:s');
   
   $insertSignup = mysql_query("INSERT INTO table (signup_email_address, signup_date, signup_time) VALUES ('$email','$date','$time')");
   if($insertSignup){ //if insert is successful
    $status = "success";
    $message = "You have been signed up!"; 
   }
   else { //if insert fails
    $status = "error";
    $message = "Oops, Theres been a technical error!"; 
   }
  }
  else { //if already signed up
   $status = "error";
   $message = "This email address has already been registered!";
  }
 }
 
 //return json response
 $data = array(
  'status' => $status,
  'message' => $message
 );

 echo json_encode($data);
 exit;
}
?>

And here is my failed attempt to convert the above MySQL code to PDO

<?php
if(isset($_GET['action'])&& $_GET['action'] == 'signup'){

 try {
  $db = new PDO('mysql:host=hostname;dbname=dbname;charset=utf8','user','pwd');
 }
 catch(exception $e) {
  echo "Error, connection failure";
 }

 $email = $_POST['emailfield'];

 //validate email address - check if input was empty
 if(empty($email)){
  $status = "error";
  $message = "You did not enter an email address!";
 }
 else if(!filter_var($email, FILTER_VALIDATE_EMAIL)){ //validate email address - check if is a valid email address
   $status = "error";
   $message = "You have entered an invalid email address!";
 }
 else {

  $existingSignup = "SELECT COUNT(*) FROM table WHERE signup_email_address='$email'";
  if ($res->fetchColumn() < 1) {

   date_default_timezone_set('Asia/Singapore');
   $date = date('Y-m-d');
   $time = date('H:i:s');
   
   $insertSignup = "INSERT INTO table (signup_email_address, signup_date, signup_time) VALUES (:signupemail, :signupdate, :signuptime)";

      $query = $db->prepare($insertSignup);
      $query->bindParam(':signupemail', $email);
      $query->bindParam(':signupdate', $date);
      $query->bindParam(':signuptime', $time);
      $results = $query->execute();

   if($insertSignup){ //if insert is successful
    $status = "success";
    $message = "You have been signed up!"; 
   }
   else { //if insert fails
    $status = "error";
    $message = "Oops, Theres been a technical error!"; 
   }
  }
  else { //if already signed up
   $status = "error";
   $message = "This email address has already been registered!";
  }
 }
 
 //return json response
 $data = array(
  'status' => $status,
  'message' => $message
 );

 echo json_encode($data);
 exit;
}
?>

Would you give me advice in correcting my PDO code? Thanks!

UPDATE:

Here's my updated PDO code:

<?php
if(isset($_GET['action'])&& $_GET['action'] == 'signup'){

 try {
  $db = new PDO('mysql:host=hostname;dbname=dbname;charset=utf8','user','pwd');
  $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  error_reporting(-1);
 }
 catch(PDOexception $e) {
  echo "Error, connection failure";
 }

 $email = $_POST['emailfield'];

 //validate email address - check if input was empty
 if(empty($email)){
  $status = "error";
  $message = "You did not enter an email address!";
 }
 else if(!filter_var($email, FILTER_VALIDATE_EMAIL)){ //validate email address - check if is a valid email address
   $status = "error";
   $message = "You have entered an invalid email address!";
 }
 else {
   $existingSignup = "SELECT * FROM table WHERE signup_email_address='$email'";
   $stmt = $db->query($existingSignup);
   $row_count = $stmt->rowCount();
   if ($row_count < 1) {

   date_default_timezone_set('Asia/Singapore');
   $date = date('Y-m-d');
   $time = date('H:i:s');
   
   $insertSignup = "INSERT INTO table (signup_email_address, signup_date, signup_time) VALUES (:signupemail, :signupdate, :signuptime)";

      $query = $db->prepare($insertSignup);
      $query->bindValue(':signupemail', $email);
      $query->bindValue(':signupdate', $date);
      $query->bindValue(':signuptime', $time);
      $results = $query->execute();

   if($insertSignup){ //if insert is successful
    $status = "success";
    $message = "You have been signed up!"; 
   }
   else { //if insert fails
    $status = "error";
    $message = "Oops, Theres been a technical error!"; 
   }
  }
  else { //if already signed up
   $status = "error";
   $message = "This email address has already been registered!";
  }
 }
 
 //return json response
 $data = array(
  'status' => $status,
  'message' => $message
 );

 echo json_encode($data);
 exit;
}
?>

But this is the error I get: Fatal error: Call to a member function query() on a non-object in index.php on line 27.

Where Line 27:

$stmt = $db->query($existingSignup);

How do I solve this?

clodal
  • 1,615
  • 20
  • 33
  • I'm not sure, but the non-object error maybe is throw because your DVD connection is bad. So I suggest to verify your parameters for the PDO class (i.e. dbname, username, password, and hostname) – slackmart Nov 25 '14 at 02:55
  • Take a look at this question: http://stackoverflow.com/questions/15297339/non-object-errors-using-php-pdo-with-mysql. I've a typo in previous comment I reffer to the variable `$db` instead DVD.. haha – slackmart Nov 25 '14 at 03:00

1 Answers1

0

Your $existingSignup query isn't executed. You must use something like:

$stmt = $db->query($existingSignup);
$row_count = $stmt->rowCount();
echo $row_count.' rows selected

And then continue with: if ($row_count < 1) {...}

For more info. consult this link: http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers

slackmart
  • 4,754
  • 3
  • 25
  • 39