Fix insert on for child data

Add a few try catch,
Add ; to the end of a few SQL queries
This commit is contained in:
FusionPBX
2022-09-30 12:46:44 -06:00
committed by GitHub
parent e06a7d4fef
commit aeec91fae8
+25 -17
View File
@@ -24,7 +24,6 @@
Mark J Crane <markjcrane@fusionpbx.com> Mark J Crane <markjcrane@fusionpbx.com>
Luis Daniel Lucio Quiroz <dlucio@okay.com.mx> Luis Daniel Lucio Quiroz <dlucio@okay.com.mx>
*/ */
include "root.php";
//define the database class //define the database class
if (!class_exists('database')) { if (!class_exists('database')) {
@@ -2068,7 +2067,7 @@ include "root.php";
//determine action update or delete and get the original data //determine action update or delete and get the original data
if ($parent_key_exists) { if ($parent_key_exists) {
$sql = "SELECT ".implode(", ", $parent_field_names)." FROM ".$table_name." "; $sql = "SELECT ".implode(", ", $parent_field_names)." FROM ".$table_name." ";
$sql .= "WHERE ".$parent_key_name." = '".$parent_key_value."' "; $sql .= "WHERE ".$parent_key_name." = '".$parent_key_value."'; ";
$prep_statement = $this->db->prepare($sql); $prep_statement = $this->db->prepare($sql);
if ($prep_statement) { if ($prep_statement) {
//get the data //get the data
@@ -2077,6 +2076,7 @@ include "root.php";
$result = $prep_statement->fetchAll(PDO::FETCH_ASSOC); $result = $prep_statement->fetchAll(PDO::FETCH_ASSOC);
} }
catch(PDOException $e) { catch(PDOException $e) {
echo $sql."<br />\n";
echo 'Caught exception: '. $e->getMessage()."<br /><br />\n"; echo 'Caught exception: '. $e->getMessage()."<br /><br />\n";
echo $sql. "<br /><br />\n"; echo $sql. "<br /><br />\n";
exit; exit;
@@ -2091,8 +2091,7 @@ include "root.php";
$action = "add"; $action = "add";
} }
} }
unset($prep_statement); unset($prep_statement, $result);
unset($result);
} }
else { else {
$action = "add"; $action = "add";
@@ -2132,7 +2131,6 @@ include "root.php";
} }
if (is_array($array)) { if (is_array($array)) {
foreach ($array as $array_key => $array_value) { foreach ($array as $array_key => $array_value) {
if (!is_array($array_value)) { if (!is_array($array_value)) {
if ($array_key != 'insert_user' && if ($array_key != 'insert_user' &&
$array_key != 'insert_date' && $array_key != 'insert_date' &&
@@ -2160,7 +2158,6 @@ include "root.php";
} }
} }
} }
$sql .= "now(), "; $sql .= "now(), ";
$sql .= ":insert_user "; $sql .= ":insert_user ";
$sql .= ");"; $sql .= ");";
@@ -2269,7 +2266,7 @@ include "root.php";
$params['update_user'] = $_SESSION['user_uuid']; $params['update_user'] = $_SESSION['user_uuid'];
//add the where with the parent name and value //add the where with the parent name and value
$sql .= "WHERE ".$parent_key_name." = '".$parent_key_value."' "; $sql .= "WHERE ".$parent_key_name." = '".$parent_key_value."'; ";
$sql = str_replace(", WHERE", " WHERE", $sql); $sql = str_replace(", WHERE", " WHERE", $sql);
//add update user parameter //add update user parameter
@@ -2388,7 +2385,8 @@ include "root.php";
//determine sql update or delete and get the original data //determine sql update or delete and get the original data
if ($uuid_exists) { if ($uuid_exists) {
$sql = "SELECT ". implode(", ", $child_field_names)." FROM ".$child_table_name." "; $sql = "SELECT ". implode(", ", $child_field_names)." FROM ".$child_table_name." ";
$sql .= "WHERE ".$child_key_name." = '".$child_key_value."' "; $sql .= "WHERE ".$child_key_name." = '".$child_key_value."'; ";
try {
$prep_statement = $this->db->prepare($sql); $prep_statement = $this->db->prepare($sql);
if ($prep_statement) { if ($prep_statement) {
//get the data //get the data
@@ -2410,6 +2408,14 @@ include "root.php";
} }
unset($prep_statement); unset($prep_statement);
} }
catch(PDOException $e) {
echo $sql."<br />\n";
echo 'Caught exception: '. $e->getMessage()."<br /><br />\n";
echo $sql. "<br /><br />\n";
exit;
}
}
else { else {
$action = "add"; $action = "add";
} }
@@ -2451,7 +2457,7 @@ include "root.php";
//add the where with the parent name and value //add the where with the parent name and value
$sql .= "WHERE ".$parent_key_name." = '".$parent_key_value."' "; $sql .= "WHERE ".$parent_key_name." = '".$parent_key_value."' ";
$sql .= "AND ".$child_key_name." = '".$child_key_value."' "; $sql .= "AND ".$child_key_name." = '".$child_key_value."'; ";
$sql = str_replace(", WHERE", " WHERE", $sql); $sql = str_replace(", WHERE", " WHERE", $sql);
//set the error mode //set the error mode
@@ -2547,16 +2553,17 @@ include "root.php";
foreach ($row as $k => $v) { foreach ($row as $k => $v) {
if (!is_array($v)) { if (!is_array($v)) {
$k = self::sanitize($k); $k = self::sanitize($k);
if ($k != 'insert_user' &&
$k != 'insert_date' &&
$k != 'update_user' &&
$k != 'update_date') {
$sql .= $k.", "; $sql .= $k.", ";
} }
} }
} }
if (!isset($row['insert_date'])) { }
$sql .= "insert_date, "; $sql .= "insert_date, ";
}
if (!isset($row['insert_user'])) {
$sql .= "insert_user "; $sql .= "insert_user ";
}
$sql .= ") "; $sql .= ") ";
$sql .= "VALUES "; $sql .= "VALUES ";
$sql .= "("; $sql .= "(";
@@ -2585,18 +2592,19 @@ include "root.php";
} }
else { else {
$k = self::sanitize($k); $k = self::sanitize($k);
if ($k != 'insert_user' &&
$k != 'insert_date' &&
$k != 'update_user' &&
$k != 'update_date') {
$sql .= ':'.$k.", "; $sql .= ':'.$k.", ";
$params[$k] = trim($v); $params[$k] = trim($v);
} }
} }
} }
} }
if (!isset($row['insert_date'])) { }
$sql .= "now(), "; $sql .= "now(), ";
}
if (!isset($row['insert_user'])) {
$sql .= ":insert_user "; $sql .= ":insert_user ";
}
$sql .= ");"; $sql .= ");";
//add insert user parameter //add insert user parameter