-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsert_transaction.php
58 lines (48 loc) · 1.51 KB
/
insert_transaction.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php
// json response array
$response = array("error" => FALSE);
if(isset($_POST["prep_query"]) && isset($_POST["param_type"]) && isset($_POST["params"]))
{
require_once "DB_Functions.php";
$db = new DB_Functions();
$queries = explode(';', $_POST["prep_query"]);
$param_types = explode(';', $_POST["param_type"]);
$params = json_decode($_POST["params"]);
$start = 0;
$db->conn->begin_transaction();
$result = true;
$lastId;
foreach ($queries as $index => $query) {
$stmt = $db->conn->prepare($query);
$count = strlen($param_types[$index]);
$params_slice = array_slice($params, $start, $count);
$stmt->bind_param($param_types[$index], ...$params_slice);
if (!$stmt->execute())
{
$result = false;
break;
}
$lastId = $db->conn->insert_id;
$start += $count;
}
if($result)
{
$db->conn->commit();
$response["result"] = $result;
$response["id"] = $lastId;
}
else
{
$db->conn->rollback();
$response["error"] = true;
$response["error_msg"] = "Problema di inserimento";
}
$db->conn->close();
}
else
{
$response["error"] = true;
$response["error_msg"] = "I parametri richiesti sono obbligatori";
}
echo json_encode($response);
?>