troubleshooting Question

PDO 2 table insert statement

Avatar of doctorbill
doctorbillFlag for United Kingdom of Great Britain and Northern Ireland asked on
16 Comments1 Solution92 ViewsLast Modified:

try {
    $dbh = new PDO("mysql:host=$hostname;dbname=inventas",$username,$password);

    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // <== add this line

$sql = "INSERT INTO inventassites (sites_name, sites_email, sites_company, sites_representative, sites_comments, sites_todo, sites_datevisited, sites_timefrom, sites_timeto, sites_return, sites_updated)
VALUES ('".$_POST["site_name"]."','".$_POST["site_email"]."','".$_POST["site_company"]."','".$_POST["site_rep"]."','".$_POST["site_comments"]."','".$_POST["site_todo"]."','".$_POST["site_visitdate"]."','".$_POST["time_from"]."','".$_POST["time_to"]."','".$_POST["site_return"]."','".$_POST["radvisit"]."')";

$sql2 = "INSERT INTO invhistory (histrepname)
VALUES ('".$_POST["site_rep"]."')";
if ($dbh->query($sql)) {
     echo "<script type= 'text/javascript'>alert('New Record Inserted Successfully');</script>";
     echo "<script type= 'text/javascript'>alert('Data not successfully Inserted.');</script>";

    $dbh = null;
catch(PDOException $e)
    echo $e->getMessage();


Open in new window

I am trying to insert data in to 2 tables at once but the second insertion does not work
Join our community to see this answer!
Unlock 1 Answer and 16 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 16 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros