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+ ?>
0 commit comments