소스 파일 : /reform/reform_man_update.php (2020-10-23)     소스 설명 : (개선중) 메뉴얼,코드 업데이트
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
<?php // (2020.10.23, 차재복, Cha Jae Bok, http://www.ktword.co.kr) 

// 코드 항목 신규 추가
function code_new_add($id, $dbi) {

	# code 테이블
	// code id 중 빈 번호 확인
	$query = "select a.id + 1 as available 
					from code a left join {$table_name} b on b.id = (a.id + 1) 
					where b.id is null 
					order by a.id limit 0,1";
	$result = mysqli_query($dbi,$query);
		if (mysqli_errno($dbi)) { $err_msg .= mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n"; }
	$matched = mysqli_fetch_assoc($result);
	$available_id = $matched[available];

	// code no 중 빈 번호 확인
	$query = "select a.no + 1 as available 
					from code a left join code b on b.no = (a.no + 1) 
					where b.no is null 
					order by a.no limit 0,1";
	$result = mysqli_query($dbi,$query);
		if (mysqli_errno($dbi)) { $err_msg .= mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n"; }
	$matched = mysqli_fetch_assoc($result);
	$available_no = $matched[available];

	// code 신규 id,no 삽입
	$query = "insert into code (no,id,id_seq,id_name,date) values ($available_no,$available_id,1,'임시',now())";
	$result = mysqli_query($dbi,$query);
		if (mysqli_errno($dbi)) { $err_msg .= mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n"; }

	# reform 테이블
	// reform 테이블의 날짜 업데이트
	$query = "update reform set date=now() where id=$id";
	$result = mysqli_query($dbi,$query);
		if (mysqli_errno($dbi)) { $err_msg .= mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n"; }

	# reform_more 테이블
	// reform_more 테이블의 현재 id에 more_type, more_ptr 업데이트
	$query = "insert reform_more (id,more_type,more_ptr) values ($id,1,$available_id)";
	$result = mysqli_query($dbi,$query);
		if (mysqli_errno($dbi)) { $err_msg .= mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n"; }

	// 결과 리턴
	if (empty($err_msg)) {
		if (mysqli_affected_rows($dbi) > 0){
			$notice = "reform_more 테이블에 신규 코드 추가 성공";
		} else {
			$notice = "reform_more 테이블에 신규 코드 추가 실패";
		}
	} 

	$return = array('err_msg'=>$err_msg,'notice'=>$notice);
	echo json_encode($return, JSON_UNESCAPED_UNICODE);

}


// 코드 항목 업데이트
function code_update($no, $str, $msg, $dbi, $table_name='code') {

		$str = mysqli_real_escape_string($dbi,$str);
		$query = "update $table_name set id_name='{$msg}',code='{$str}',date=now() where no={$no}";
		$result = mysqli_query($dbi,$query);
			if (mysqli_errno($dbi)) {echo mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n";}
		if (mysqli_affected_rows($dbi) > 0){
			$query = "select id_name,code,date_format(date,'%Y-%m-%d') as date from $table_name where no={$no}";
			$result = mysqli_query($dbi,$query);
				if (mysqli_errno($dbi)) {echo mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n";}
			$matched = mysqli_fetch_assoc($result);

			$return = array('code'=>$matched[code],'date'=>$matched[date],'name'=>$matched[id_name]);
			echo json_encode($return, JSON_UNESCAPED_UNICODE);
		} else {
			echo false;
		}

}


// 메뉴얼 항목 업데이트
//	if ($ch == 'man_update' and !empty($str) and !empty($msg) and is_numeric($no)) {
function manual_update($no, $str, $msg, $dbi, $table_name='manual') {
		// 해킹 방어
		$msg = trim($msg);
		$msg = mysqli_real_escape_string($dbi,$msg);
		$str = trim($str);
		$str = mysqli_real_escape_string($dbi,$str);

		$query = "update manual set id_name='{$msg}',txt='{$str}',date=now() where no={$no}";
		$result = mysqli_query($dbi,$query);
			if (mysqli_errno($dbi)) { $err_msg .= mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n"; }

		if (mysqli_affected_rows($dbi) > 0){
			$query = "select id_name,txt,date_format(date,'%Y-%m-%d') as date from manual where no={$no}";
			$result = mysqli_query($dbi,$query);
				if (mysqli_errno($dbi)) { $err_msg .= mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n"; }
			$matched = mysqli_fetch_assoc($result);
		} else {
			$err_msg = '업데이트 실패!';
		}

		$return = array('err_msg'=>$err_msg,'code'=>$matched[txt],'date'=>$matched[date],'name'=>$matched[id_name]);
		echo json_encode($return, JSON_UNESCAPED_UNICODE);

}


// 메뉴얼 항목 삭제
function manual_delete($no, $id, $dbi) {
	$query = "select id_seq from manual where no=$no and id=$id limit 1";
	$result = mysqli_query($dbi,$query);
		if (mysqli_errno($dbi)) { $err_msg .= mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n"; }
	$matched = mysqli_fetch_assoc($result);
	$current_id_seq = $matched[id_seq];
	
	$query = "update manual set id_seq=(id_seq-1) where id=$id and id_seq>$current_id_seq";
	$result = mysqli_query($dbi,$query);
		if (mysqli_errno($dbi)) { $err_msg .= mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n"; }

	$query = "delete from manual where no=$no and id=$id limit 1";
	$result = mysqli_query($dbi,$query);
		if (mysqli_errno($dbi)) { $err_msg .= mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n"; }

	// 현재 id 산하 자식 없으면, reform_more 해당 레코드 삭제
	$query = "select count(*) as cnt from manual where id=$id";
	$result = mysqli_query($dbi,$query);
		if (mysqli_errno($dbi)) { $err_msg .= mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n"; }
	$matched = mysqli_fetch_assoc($result);
	$cnt = $matched[cnt];

	if ($cnt < 1) {
		// reform_more 해당 레코드 삭제
		$query = "delete from reform_more where more_type='2' and more_ptr=$id limit 1";
		$result = mysqli_query($dbi,$query);
			if (mysqli_errno($dbi)) { $err_msg .= mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n"; }
		if (mysqli_affected_rows($dbi) < 1) $notice .= 'reform_more 1건 삭제 못함';
		
	}

	$return = array('err_msg'=>$err_msg);
	echo json_encode($return, JSON_UNESCAPED_UNICODE);
}


// 메뉴얼 항목을 바로다음에 추가
function manual_insert($id, $no, $dbi) {

	// manual 현재 항목 세부정보 추출
	$query = "select id,id_seq from manual where id=$id and no=$no limit 1";
	$result = mysqli_query($dbi,$query);
		if (mysqli_errno($dbi)) { $err_msg .= mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n"; }
	$matched = mysqli_fetch_assoc($result);
	$cur_seq = $matched[id_seq];
	$cur_id = $matched[id];

	if( mysqli_num_rows($result) <= 0 ) {
		$err_msg .= "해당되는 항목 없음({$no})";

	} else { 
		// manual no 중 빈 번호 확인
		$query = "select a.no + 1 as available 
						from manual a left join manual b on b.no = (a.no + 1) 
						where b.no is null 
						order by a.no limit 0,1";
		$result = mysqli_query($dbi,$query);
			if (mysqli_errno($dbi)) { $err_msg .= mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n"; }
		$matched = mysqli_fetch_assoc($result);
		$available_no = $matched[available];

		// manual 직후 id_seq 부터 1씩 증가시킴
		$query = "update manual set id_seq=(id_seq+1) where parent=$id and id_seq>$cur_seq";
		$result = mysqli_query($dbi, $query);

		// manual 직후 항목 생성
		$query = "insert into manual (no,id,id_seq,id_name,date) values ($available_no,$cur_id,$cur_seq+1,'임시',now())";
		$result = mysqli_query($dbi,$query);
			if (mysqli_errno($dbi)) { $err_msg .= mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n"; }
	}

	// 결과 JSON 송출
	$return = array('err_msg'=>$err_msg,'no'=>$available_no,'name'=>'(임시)','date'=>date('Y-m-d'),'seq'=>($cur_seq+1));
	echo json_encode($return, JSON_UNESCAPED_UNICODE);
}

// 메뉴얼 항목 신규 추가
function manual_new_add($id, $dbi) {

	# manual 테이블
	// manual id 중 빈 번호 확인
	$query = "select a.id + 1 as available 
					from manual a left join manual b on b.id = (a.id + 1) 
					where b.id is null 
					order by a.id limit 0,1";
	$result = mysqli_query($dbi,$query);
		if (mysqli_errno($dbi)) { $err_msg .= mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n"; }
	$matched = mysqli_fetch_assoc($result);
	$available_id = $matched[available];

	// manual no 중 빈 번호 확인
	$query = "select a.no + 1 as available 
					from manual a left join manual b on b.no = (a.no + 1) 
					where b.no is null 
					order by a.no limit 0,1";
	$result = mysqli_query($dbi,$query);
		if (mysqli_errno($dbi)) { $err_msg .= mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n"; }
	$matched = mysqli_fetch_assoc($result);
	$available_no = $matched[available];

	// manual 신규 id,no 삽입
	$query = "insert into manual (no,id,id_seq,id_name,date) values ($available_no,$available_id,1,'임시',now())";
	$result = mysqli_query($dbi,$query);
		if (mysqli_errno($dbi)) { $err_msg .= mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n"; }

	# reform 테이블
	// reform 테이블의 날짜 업데이트
	$query = "update reform set date=now() where id=$id";
	$result = mysqli_query($dbi,$query);
		if (mysqli_errno($dbi)) { $err_msg .= mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n"; }

	// reform 테이블의 부모 id
	$query = "select parent from reform where id=$id";
	$result = mysqli_query($dbi,$query);
		if (mysqli_errno($dbi)) { $err_msg .= mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n"; }
	$matched = mysqli_fetch_assoc($result);
	$parent_id = $matched[parent];

	# reform_more 테이블
	// reform_more 테이블의 현재 id에 more_type, more_ptr 업데이트
	$query = "insert reform_more (id,more_type,more_ptr) values ($id,2,$available_id)";
	$result = mysqli_query($dbi,$query);
		if (mysqli_errno($dbi)) { $err_msg .= mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n"; }

	# 현재 Li 노드의 정보
//	$currentParentInfo = nextParentChilds($parent_id, $dbi);
	$currentLiInfo = currentLiInfo($id, $dbi);

	$return = array('err_msg'=>$err_msg,'currentLiInfo'=>$currentLiInfo);
	echo json_encode($return, JSON_UNESCAPED_UNICODE);
}


// 메뉴얼 항목 끝에 추가
function manual_add($id, $ch, $sub_ch, $msg, $str, $dbi, $table_name='manual') {

	$title = mysqli_real_escape_string($dbi,$msg);
	$content = mysqli_real_escape_string($dbi,$str);

	// no 중 빈 번호 찾아냄
	$query = "select a.no + 1 as available from {$table_name} a left join {$table_name} b on b.no = (a.no + 1) 
					where b.no is null order by a.no limit 0,1";
	$result=mysqli_query($dbi,$query);
		if (mysqli_errno($dbi)) { $err_msg .= mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n"; }
	$matched=mysqli_fetch_assoc($result);
	// 텅 비었으면, $available_no = 1 (초기값)
	$available_no = ( empty($matched[available]) ? 1 : $matched[available] );

	// 해당 id의 자식 최대 번호 알아보기
	$query = "select max(id_seq) as max_id_seq from {$table_name} where id=$id";
	$result = mysqli_query($dbi,$query);
		if (mysqli_errno($dbi)) { $err_msg .= mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n"; }
	$matched = mysqli_fetch_assoc($result);
	$max_id_seq = $matched[max_id_seq] + 1;

	$query = "insert into {$table_name} (no,id,id_seq,id_name,txt,date) values ($available_no,$id,$max_id_seq,'{$title}','{$content}',now())";
	$result = mysqli_query($dbi,$query);
		if (mysqli_errno($dbi)) { $err_msg .= mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n"; }
	
	// 결과 리턴
	if (mysqli_affected_rows($dbi) > 0){
		$return = array('notice'=>'메뉴얼 항목 추가 성공 !!!');
	} else {
		$return = array('err_msg'=>$err_msg.'메뉴얼 항목 추가 에러','notice'=>$query);
	}

	echo json_encode($return, JSON_UNESCAPED_UNICODE);
}

?>


Copyrightⓒ written by 차재복 (Cha Jae Bok)
"본 웹사이트 내 모든 저작물은 원출처를 밝히는 한 자유롭게 사용(상업화포함) 가능합니다"