CI 묻고 답하기

제목 특정id 가져와서 수정하는 쿼리 한번 봐주세요 ㅠ
글쓴이 하얀당근 작성시각 2013/06/18 17:05:35
댓글 : 8 추천 : 0 스크랩 : 0 조회수 : 12513   RSS
이제 코드이그나이터 시작하는 초보입니다. ㅠ
하나씩 따라하고 있는데 db값 수정에서 지금 막혀있네요...

model에서 id값을 못 받고 있어요 ㅠㅠ 아래 $topic_id 부분을 어떻게 고쳐야 id값이 들어가나요??

=======<Model>=======================================================
function update_exec($title, $description){
$this->db->set('created', 'NOW()', false);
$this->db->update('topic',array(
'title'=>$title,
'description'=>$description
),array('id' => $topic_id));       <===이 부분
echo $topic_id;
return $this->db->update_id(); 

========<Controller>=======================================================
function update_exec(){
.................생략..........................
       $topic_id = $this->topic_model->update_exec($this->input->post('title'), $this->input->post('description'));
       $this->load->model('topic_model');
       redirect('/topic/update/'.$topic_id);
.................생략..........................
    }
 
 다음글 insert 질문이요` (3)
 이전글 CI 도메인별 app관리 관련한 질문입니다. (2)

댓글

변종원(웅파) / 2013/06/18 17:14:35 / 추천 0
컨트롤러에서 모델에 값을 전달하지 않고 있습니다.

어디 다른데서 불러오는건가요?

컨트롤러
$topic_id = $this->topic_model->update_exec($this->input->post('title'), $this->input->post('description'), $id);

모델
function update_exec($title, $description, $id){

한대승(불의회상) / 2013/06/18 17:24:58 / 추천 0
id 가 $topic_id 일때 update를 하고 싶은 거라면...
아래 처럼 해 주세요.
$this->db->set('created', 'NOW()', false);

// 추가 할 부분
$this->db->where(array('id' => $topic_id));

// 수정 할 부분
$this->db->update('topic',array(
'title'=>$title,
'description'=>$description
)); 

하얀당근 / 2013/06/19 13:04:13 / 추천 0
해봤는데 아직 안돼네요 ㅠㅠ

 
 function update_exec($title, $description, $id){
  $this->db->set('created', 'NOW()', false);

  $data = array(
               'title' => $title,
               'description' => $description
            );
  $this->db->where('id',$id);
  $this->db->update('topic', $data); 

  var_dump($data);
  echo $this->db->last_query();

이렇게 var_dump 랑 마지막 쿼리해보니깐 아래처럼 나오네요

array(2) { ["title"]=> string(4) "1212" ["description"]=> string(6) "121212" } UPDATE `topic` SET `created` = NOW(), `title` = '1212', `description` = '121212' WHERE `id` = 0NULL

whre 값이 0으로 계속 나오는데 왜이런지 ㅠㅠ update 페이지에서 히든으로 값을 넘겼습니다.
<input type="hidden" name="id" value="<?= $id ?>" />

 
변종원(웅파) / 2013/06/19 17:00:13 / 추천 0
컨트롤러에서 

$id = $this->input->post('id', true); 로 받아서 모델에 넘겨주신거 맞죠?

컨트롤러에서도 값을 찍어보고 모델에서도 찍어보세요.

값 찍어보면 어디가 문제인지 알 수 있습니다. (아주 기본적인건데요)

쿼리상으로는 0이란 값이 들어가 있습니다.
한대승(불의회상) / 2013/06/19 18:30:33 / 추천 0
ㅎㅎㅎ 복사를 잘못 하셨군요. ^^
저도 자주 하는 실수중 하나 입니다.

틀림
$this->db->where(array('id',$id));

맞음

$this->db->where(array('id' => $id));

변종원(웅파) / 2013/06/19 18:38:40 / 추천 0
 $this->db->where('name', $name);   요게 맞구요

배열로 쓰려면


$this->db->where(array('name'=> $name));  이렇게 써야합니다.
한대승(불의회상) / 2013/06/20 11:02:29 / 추천 0
앗... 제눈에 뭔가가 씌였군요.
웅파님 지적이 맞습니다.

댓글 수정 할게요.
하얀당근 / 2013/06/20 11:31:42 / 추천 0
 해결했습니다 ^^ 모델에서 값을 잘못 넘겨주고 있었습니다 
두 분 덕분에 많은 도움되었습니다. 감사합니다.ㅎ

오늘도 좋은 하루 보내세요~