Hi pbismad, Thank you for responding to my post. Below are my responses to your questions:
The database connection code is just for this query. I opened up the same connection for the first query but closed it when I was done with the first query. I've also tried leaving it open. In neither case did the row get inserted.
These are the errors that occur for the working query at the top of the code:
2017-12-02 19:31:02 700000934000 InnoDB: Error: Table "mysql"."innodb_table_stats" not found.
2017-12-02 19:31:02 700000934000 InnoDB: Recalculation of persistent statistics requested for table "ginkgo"."fintrans" but the required persistent statistics storage is not present or is corrupted. Using transient stats instead.
2017-12-02 19:31:12 700000934000 InnoDB: Error: Table "mysql"."innodb_table_stats" not found.
The HTML source shows the following:
<html><head></head><body>INSERT INTO `FinTrans` (`Category`,`Location`,`FAmt`,`FDT`) values ('Tenant','514 Creekview',3200,'2000-01-01');success</body></html>
$gross_rent[0] comes from PHP calculations but for testing purposes, I deleted all of that and just created an array with one value at $gross_rent[0]. A vardump shows the value that I manually set it to.
There are no triggers set up in the database. I am unsure what you mean by transactions in my code. As of now, there is nothing after the second query.
I'm not actually sure how to set a secondary table to update automatically if I change the primary table. FinTrans (Final transaction table) is a select from the ExpTrans table. I haven't deleted any rows I don't think.
I've attached my full code including the working portion below:
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
$mysqli = new mysqli('localhost:8889','root','root','Ginkgo');
if($mysqli->connect_errno){
echo ('Error connecting to MySQL server. '.$mysqli->connect_errno .' '.$mysqli->connect_error);
}
//drop exptrans
$sql = "TRUNCATE ExpTrans; ";
//expand start and end dates
$sql .= "INSERT INTO ExpTrans (`Date`,`Acct`,`Clients`,`Category`,`Counterparty`,`RentDate`,`Description`,`Location`,`Amount`,`ST`,`EN`,`Date Spent by Employee`,`Date Reimbursed by Client`,`ID`,`caldate`) (SELECT `Date`,`Acct`,`Clients`,`Category`,`Counterparty`,`RentDate`,`Description`,`Location`,`Amount`,`ST`,`EN`,`Date Spent by Employee`,`Date Reimbursed by Client`,`ID`,`caldate` FROM trans t1 left JOIN cal On cal.caldate >= date_add(t1.ST, interval -1 MONTH) and cal.caldate <= t1.EN); ";
//add final date and if caldate is empty then use original date, otherwise, use caldate
//$sql .= "ALTER TABLE ExpTrans ADD FDT date; ";
$sql .= "UPDATE ExpTrans SET FDT = case when caldate is NULL then Date when caldate <= ST then ST else caldate end; ";
//add final amount and calculate out the monthly amount
//$sql .= "ALTER TABLE ExpTrans ADD FAmt decimal (18,2); ";
$sql .= "UPDATE ExpTrans set FAmt = case when ST is NULL then Amount else (Amount/(datediff(EN,ST)+1))*(day(last_day(FDT))-day(FDT)+1) end; ";
//drop fintrans
$sql .= "TRUNCATE FinTrans; ";
//only copy over everything that's not a transfer
$sql .= "insert into FinTrans (`Acct`,`Clients`,`Category`,`Counterparty`,`RentDate`,`Description`,`Location`,`Date Spent by Employee`,`Date Reimbursed by Client`,`ID`,`FDT`,`FAmt`) SELECT `Acct`,`Clients`,`Category`,`Counterparty`,`RentDate`,`Description`,`Location`,`Date Spent by Employee`,`Date Reimbursed by Client`,`ID`,`FDT`,`FAmt` FROM ExpTrans where `Category` <> 'Transfer'; ";
//null out anything that's blank
$sql .= "UPDATE FinTrans SET `Acct` = NULLIF(`Acct`, ''),`Clients` = NULLIF(`Clients`, ''),`Category` = NULLIF(`Category`, ''),`Description` = NULLIF(`Description`, ''),`Location` = NULLIF(`Location`, ''),`ID` = NULLIF(`ID`, ''); ";
//add a primary key
//$sql .= "ALTER TABLE FinTrans add column `idkey` int not null auto_increment first, add primary key (`idkey`); ";
//insert market value from propvalue
$sql .= "insert into FinTrans (`Category`,`FDT`,`FAmt`,`Location`) select 'Value',caldate,Price,'514 Creekview' FROM (SELECT * FROM propvalue t1 CROSS JOIN (SELECT caldate FROM cal) t2 WHERE t2.caldate >= t1.ST and t2.caldate <= t1.EN) t3; ";
//insert market rent from propvalue
$sql .= "insert into FinTrans (`Category`,`FDT`,`FAmt`,`Location`) select 'MktRent',caldate,Rent,'514 Creekview' FROM (SELECT * FROM propvalue t1 CROSS JOIN (SELECT caldate FROM cal) t2 WHERE t2.caldate >= t1.ST and t2.caldate <= t1.EN) t3; ";
//insert rent roll
$sql .= "insert into FinTrans (`Category`,`Counterparty`,`FDT`,`FAmt`,`Location`) select 'Tenant',Tenant,caldate,Rent,'514 Creekview' FROM (SELECT * FROM rentroll t1 CROSS JOIN (SELECT caldate FROM cal) t2 WHERE t2.caldate >= t1.startdate and t2.caldate <= t1.enddate) t3; ";
if (!$mysqli->multi_query($sql)) {
echo "Multi query failed: (" . $mysqli->errno . ") " . $mysqli->error;
}
mysqli_close($mysqli);
//this section is the one that's not working, where the row is not inserting into the FinTrans table.
$mysqli = new mysqli('localhost:8889','root','root','Ginkgo');
if($mysqli->connect_errno){
echo ('Error connecting to MySQL server. '.$mysqli->connect_errno .' '.$mysqli->connect_error);
}
//gross rent sum
$gross_rent = array();
$gross_rent[0] = 3200;
$sql = "INSERT INTO `FinTrans` (`Category`,`Location`,`FAmt`,`FDT`) values ('Tenant','514 Creekview',".$gross_rent[0].",'2000-01-01');";
echo $sql;
if( mysqli_query($mysqli,"INSERT INTO `FinTrans` (`Category`,`Description`,`Location`) values ('Tenant3','test','514 Creekview')"))
{
echo 'success';
}else{
echo 'failed';
}
var_dump($gross_rent[0]);
$mysqli->close();
?>