| 제목 | 게시판을 만들고 있는데, 궁금한 부분이 있어 질문합니다. | ||
|---|---|---|---|
| 카테고리 | CI 2, 3 | ||
| 글쓴이 | lsh955 | 작성시각 | 2017/05/28 22:36:03 |
|
|
|||
|
등록할때는 문제없는데, 수정페이지 에서 만 한글로 작성하고 저장을 누르는데 한글이 깨지면서 데이터베이스 오류가 뜹니다... 그 이유를 몰라 포럼에 질문드립니다.........
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Main extends CI_Controller // URL과 상호작용하는 클래스 파일
{
function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
$this->load->library('javascript');
}
// public은 클래스내, 클래스외의 어디에서라도 엑세스 가능
// function은 사용자 정의 함수
public function index() {
$this->lists();
}
//리스트
public function lists() {
$this->load->model('board_m');
$data['list'] = $this->board_m->get_list();
$this->load->view('board/lists.html', $data);
}
//작성
public function write() {
// 라이브러리 로드
$this->load->library('form_validation');
$this->load->view('board/write.html');
}
//저장
function save() {
$this->load->model('board_m');
// 두번째 파라미터의 TRUE는 데이터를 XSS 필터링 하게만듬.
$itile = $this->input->post('itile', TRUE);
$content = $this->input->post('content', TRUE);
$name = $this->input->post('name', TRUE);
// 폼 검증 라이브러리 로드
$this->load->library('form_validation');
// required : 검사대상이 비어있으면 FALSE 를 리턴 한다.
$this->form_validation->set_rules('itile', '제목', 'required');
$this->form_validation->set_rules('name', '이름', 'required');
$this->form_validation->set_rules('content', '내용', 'required');
if ($this->form_validation->run() == FALSE) {
$this->load->view('board/write.html');
} else {
$id = $this->input->post('id', TRUE); //수정,저장 구분자
if($id) {
$up_data = "itile = ".$itile.", content =".$content.", name=".$name;
$where = "id = ".$id;
$data = $this->board_m->get_modify('items', $up_data, $where);
} else {
$data = $this->board_m->post_write($itile, $content, $name);
}
header('Location: /index.php/main');
}
}
//수정
function modify() {
$this->load->model('board_m');
$id = $this -> uri -> segment(3);
$data['views'] = $this -> board_m -> get_view($id);
$this->load->view('board/modify.html',$data);
}
//조회
function view() {
$this->load->model('board_m');
// 3번째 세그먼트인 id값을 가져와서 $id로 대입한다.q
$id = $this -> uri -> segment(3);
$data['views'] = $this -> board_m -> get_view($id);
$this->load->view('board/view.html', $data);
}
//삭제
function deletes() {
$this->load->model('board_m');
$id = $this -> uri -> segment(3);
$this -> board_m -> get_deletes($id);
// 삭제버튼을 누르면 바로 리스트페이지로 넘어간다.
header('Location: /index.php/main');
}
}
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Board_m extends CI_Model // 데이터베이스와 연동하여 사용하기위한 PHP 클래스 파일
{
function __construct()
{
parent::__construct();
$this->load->database();
}
//목록
function get_list() {
// items 테이블에 있는 모든 데이터를 불러와서 $sql 변수에 대입..
$sql = "SELECT * FROM items";
$query = $this->db->query($sql);
//마지막으로 사용한 쿼리 문자열을 리턴
$this->db->last_query();
// 쿼리수행의 결과로 순수한 배열을 리턴한다.
$result = $query->result_array();
return $result;
}
//작성
function post_write($itile, $content, $name) {
$sql = "INSERT INTO items (itile, content, name) VALUES ('" . $itile . "', '" . $content . "', '" . $name . "')";
$query = $this->db->query($sql);
}
//수정
function get_modify($table, $up_data, $where) {
$sql ="update ".$table." set ".$up_data." where ".$where;
$query = $this->db->query($sql);
}
//조회
function get_view($id) {
// items 테이블에 id 컬럼을 찾아 id인 데이터만을 찾는다.
$sql = "SELECT * FROM items WHERE id='" . $id . "'";
$query = $this -> db -> query($sql);
// row();는 한줄의 결과만을 리턴하여 $result; 변수로 대입한다.
$result = $query -> row();
return $result;
}
//삭제
function get_deletes($id) {
// DELETE는 데이터 행을 삭제하며, id 컬럼을 찾아 id인 데이터만 골라서 삭제..
$sql = "DELETE FROM items WHERE id = '" . $id . "'";
$this -> db -> query($sql);
}
}
<!DOCTYPE html>
<html>
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<meta http-equiv="content-language" content="ko" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no"/>
<title>게시판 수정</title>
<link type="text/css" rel='stylesheet' href="/application/css/board.css" />
<link type="text/css" rel='stylesheet' href="/application/css/button.css" />
</head>
<body>
<div id="board_write">
<article id="board_area">
<form name="f" method="post" action="/index.php/main/save/<?php echo $this->uri->segment(3); ?>" id="write_action" onsubmit="return check();">
<input type="hidden" name='id' value="<?php echo $this->uri->segment(3); ?>">
<input type="hidden" id="<?= $this->security->get_csrf_token_name() ?>" name="<?= $this->security->get_csrf_token_name() ?>" value="<?= $this->security->get_csrf_hash() ?>" />
<table>
<tbody>
<tr>
<th scope="row" style="height: 50px;">제목<span class="star">*</span></th>
<td>
<input type="text" name="itile" value="<?php echo $views -> itile;?>" autofocus/>
<?php echo form_error('itile', '<div class="error">', '</div>'); ?>
<?php echo form_error('username'); ?>
</td>
</tr>
<tr>
<th scope="row" style="height: 50px;">이름<span class="star">*</span></th>
<td>
<input type="text" name="name" value="<?php echo $views -> name;?>"/>
<?php echo form_error('name', '<div class="error">', '</div>'); ?>
<?php echo form_error('username'); ?>
</td>
</tr>
</tbody>
</table>
<table>
<tbody>
<tr>
<th scope="row" style="padding-right: 98px;">내용<span class="star">*</span></th>
<td>
<textarea name="content" rows="10" cols="60"><?php echo $views -> content;?></textarea>
<?php echo form_error('content', '<div class="error">', '</div>'); ?>
<?php echo form_error('username'); ?>
</td>
</tr>
</tbody>
</table>
<div id="board_btn">
<div class="fl">
<span class="button small icon">
<span class="icon_cancel"></span><a href="/index.php/main"><input type="button" value="취소"/></a>
</span>
<span class="button small icon"><span class="icon_save"></span><input type="submit" value="저장"/>
</span>
</div>
</div>
</form>
</article>
</div>
</body>
</html>
<script type="text/javascript">
/*window.ready({
$("#write_action").submit(function(){
alert("dd");
return false;
});
})*/
function check(){
if(document.f.itile.value.length == 0){
alert("제목은 필수 이다");
document.f.focus();
return false;
}
if(document.f.name.value.length == 0){
alert("이름은 필수 이다");
document.f.focus();
return false;
}
if(document.f.content.value.length == 0){
alert("내용은 필수 이다");
document.f.focus();
return false;
}
document.f.submit();
}
</script>
|
|||
| 다음글 | ajax long polling 방식으로 데이터를 주고... (6) | ||
| 이전글 | foreach 문 입니다. 제가 작성한 코드보다 더 간... (3) | ||
|
변종원(웅파)
/
2017/05/29 08:23:05 /
추천
0
수정 페이지 캐릭터셋 확인해보세요.
|