Kaynağa Gözat

Improved update by uuid

Igor 3 yıl önce
ebeveyn
işleme
78cf7aa7f8
3 değiştirilmiş dosya ile 40 ekleme ve 8 silme
  1. 5 1
      app/post.php
  2. 35 3
      controller/forms.php
  3. 0 4
      explorer/destroy.php

+ 5 - 1
app/post.php

@@ -43,7 +43,11 @@
 
         require_once('../controller/forms.php');
         try {
-            store(json_encode($data, JSON_UNESCAPED_UNICODE));
+            if (isset($data['uuid'])) {
+                store(json_encode($data, JSON_UNESCAPED_UNICODE), $data['uuid']);
+            } else {
+                store(json_encode($data, JSON_UNESCAPED_UNICODE));
+            }
         } catch (Exception $e) {
             echo print_error($e->getMessage());
             exit;

+ 35 - 3
controller/forms.php

@@ -25,12 +25,44 @@
     $DB->set_charset("utf8");
  }
 
- function store ($data) {
+ function get_id_by_uuid ($uuid) {
     global $DB; connect();
+
+    $sql = "SELECT * FROM records";
+
+    if (!($stmt = $DB->prepare($sql))) {
+        echo "Prepare failed: (" . $DB->errno . ") " . $DB->error;
+    }
+    
+    $stmt->execute();
+    
+    $res_data = mysqli_stmt_get_result($stmt);
+    
+    $all_data = array();
     
-    $sql = "INSERT INTO records (id, form)
-            VALUES (null, ?)";
+    while($row = mysqli_fetch_array($res_data)){
+        $data = json_decode($row['form']);
+        if (isset($data->uuid) && ($data->uuid == $uuid)) {
+            return $row['id'];
+        }
+    }
+    return -1;
+ }
 
+ function store ($data, $uuid = null) {
+    global $DB; connect();
+
+    $sql = "";
+
+    $id_p = get_id_by_uuid($uuid);
+    if ($id_p >= 0) {
+        $sql =  "UPDATE records SET form = ?
+                WHERE id = " . $id_p;
+    } else {
+        $sql =  "INSERT INTO records (id, form)
+                VALUES (null, ?)";
+    }
+    
     if (!($stmt = $DB->prepare($sql))) {
         echo "Prepare failed: (" . $DB->errno . ") " . $DB->error;
     }

+ 0 - 4
explorer/destroy.php

@@ -1,4 +0,0 @@
-<?php 
-session_start();
-session_destroy();
-?>