update.php 932 B

12345678910111213141516171819202122232425262728293031323334
  1. <?php
  2. include '../connection.php';
  3. try {
  4. $key = $_POST['key'];
  5. $value = $_POST['value'];
  6. // first read the config table to see if a config already exists for this key
  7. $statement = $db->prepare("select value from config where key = '$key'");
  8. if(!$statement->execute()) {
  9. throw new Exception(implode(', ', $statement->errorInfo()));
  10. }
  11. if(!$statement->fetch(PDO::FETCH_COLUMN)) {
  12. $sql = "insert into config (key, value) values('$key', '$value')";
  13. } else {
  14. $sql = "update config set value = '$value' where key = '$key'";
  15. }
  16. $statement = $db->prepare($sql);
  17. if(!$statement->execute()) {
  18. throw new Exception(implode(', ', $statement->errorInfo()));
  19. }
  20. $jsonResult = array('success' => true);
  21. } catch(Exception $e) {
  22. $jsonResult = array(
  23. 'success' => false,
  24. 'message' => $e->getMessage()
  25. );
  26. }
  27. echo json_encode($jsonResult);
  28. ?>