CodeIgniter3

08. [CodeIgniter3] 데이터 수정하기

drizzle0925 2021. 12. 29. 13:13
728x90

데이터 수정하기

이번에는 데이터 수정하는 방법에 대해서 알아봅시다.

 

Controller

application/controllers/Hello.php 에 아래 코드를 입력합니다.

<?php 
    class Hello extends CI_Controller
    {
        public function __construct()
        {
            // CodeIgniter의 기본 생성자 호출
            parent::__construct();
            // 데이터베이스 라이브러리를 수동으로 로드 (auto_load를 사용하지 않는 방법)
            $this->load->database();
            // url 헬퍼 호출. helper란 특정영역에 해당하는 함수들의 모음입니다.
            $this->load->helper('url');
            // 모델 로드
            $this->load->model('Hello_Model');
        }

        public function savedata()
        {
            // 등록 보기 양식 로드
            $this->load->view('registration');

            // 제출 버튼 확인
            if($this->input->post('save'))
            {
                // 폼 데이터를 가져와서 로컬 변수에 저장
                $n = $this->input->post('name');
                $e = $this->input->post('email');
                $m = $this->input->post("mobile");

                // Hello_Model의 saverecord 메소드를 호출하고 변수를 매개변수로 전달
                $this->Hello_Model->saverecords($n, $e, $m);
                redirect("Hello/dispdata");
            }
        }

        public function dispdata()
        {
            // 화면에 표시할 데이터를 모델에 있는 메서드로 받아와서 $result['data']에 넣음
            $result['data'] = $this->Hello_Model->displayrecords();
            // view()메서드 2번째 인자에 배열로 값을 전달하면 View에서 사용할수 있다. View에서 표시할 데이터를 전달.
            $this->load->view('display_records', $result);
        }

        public function deletedata()
        {
            // id값을 get타입으로 받아옵니다.
            $id = $this->input->get('id');
            // Hello_Model에 있는 deleterecords 메서드 호출
            $this->Hello_Model->deleterecords($id);
            // url을 Hello/dispdate로 이동시킴
            redirect("Hello/dispdata");
        }

        public function updatedata()
        {
            // id값을 get타입으로 받아옵니다.
            $id = $this->input->get('id');
            // Hello_Model에 있는 displayrecordsById 메서드 호출 결과값을 $result에 담습니다.
            $result['data'] = $this->Hello_Model->displayrecordsById($id);
            // update_records view에 $result값을 전달하여 호출합니다.
            $this->load->view('update_records', $result);

            // update에 값이 전송되오면 아래 로직을 실행합니다.
            if($this->input->post('update'))
            {
                // post로 전달된 데이터를 변수에 담습니다.
                $n = $this->input->post('name');
                $e = $this->input->post('email');
                $m = $this->input->post('mobile');
                // Hello_Model에 있는 updaterecords 메서드를 호출합니다.
                $this->Hello_Model->updaterecords($n, $e, $m, $id);
                // Hello/dispdata로 리다이렉트 합니다.
                redirect("Hello/dispdata");
            }
        }
    }
?>

 

View

application/views/display_records.php에 아래 코드를 입력합니다.

<!DOCTYPE html>
<html lang="ko">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>데이터 보기</title>
</head>
<body>
    <table width="600" border="1" cellspacing="5" cellpadding="5">
        <tr style="background:#CCC">
            <th>No</th>
            <th>이름</th>
            <th>이메일</th>
            <th>연락처</th>
            <th>삭제하기</th>
            <th>업데이트</th>
        </tr>
        <?php 
        $i = 1;
        foreach($data as $row)
        {
            echo "<tr>";
            echo "<td>".$i."</td>";
            echo "<td>".$row->name."</td>";
            echo "<td>".$row->email."</td>";
            echo "<td>".$row->mobile."</td>";
            echo "<td><a href='deletedata?id=".$row->user_id."'>삭제</a></td>";
            echo "<td><a href='updatedata?id=".$row->user_id."'>업데이트</a></td>";
            echo "</tr>";
            $i++;
        }
        ?>
    </table>
</body>
</html>

 

application/views/update_records.php 파일을 만들어 아래 코드를 입력합니다.

<!DOCTYPE html>
<html lang="ko">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>데이터 수정하기</title>
</head>
<body>
    <?php 
    $i = 1;
    foreach($data as $row)
    {
    ?>
    <form method="post">
        <table width="600" border="1" cellspacing="5" cellpadding="5">
            <tr>
                <td width="230">이름을 입력하세요</td>
                <td width="329"><input type="text" name="name" value="<?php echo $row->name; ?>" /></td>
            </tr>
            <tr>
                <td>이메일을 입력하세요</td>
                <td><input type="text" name="email" value="<?php echo $row->email; ?>" /></td>
            </tr>
            <tr>
                <td>연락처를 입력하세요</td>
                <td><input type="text" name="mobile" value="<?php echo $row->mobile; ?>" /></td>
            </tr>
            <tr>
                <td colspan="2" align="center"><input type="submit" name="update" value="데이터 수정" /></td>
            </tr>
        </table>
    </form>
    <?php
    }
    ?>
</body>
</html>

 

Model

application/models/Hello_Model.php에 아래 코드를 입력합니다.

<?php 
class Hello_Model extends CI_Model
{
    function saverecords($name, $email, $mobile)
    {
        $query = "INSERT INTO users values('','$name','$email','$mobile')";
        $this->db->query($query);
    }

    function displayrecords()
    {
        $query = $this->db->query("select * from users");
        return $query->result();
    }

    function deleterecords($id)
    {
        $this->db->query("delete from users where user_id='".$id."'");
    }

    function displayrecordsById($id)
    {
        $query = $this->db->query("select * from users where user_id='".$id."'");
        return $query->result();
    }

    function updaterecords($name, $email, $mobile, $id)
    {
        $this->db->query("update users set name='$name',email='$email',mobile='$mobile' where user_id='".$id."'");
    }
}
?>

 

수정해보겠습니다.

이름과 메일을 수정합니다.

수정되었습니다.

 

Github

https://github.com/jun0925/study/commit/7151ae2720ba692d4f2af7b2d086e64a93db9237

728x90