<?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();
}
}
?>
<?php
if(isset($_POST["submit"])){
$hostname='localhost';
$username='root';
$password='xxxxxx';
try {
$dbh = new PDO("mysql:host=$hostname;dbname=inventas",$username,$password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // <== add this line
$dbh->beginTransaction();
$sitesSql = "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 (:name, :email, :company, :rep, :comments, :todo, :visit, :from, :to, :return, :updated)";
$historySql = "INSERT INTO invhistory (histrepname, histwork, histdate, histupdated) VALUES (:siteRep, :work, :date, :updated)";
$sites = $dbh->prepare($sitesSql);
$history = $dbh->prepare($historySql);
$sites->execute([
'name' => $_POST["site_name"],
'email' => $_POST["site_email"],
'company' => $_POST["site_company"],
'rep' => $_POST["site_rep"],
'comments' => $_POST["site_comments"],
'todo' => $_POST["site_todo"],
'visit' => $_POST["site_visitdate"],
'from' => $_POST["time_from"],
'to' => $_POST["time_to"],
'return' => $_POST["site_return"],
'updated' => $_POST["radvisit"],
]);
$history->execute([
'siteRep' => $_POST["site_rep"],
'work' => $_POST["site_comments"],
'date' => $_POST["sites_visitdate"],
'updated' => $_POST["radvisit"],
]);
$dbh->commit();
echo "<script type= 'text/javascript'>alert('New Record Inserted Successfully');</script>";
} catch (Exception $e) {
$dbh->rollBack();
echo "<script type= 'text/javascript'>alert('Data not successfully Inserted.');</script>";
}
}
?>
} catch (Exception $e) {
$dbh->rollBack();
printf( "<script type= 'text/javascript'>alert('%s');</script>", $e->getMessage() );
}
You should now see the error message the the exception contains. Should help you identify what the problem is, but if not, post the error message here and we can take a look.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
if( isset($_POST["submit"]) ){
$hostname='localhost';
$username='root';
$password='xxxxxx';
try {
$dbh = new PDO("mysql:host=$hostname;dbname=inventas",$username,$password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // <== add this line
$dbh->beginTransaction();
$sitesSql = "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 (:name, :email, :company, :rep, :comments, :todo, :visit, :from, :to, :return, :updated)";
$historySql = "INSERT INTO invhistory (histrepname, histwork, histdate, histupdated) VALUES (:siteRep, :work, :date, :updated)";
$sites = $dbh->prepare($sitesSql);
$history = $dbh->prepare($historySql);
$sites->execute([
'name' => $_POST["site_name"],
'email' => $_POST["site_email"],
'company' => $_POST["site_company"],
'rep' => $_POST["site_rep"],
'comments' => $_POST["site_comments"],
'todo' => $_POST["site_todo"],
'visit' => $_POST["site_visitdate"],
'from' => $_POST["time_from"],
'to' => $_POST["time_to"],
'return' => $_POST["site_return"],
'updated' => $_POST["radvisit"],
]);
$history->execute([
'siteRep' => $_POST["site_rep"],
'work' => $_POST["site_comments"],
'date' => $_POST["sites_visitdate"],
'updated' => $_POST["radvisit"],
]);
$dbh->commit();
echo "Data Inserted Successfully";
} catch (Exception $e) {
$dbh->rollBack();
echo $e->getMessage();
}
}
?>
Run the script again and see what you get. If you're running this code before outputting some html, you may need to view the source to see the error message.
As for your code, the security is paramount when working with user inputs. PHP handles this using data filtering and PDO provides prepare statement to avoid security pitfalls. Read more before going in production with the above code.