Work on insert

This commit is contained in:
sctn4elk 2024-05-03 12:41:39 -05:00
parent 564ed4f50c
commit 15d72de736
3 changed files with 41 additions and 36 deletions

View File

@ -117,11 +117,18 @@ class CustomerController extends BaseController{
return; return;
} }
// reads the raw POST data and returns it as a string. // reads the raw POST data and returns it as a string.
$jsonPayload = (array) json_decode(file_get_contents('php://input'), TRUE); $jsonPayload = file_get_contents('php://input');
if (! $this->validatePerson($jsonPayload)) { $input = json_decode($jsonPayload, TRUE);
return $this->unprocessableEntityResponse(); //if (! $this->validatePerson($input)) {
} //return $this->unprocessableEntityResponse();
$response = $this->customerModel->insertCustomer($jsonPayload); //}
return var_dump($input);
//remove customer_id field so it doesn't break
unset($input['customer_id']);
return var_dump($input);
//$modinput = json_encode($input);
//$newinput = json_decode($modinput, TRUE);
$response = $this->customerModel->insertCustomer($input);
return $response; return $response;
} }
@ -187,7 +194,7 @@ class CustomerController extends BaseController{
private function validatePerson($input) private function validatePerson($input)
{ {
$validtion = false; $validtion = false;
if($input['customer_name_first'] != null){ if($input['customer_name_first'] != null){
$validtion = true; $validtion = true;
if($input['customer_name_last'] == null) { if($input['customer_name_last'] == null) {
$validtion = false; $validtion = false;

View File

@ -87,17 +87,35 @@ class CustomerModel extends Database {
} }
public function insertCustomer($jsonPayLoad) public function insertCustomer($jsonPayLoad)
{ {
$rowCount = $this->processStatement("CALL insert_new_customer_proc", $jsonPayLoad); //return var_dump($jsonPayLoad);
$result = (object) ['rowCount'=>$rowCount]; $keys = array_keys($jsonPayLoad);
return $result; $n = count($keys);
$query .= "CALL insert_new_customer_proc ('" . $jsonPayLoad[$keys[0]] . "', ";
for($i = 1; $i < $n-1; $i++) {
$query .= "'" . $jsonPayLoad[$keys[$i]] . "', ";
}
$query .= $jsonPayLoad[$keys[$i]] . ")";
return var_dump($query);
$rowCount = $this->processStatement($query);
return $rowCount;
} }
public function updateCustomer($jsonPayLoad) public function updateCustomer($jsonPayLoad)
{ {
$rowCount = $this->processStatement("CALL update_existing_customer_proc", $jsonPayLoad); $keys = array_keys($jsonPayLoad);
$result = (object) ['rowCount'=>$rowCount]; $n = count($keys);
return $result;
$query .= "CALL update_existing_customer_proc (" . $jsonPayLoad[$keys[0]] . ", ";
for($i = 1; $i < $n-1; $i++) {
$query .= "'" . $jsonPayLoad[$keys[$i]] . "', ";
}
$query .= $jsonPayLoad[$keys[$i]] . ")";
$rowCount = $this->processStatement($query);
//$result = (object) ['rowCount'=>$rowCount];
return $rowCount;
} }
public function deleteCustomer() public function deleteCustomer()

View File

@ -56,18 +56,9 @@ class Database {
} }
} }
public function processStatement($query = "", $params = []) public function processStatement($query = "")
{ {
try { try {
$keys = array_keys($params);
$n = count($params);
$query .= " (" . $params[$keys[0]] . ", ";
for($i = 1; $i < $n-1; $i++) {
$query .= "'" . $params[$keys[$i]] . "', ";
}
$query .= $params[$keys[$i]] . ")";
//Prepare the statement //Prepare the statement
$stmt = $this->connection->stmt_init(); $stmt = $this->connection->stmt_init();
$stmt = $this->connection->prepare($query); $stmt = $this->connection->prepare($query);
@ -80,6 +71,7 @@ class Database {
throw New Exception("Unable to execute the statement."); throw New Exception("Unable to execute the statement.");
} }
//$rowCount = $this->executeStatement($stmt);
$rowCount = $this->connection->affected_rows; $rowCount = $this->connection->affected_rows;
if($rowCount < 1) if($rowCount < 1)
{ {
@ -97,9 +89,7 @@ class Database {
private function executeStatement(&$stmt) private function executeStatement(&$stmt)
{ {
try { try {
return var_dump($stmt);
$result = $stmt->execute(); $result = $stmt->execute();
if($result === false) { if($result === false) {
throw New Exception("Unable to execute the statement."); throw New Exception("Unable to execute the statement.");
} }
@ -110,14 +100,4 @@ class Database {
throw New Exception( $e->getMessage() ); throw New Exception( $e->getMessage() );
} }
} }
private function initStatement()
{
$statement = mysqli_stmt_init($this->connection);
return $statement;
}
private function prepareStatement($statement, $querystring)
{
}
} }