선택한 행의 DB값만 update하기

선택한 행의 DB값만 update하기

QA

선택한 행의 DB값만 update하기

답변 1

본문

list.php


<ul>
<?php
    $sql = "select * from g5_shop_cart where od_id = '{$od_id}'";
    $result = sql_query($sql);
    for($i=0; $row=sql_fetch_array($result); $i++){
?>
    <li>
        <input type="checkbox" name="chk[]">
        <input type="number" name="quantity" value="<?php echo $row['quantity'] ?>">
    </li>
<?php } ?>
</ul>

 

listupdate.php


for($i=0; $i<count($_POST['chk']); $i++){
    if($_POST['chk'][$i]){
        $sql2 = "update g5_shop_cart
                            set quantity = '{$_POST['quantity'][$i]}'";
        sql_query($sql2);
    }
}

 

안녕하세요.

아직 sql이 익숙하지 않아서 헤매고 있습니다ㅠ

g5_shop_cart 테이블에 quantity라는 열을 추가했고

list.php에서 체크박스를 선택한 데이터값만 업데이트 하고 싶습니다.

지금은 체크하지 않은 다른 행의 값도 다 같이 바뀝니다...

 

질문은 어떻게 하면 선택한 행의 값만 넘길 수 있나요...?

도움 부탁드립니다.

이 질문에 댓글 쓰기 :

답변 1

list.php 부터 잘못되었네요.

장바구니의 키를 저장해주는것이 없네요.

예: 

<input type="checkbox" name="chk[]" value="<?php $row["ca_id"]?>">

 

listupdate.php

$ca_id=$_POST['chk'][$i];

$sql2 = "update g5_shop_cart
                            set quantity = '{$_POST['quantity'][$i]}' where ca_id=$ca_id";

 

참고만 하세요.

 

 

 

 

답변을 작성하시기 전에 로그인 해주세요.
QA 내용 검색
질문등록
전체 21
© SIRSOFT
현재 페이지 제일 처음으로