Skip to content

Commit dc7ebba

Browse files
authored
Merge pull request #302 from AMTuttle02/test
Test
2 parents 5e32a5a + d9d89d0 commit dc7ebba

File tree

11 files changed

+1777
-538
lines changed

11 files changed

+1777
-538
lines changed

API/src/admin/updateStaticText.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
header('Access-Control-Allow-Origin: *');
3+
header('Access-Control-Allow-Methods: GET, POST');
4+
header("Access-Control-Allow-Headers: X-Requested-With");
5+
header('Access-Control-Allow-Headers: Origin, Content-Type');
6+
header('Content-Type: application/json');
7+
8+
include '../admin/conn.php';
9+
10+
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
11+
try {
12+
$page = $_POST["page"];
13+
$location = $_POST["location"];
14+
$text = $_POST["text"];
15+
// Update the text for the given page and location
16+
$update = $conn->prepare("UPDATE staticText SET text = ? WHERE page = ? AND location = ?;");
17+
if ($update === false) {
18+
throw(new Exception("Prepare failed: " . $conn->error));
19+
}
20+
$update->bind_param("sss", $text, $page, $location);
21+
$ok = $update->execute();
22+
if ($ok === false) {
23+
throw(new Exception("Execute failed: " . $update->error));
24+
}
25+
26+
// If no rows were updated, optionally insert a new row
27+
if ($update->affected_rows === 0) {
28+
// check if a row exists; if not, insert
29+
$check = $conn->prepare("SELECT id FROM staticText WHERE page = ? AND location = ?;");
30+
$check->bind_param("ss", $page, $location);
31+
$check->execute();
32+
$res = $check->get_result();
33+
if ($res->num_rows === 0) {
34+
$insert = $conn->prepare("INSERT INTO staticText (page, location, text) VALUES (?, ?, ?);");
35+
if ($insert === false) {
36+
throw(new Exception("Prepare insert failed: " . $conn->error));
37+
}
38+
$insert->bind_param("sss", $page, $location, $text);
39+
$insOk = $insert->execute();
40+
if ($insOk === false) {
41+
throw(new Exception("Insert failed: " . $insert->error));
42+
}
43+
echo json_encode(1);
44+
exit;
45+
}
46+
}
47+
48+
echo json_encode(1);
49+
} catch (Exception $e) {
50+
http_response_code(500);
51+
echo json_encode("ERR: Update static text failed with page " . $page
52+
. ", location: " . $location
53+
. ". " . $e->getMessage()
54+
);
55+
}
56+
}
57+
58+
?>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
header('Access-Control-Allow-Origin: *');
3+
header('Access-Control-Allow-Methods: GET, POST');
4+
header("Access-Control-Allow-Headers: X-Requested-With");
5+
6+
include '../admin/conn.php';
7+
8+
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
9+
$input = json_decode(file_get_contents('php://input'), true);
10+
if (!$input || !is_array($input)) {
11+
echo json_encode("Error - invalid input");
12+
exit;
13+
}
14+
15+
// set all featured = 0
16+
$reset = $conn->query("UPDATE products SET featured = 0");
17+
if ($reset === false) {
18+
echo json_encode("Error - cannot reset featured products");
19+
exit;
20+
}
21+
22+
// prepare update
23+
$stmt = $conn->prepare("UPDATE products SET featured = ? WHERE product_id = ?");
24+
if ($stmt === false) {
25+
echo json_encode("Error - cannot prepare update statement");
26+
exit;
27+
}
28+
29+
foreach ($input as $p) {
30+
if (isset($p['id']) && isset($p['position']) && $p['position'] >= 1) {
31+
$stmt->bind_param("ii", $p['position'], $p['id']);
32+
$stmt->execute();
33+
}
34+
}
35+
36+
echo json_encode(1);
37+
}
38+
39+
mysqli_close($conn);
40+
?>

0 commit comments

Comments
 (0)