자바스크립트 체크박스 배열 값 DB저장

자바스크립트 체크박스 배열 값 DB저장

QA

자바스크립트 체크박스 배열 값 DB저장

본문


function ajax_del(){
        if (!confirm(`정말로 삭제하시겠습니까?`)) {
        //취소(아니오) 버튼 클릭 시 이벤트
       } else {
          //  확인(예) 버튼 클릭 시 이벤트
    var chk = [];
    $("input[name='chk[]']:checked").each(function(i){
        chk.push($(this).val());
    });
    let data = {"chk" : chk};
    console.log(data);
    $.ajax({
        url:"<?php echo G5_URL?>/admin/ajax_delete.php",
        type:'POST',
        data: data,
        success:function(data){
            alert("삭제가 완료되었습니다.");
            location.reload(true);
        },
        error:function(jqXHR, textStatus, errorThrown){
            alert("에러 발생~~ \n" + textStatus + " : " + errorThrown);
            self.close();
        }
    });
       }
    }

스크립트로 체크된 배열값을 보내긴 하였습니다.

 

ajax_delete.php


<?php
    include_once $_SERVER['DOCUMENT_ROOT'].'/common.php';
    $chk_arr = $_POST["chk"];
$cnt = count($chk_arr);
$subqry='';
$it_id_array = array();
$it_price_array = array();
$pin_array = array();
 
for($i=0; $i<$cnt; $i++) {
if($subqry) $subqry .= ',';
$subqry .= $chk_arr[$i];
}
 
if($subqry) {
$find_no = "SELECT * FROM sw_pin_box WHERE no = ('.$subqry.')";
//sql_query("INSERT INTO sw_pin_box_del SET no = '{$find_no['no']}', it_id = '{$find_no['it_id']}', it_name = '{$find_no['it_name']}', it_price = '{$find_no['it_price']}', pin = '{$find_no['pin']}', status = 'N', datetime = NOW()");
sql_query('DELETE from sw_pin_box where no in ('.$subqry.')');
}
?>

배열값은 no이고 no에 해당된 데이터정보들을 삭제는 가능합니다. 하지만 sw_pin_box테이블에서는 삭제되지만 그 정보들을 그대로 sw_pin_box_del테이블에 insert를 해야하는데 이름이랑 등등을 배열로 다시 해야하는데 어려워서 해결하지못하고있습니다ㅠㅠㅠ 방법좀알려주세요..

이 질문에 댓글 쓰기 :

답변 1


if($subqry) {
    $find_no = sql_fetch("SELECT * FROM sw_pin_box WHERE no IN ($subqry)");
    if ($find_no) {
        sql_query("INSERT INTO sw_pin_box_del (no, it_id, it_name, it_price, pin, status, datetime) 
                   VALUES ('{$find_no['no']}', '{$find_no['it_id']}', '{$find_no['it_name']}', '{$find_no['it_price']}', '{$find_no['pin']}', 'N', NOW())");
    }
    sql_query("DELETE from sw_pin_box where no in ($subqry)");
}

 

dud 님 다시 뵙네요, 해당 코드로 해보시겠나요?


if($subqry) {
    $cnt = count($chk_arr);
    for($i=0; $i<$cnt; $i++) {
        $find_no = sql_fetch("SELECT * FROM sw_pin_box WHERE no = $chk_arr[$i]");
        if ($find_no) {
            sql_query("INSERT INTO sw_pin_box_del (no, it_id, it_name, it_price, pin, status, datetime) 
                       VALUES ('{$find_no['no']}', '{$find_no['it_id']}', '{$find_no['it_name']}', '{$find_no['it_price']}', '{$find_no['pin']}', 'N', NOW())");
        }
    }
    sql_query("DELETE from sw_pin_box where no in ($subqry)");
}

답변을 작성하시기 전에 로그인 해주세요.
전체 819
QA 내용 검색
filter #DB ×

회원로그인

(주)에스아이알소프트 / 대표:홍석명 / (06211) 서울특별시 강남구 역삼동 707-34 한신인터밸리24 서관 1404호 / E-Mail: admin@sir.kr
사업자등록번호: 217-81-36347 / 통신판매업신고번호:2014-서울강남-02098호 / 개인정보보호책임자:김민섭(minsup@sir.kr)
© SIRSOFT