term_post.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. /*
  3. 修改术语
  4. */
  5. require_once "../path.php";
  6. require_once "../public/_pdo.php";
  7. require_once '../public/function.php';
  8. require_once "../redis/function.php";
  9. $redis = redis_connect();
  10. #未登录不能修改
  11. if (isset($_COOKIE["userid"]) == false) {
  12. $respond['status'] = 1;
  13. $respond['message'] = "not yet log in";
  14. echo json_encode($respond, JSON_UNESCAPED_UNICODE);
  15. exit;
  16. }
  17. $respond = array("status" => 0, "message" => "");
  18. PDO_Connect("" . _FILE_DB_TERM_);
  19. if ($_POST["id"] != "") {
  20. #更新
  21. #先查询是否有权限
  22. $query = "SELECT id from term where guid= ? and owner = ? ";
  23. $stmt = $PDO->prepare($query);
  24. $stmt->execute(array($_POST["id"],$_COOKIE["userid"]));
  25. if ($stmt) {
  26. $Fetch = $stmt->fetch(PDO::FETCH_ASSOC);
  27. if(!$Fetch){
  28. $respond['status'] = 1;
  29. $respond['message'] = "no power";
  30. echo json_encode($respond, JSON_UNESCAPED_UNICODE);
  31. exit;
  32. }
  33. }
  34. $query = "UPDATE term SET meaning= ? ,other_meaning = ? , tag= ? ,channal = ? , language = ? , note = ? , receive_time= ?, modify_time= ? where guid= ? and owner = ? ";
  35. $stmt = @PDO_Execute($query,
  36. array($_POST["mean"],
  37. $_POST["mean2"],
  38. $_POST["tag"],
  39. $_POST["channal"],
  40. $_POST["language"],
  41. $_POST["note"],
  42. mTime(),
  43. mTime(),
  44. $_POST["id"],
  45. $_COOKIE["userid"],
  46. ));
  47. if (!$stmt || ($stmt && $stmt->errorCode() != 0)) {
  48. $error = PDO_ErrorInfo();
  49. $respond['status'] = 1;
  50. $respond['message'] = $error[2] . $query;
  51. } else {
  52. $respond['status'] = 0;
  53. $respond['message'] = $_POST["word"];
  54. $respond['data'] = ["guid"=>$_POST["id"],
  55. "word"=>$_POST["word"],
  56. "word_en"=>$_POST["word"],
  57. "meaning"=>$_POST["mean"],
  58. "other_meaning"=>$_POST["mean2"],
  59. "tag"=>$_POST["tag"],
  60. "channal"=>$_POST["channal"],
  61. "language"=>$_POST["language"],
  62. "note"=>$_POST["note"],
  63. "owner"=>$_COOKIE["userid"]
  64. ];
  65. }
  66. } else {
  67. #新建
  68. if(trim($_POST["word"])==""){
  69. $respond['status'] = 1;
  70. $respond['message'] = "pali word cannot be empty";
  71. echo json_encode($respond, JSON_UNESCAPED_UNICODE);
  72. exit;
  73. }
  74. if(trim($_POST["mean"])==""){
  75. $respond['status'] = 1;
  76. $respond['message'] = "meaning cannot be empty";
  77. echo json_encode($respond, JSON_UNESCAPED_UNICODE);
  78. exit;
  79. }
  80. if(trim($_POST["language"])==""){
  81. $respond['status'] = 1;
  82. $respond['message'] = "language cannot be empty";
  83. echo json_encode($respond, JSON_UNESCAPED_UNICODE);
  84. exit;
  85. }
  86. #先查询是否有重复数据
  87. if($_POST["channal"]==""){
  88. $query = "SELECT id from term where word= ? and language=? and tag=? and owner = ? ";
  89. $stmt = $PDO->prepare($query);
  90. $stmt->execute(array($_POST["word"],$_POST["language"],$_POST["tag"],$_COOKIE["userid"]));
  91. }else{
  92. $query = "SELECT id from term where word= ? and channal=? and tag=? and owner = ? ";
  93. $stmt = $PDO->prepare($query);
  94. $stmt->execute(array($_POST["word"],$_POST["channal"],$_POST["tag"],$_COOKIE["userid"]));
  95. }
  96. if ($stmt) {
  97. $Fetch = $stmt->fetch(PDO::FETCH_ASSOC);
  98. if($Fetch){
  99. $respond['status'] = 1;
  100. $respond['message'] = "已经有同样的记录";
  101. echo json_encode($respond, JSON_UNESCAPED_UNICODE);
  102. exit;
  103. }
  104. }
  105. $parm[] = UUID::v4();
  106. $parm[] = $_POST["word"];
  107. $parm[] = pali2english($_POST["word"]);
  108. $parm[] = $_POST["mean"];
  109. $parm[] = $_POST["mean2"];
  110. $parm[] = $_POST["tag"];
  111. $parm[] = $_POST["channal"];
  112. $parm[] = $_POST["language"];
  113. $parm[] = $_POST["note"];
  114. $parm[] = $_COOKIE["userid"];
  115. $parm[] = 0;
  116. $parm[] = mTime();
  117. $parm[] = mTime();
  118. $parm[] = mTime();
  119. $query = "INSERT INTO term (id, guid, word, word_en, meaning, other_meaning, tag, channal, language,note,owner,hit,create_time,modify_time,receive_time )
  120. VALUES (NULL, ? , ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ";
  121. $stmt = @PDO_Execute($query, $parm);
  122. if (!$stmt || ($stmt && $stmt->errorCode() != 0)) {
  123. $error = PDO_ErrorInfo();
  124. $respond['status'] = 1;
  125. $respond['message'] = $error[2] . $query;
  126. } else {
  127. $respond['status'] = 0;
  128. $respond['message'] = $_POST["word"];
  129. $respond['data'] = ["guid"=>$parm[0],
  130. "word"=>$parm[1],
  131. "word_en"=>$parm[2],
  132. "meaning"=>$parm[3],
  133. "other_meaning"=>$parm[4],
  134. "tag"=>$parm[5],
  135. "channal"=>$parm[6],
  136. "language"=>$parm[7],
  137. "note"=>$parm[8],
  138. "owner"=>$parm[9]
  139. ];
  140. }
  141. }
  142. #更新 redis
  143. if ($redis != false) {
  144. {
  145. # code...
  146. $query = "SELECT id,word,meaning,other_meaning,note,owner,language from term where word = ? ";
  147. $stmt = $PDO->prepare($query);
  148. $stmt->execute(array($_POST["word"]));
  149. if ($stmt) {
  150. $Fetch = $stmt->fetchAll(PDO::FETCH_ASSOC);
  151. $redisWord=array();
  152. foreach ($Fetch as $one) {
  153. # code...
  154. $redisWord[] = array($one["id"],
  155. $one["word"],
  156. "",
  157. "",
  158. "",
  159. $one["meaning"]."$".$one["other_meaning"],
  160. $one["note"],
  161. "",
  162. "",
  163. 1,
  164. 100,
  165. $one["owner"],
  166. "term",
  167. $one["language"]
  168. );
  169. }
  170. $redis->hSet("dict://term",$_POST["word"],json_encode($redisWord,JSON_UNESCAPED_UNICODE));
  171. }
  172. }
  173. }
  174. #更新redis结束
  175. echo json_encode($respond, JSON_UNESCAPED_UNICODE);