-1

I have this code, the purpose of which is to insert values into two tables (propects, emailsent) in mysql. I have followed the answer in Insert into 2 tables with PDO MySQL. However, I have a hard time saving the respective values in the second table. Saving to the first table (prospects) seems to work. I've search this site for answers but to no avail.

I am using PDO.

So my problem is two-fold:

  1. Is [A] (call to insert in tables) valid code?
  2. How can I make [B] valid as to save the respective values to its respective keys/column in the 2nd table (emailsent)?

if(isset($_POST['senderEmail'])) 
{
      try
      {
    ...

[A]      $q= "INSERT INTO 'prospects'('senderName', 'senderEmail', 'offer', 'dateTimeSent') 
        VALUES (:senderName, :senderEmail, :offer, :dateTimeSent);    

        INSERT INTO 'emailsent'('call_timeSched', 'call_time', 'contactTelSched', 'contactTel', 'fileAttach', 'emailTextbox') 
            VALUES (:call_timeSched, :call_time, :contactTelSched, :contactTel, :fileAttach, :emailTextbox)"; // inserting into EMAILSENT table 

        $query    = $dbh  ->prepare($q);

[B]        $results  = $query->execute(array(
            ":senderName"=>$senderName,
            ":senderEmail"=>$senderEmail,
            ":offer"=>$offer,
            ":dateTimeSent"=>$dateTimeSent,
            ":call_timeSched"=>$call_timeSched,
            ":call_time"=>$call_time,
            ":contactTelSched"=>$contactTelSched,
            ":contactTel"=>$contactTel,
            ":fileAttach"=>$fileAttach,
            ":emailTextbox"=>$emailTextbox,
        ));

      }
        catch (PDOException $e)
      {
        $error = 'Error adding elements to database: ' . $e->getMessage();
        include 'error.html.php';
        exit();
      }

      exit();
    }
Community
  • 1
  • 1
Synod
  • 29
  • 10

1 Answers1

0

FOr me I prefer to use this ...

    "INSERT INTO emailsent VALUES (:call_timeSched, :call_time, :contactTelSched, :contactTel, :fileAttach, :emailTextbox)"; // inserting into EMAILSENT table 

Note U must put all database fields in values.

Marc
  • 139
  • 1
  • 7
  • I haven't tested it yet but if the form user left a field blank, will this code work? Is it then a good practice to use defaults in all fields? Also, will it affect [B]? – Synod Dec 13 '13 at 06:29
  • this help to minimize the process in sql query. Its Ok to use this as long u memorize ur fields. – Marc Dec 13 '13 at 06:39