troubleshooting Question

PDO 2 table insert statement

Avatar of doctorbill
doctorbillFlag for United Kingdom of Great Britain and Northern Ireland asked on
PHPMySQL Server* PDO
16 Comments1 Solution92 ViewsLast Modified:
<?php
if(isset($_POST["submit"])){
$hostname='localhost';
$username='root';
$password='xxxxxxx';

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>";
} 
else{
     echo "<script type= 'text/javascript'>alert('Data not successfully Inserted.');</script>";
}

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

}
?>

I am trying to insert data in to 2 tables at once but the second insertion does not work
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 16 Comments.
Join the Community
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