본문 바로가기
PHP

[PHP]edit.php에서 코드짤 때 주의할 점 [INSERT, UPDATE 화면]

by 개발하자구 2023. 3. 3.

insertupdate를 한 php내에서 사용한다.

 

zcompany.edit.php = INSERT, UPDATE 화면

zcompany_update.php = INSERT, UPDATE 처리

 

이 둘을 구분하기 위해서 신규등록일 때 주소를 ?mode=new로 고정시킨다.

 

만약 modenew 이면 신규등록모드이고,

modenew가 아니면 수정모드가 된다.

(modenew이면 $sql문을 쓸 필요도 없기 때문에 이 모든 걸 if문에 넣어서 update할 때만 쓸 수 있게 걸러내야 한다.)

 

if ($mode != "new"){     // 수정모드일때
    if((int)$cid==0) alert("잘못된 접근입니다.", $href_list);
    
    //if($cid==null || $cid=="" || $cid==0){
	//  alert("잘못된 접근입니다.", $href_list);
    //}

    
    $sql = "select * from zcompany where id=$cid";
    $row = sql_fetch($sql);

    // echo $sql.'<br>';
    // print_r2  ($row);

    if($row==null) alert("자료가 존재하지 않습니다", $href_list);
}

 

 

번외) 같은 페이지를 쓰기 때문에 사용자에게 보여지는 title이름도 경우에 따라 바꿔줘야한다.

$mode_title = ($mode=="new") ? "신규등록" : "거래처 상세보기 수정";

 

 

 

전체코드는

<?php
include_once('./_common.php');

define('_WORK_', true);
if (!defined('_GNUBOARD_')) exit; // 개별 페이지 접근 불가

include_once(G5_PATH.'/head.php');
add_stylesheet('<link rel="stylesheet" href="zcompany.css">', 0);

$href_list = G5_URL."/work/zcompany.list.php";
$href_update = G5_URL."/work/zcompany_update.php";

// if ($cid == null) echo "null입니다";
// if ($cid == "") echo "empty입니다";
// if ($cid == "0") echo "String zero 입니다";
// if ($cid == 0) echo "숫자 0 입니다";

if ($mode != "new"){     // 수정모드일때
    if((int)$cid==0) alert("잘못된 접근입니다.", $href_list);
    
    $sql = "select * from zcompany where id=$cid";
    $row = sql_fetch($sql);

    // echo $sql.'<br>';
    // print_r2  ($row);

    if($row==null) alert("자료가 존재하지 않습니다", $href_list);
}

$mode_title = ($mode=="new") ? "신규등록" : "거래처 상세보기 수정";
?>

<form name="frm_edit" id="frm_edit" action="<?php echo $href_update ?>" method="post">
    <input type="hidden" name="cid" value="<?php echo $cid?>">
    <input type="hidden" name="mode" value="<?php echo $mode?>">        
    <div class="zcom_view01">
        <p><?php echo $mode_title ?></p>    
        <div class="view01_btn"><button type="submit">자료저장</button></div>
    <table>
        <tbody>
            <tr>
                <th>거래처명</th>
                <td><input type="text" name="cname" value="<?php echo $row['cname'] ?>"></td>
                <th>대표자</th>
                <td><input type="text" name="ceo" value="<?php echo $row['ceo'] ?>"></td>
            </tr>
            <tr>
                <th>주소</th>
                <td colspan=3><input type="text" name="address" value="<?php echo $row['address'] ?>"></td>
            </tr>
            <tr>
                <th>전화번호</th>
                <td><input type="text" name="tel" value="<?php echo $row['tel'] ?>"></td>
                <th>핸드폰번호</th>
                <td><input type="text" name="hp" value="<?php echo $row['hp'] ?>"></td>
            </tr>  
            <tr>
                <th>사업시작일</th>
                <td><input type="text" name="dtstart" value="<?php echo $row['dtstart'] ?>"></td>                              
                <th>사업자번호</th>
                <td><input type="text" name="cnumber" value="<?php echo $row['cnumber'] ?>"></td>  
            </tr>         
            <tr>
                <th>기초잔액</th>
                <td><input type="text" name="smoney" value="<?php echo $row['smoney'] ?>"></td>                              
                <th>거래잔액</th>
                <td><input type="text" name="tmoney" value="<?php echo $row['tmoney'] ?>"></td>                              
            </tr>            
        </tbody>
    </table>
    </div>
</form>

<?php include_once(G5_PATH.'/tail.php');?>