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;
}
// reads the raw POST data and returns it as a string.
$jsonPayload = (array) json_decode(file_get_contents('php://input'), TRUE);
if (! $this->validatePerson($jsonPayload)) {
return $this->unprocessableEntityResponse();
}
$response = $this->customerModel->insertCustomer($jsonPayload);
$jsonPayload = file_get_contents('php://input');
$input = json_decode($jsonPayload, TRUE);
//if (! $this->validatePerson($input)) {
//return $this->unprocessableEntityResponse();
//}
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;
}
@ -187,7 +194,7 @@ class CustomerController extends BaseController{
private function validatePerson($input)
{
$validtion = false;
if($input['customer_name_first'] != null){
if($input['customer_name_first'] != null){
$validtion = true;
if($input['customer_name_last'] == null) {
$validtion = false;

View File

@ -88,16 +88,34 @@ class CustomerModel extends Database {
public function insertCustomer($jsonPayLoad)
{
$rowCount = $this->processStatement("CALL insert_new_customer_proc", $jsonPayLoad);
$result = (object) ['rowCount'=>$rowCount];
return $result;
//return var_dump($jsonPayLoad);
$keys = array_keys($jsonPayLoad);
$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)
{
$rowCount = $this->processStatement("CALL update_existing_customer_proc", $jsonPayLoad);
$result = (object) ['rowCount'=>$rowCount];
return $result;
$keys = array_keys($jsonPayLoad);
$n = count($keys);
$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()

View File

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