1.db 데이터구조 설계
2. db를 입력해줄 페이지 구성
<?
echo("<center><h2>재고관리<h2></center>");
echo("
<table border=0 width=800 align=center>
<form method=post action=process.php>
<tr>
<td>상품코드: <input type=text size=8 name=ipcode></td>
<td>상품이름: <input type=text size=11 name=ipname></td>
<td>상품단가: <input type=text size=8 name=ipprice></td>
<td>상품수량: <input type=text size=6 name=ipunit></td>
<td><input type=submit value='신규구입'></td>
</tr>
</form>
<table>
<br>
");
?>
process 생성해서 값들 db에 저장해보기
<?
$con = mysql_connect("localhost","root","apmsetup");
mysql_select_db("class",$con);
$result = mysql_query("insert into product values('$ipcode','$ipname',$ipprice,$ipunit)",$con);
mysql_close($con);
?>
input에 db를 나타낼 폼 추가.
<?
$con = mysql_connect("localhost","root","apmsetup");
mysql_select_db("class",$con);
echo("
<table border=1 width=800 align=center>
<tr border>
<td align=center>상품코드</td>
<td align=center>상품이름</td>
<td align=center>상품단가</td>
<td align=center>상품수량</td>
<td align=center>관리</td>
</tr>
");
$result = mysql_query("select * from product ",$con);
$total = mysql_num_rows($result);
$i=0;
while($i<$total):
$opcode = mysql_result($result,$i,"pcode");
$opname = mysql_result($result,$i,"pname");
$opprice = mysql_result($result,$i,"pprice");
$opunit = mysql_result($result,$i,"punit");
echo("
<tr>
<td align=center>$opcode</td>
<td align=center>$opname</td>
<td align=center>$opprice</td>
<td align=center>$opunit</td>
<td align=center>O/X</td>
</tr>
");
$i++;
endwhile;
echo("</table>");
?>
3. 동일 제품에 추가시 상품 갯수와 상품단가 누적 증가시키기
process.php 코드 수정.
동일한 상품코드가 조건임으로, ipcode를 키 값으로 일관성을 유지.
total 값이 0일땐, 값을 insert하도록하고, 0을 초과할 땐 그 값을 누적 갱신하도록 누적 변수를 만들고 기입함.
<?
$con = mysql_connect("localhost","root","apmsetup");
mysql_select_db("class",$con);
$result = mysql_query("select * from product where pcode='$ipcode'",$con);
$total = mysql_num_rows($result);
if($total>0){
$opprice = mysql_result($result,$i,"pprice");
$opunit = mysql_result($result,$i,"punit");
$npprice = $opprice + $ipprice;
$npunit = $opunit + $ipunit;
mysql_query("update product set pprice=$npprice, punit=$npunit where pcode='$ipcode'" ,$con);
}else{
mysql_query("insert into product values('$ipcode','$ipname',$ipprice,$ipunit)",$con);
}
mysql_close($con);
echo("<meta http-equiv='Refresh' content='0; url=input.php'>");
?>
4. 상품 수정기능 (input.php 수정 , 값을 수정할 화면인 modify.php와 수정값을 db에 반영하는 process2.php 생성)
input 수정내역
echo("
<tr>
<td align=center>$opcode</td>
<td align=center>$opname</td>
<td align=center>$opprice</td>
<td align=center>$opunit</td>
<td align=center><a href=modify.php?mpcode=$opcode>O</a>/X</td>
</tr>
");
modify.php
<?
$con = mysql_connect("localhost","root","apmsetup");
mysql_select_db("class",$con);
$result = mysql_query("select * from product where pcode='$mpcode'",$con);
$total = mysql_num_rows($result);
$old_pname = mysql_result($result,0,"pname");
$old_pprice = mysql_result($result,0,"pprice");
$old_punit = mysql_result($result,0,"punit");
echo("
<table border=0 width=800>
<form method=post action=process2.php?mpcode=$mpcode>
<tr>
<td>상품코드: $mpcode</td>
<td>상품이름: <input type=text size=14 name=ipname value=$old_pname></td>
<td>상품단가: <input type=text size=8 name=ipprice value=$old_pprice></td>
<td>상품수량: <input type=text size=6 name=ipunit value=$old_punit></td>
<td><input type=submit value='수정완료'></td>
</tr>
</form>
<table>
<br>
");
?>
process2.php
<?
$con = mysql_connect("localhost","root","apmsetup");
mysql_select_db("class",$con);
mysql_query("update product set pname='$ipname',pprice=$ipprice,punit=$ipunit where pcode='$mpcode'",$con);
mysql_close($con);
echo("<meta http-equiv='Refresh' content='0; url=input.php'>");
?>
5. 상품 삭제 기능.
input.php 수정내용
echo("
<tr>
<td align=center>$opcode</td>
<td align=center>$opname</td>
<td align=center>$opprice</td>
<td align=center>$opunit</td>
<td align=center><a href=modify.php?mpcode=$opcode>O</a>/<a href=delete.php?mpcode=$opcode>X</a></td>
</tr>
");
delete.php
<?
$con = mysql_connect("localhost","root","apmsetup");
mysql_select_db("class",$con);
mysql_query("delete from product where pcode='$mpcode'",$con);
mysql_close($con);
echo("<meta http-equiv='Refresh' content='0; url=input.php'>");
?>
'🔓데이터베이스 > SQL' 카테고리의 다른 글
메모장 만들기 [primary key 선언하기] (0) | 2023.11.08 |
---|---|
재고관리 매출관리 프로그램 만들어보기[문제2번] (0) | 2023.10.29 |
기초닦기[여러가지 기본 php동작예시들] (0) | 2023.10.28 |
(php + mysql)(3) [수정(modify)/삭제(delete) 기능 만들기] (0) | 2023.10.28 |
야구 기록 관리 메인 프로그램 만들기(php + mysql)(2) (0) | 2023.10.26 |