|
|
|
มีปัญหา ในการ เพิ่ม ลบ อัพเดทในหน้าเดียว แบบแบ่งหน้า รบกวนผู้รู้เช็คโค้ดให้หน่อยครับ |
|
|
|
|
|
|
|
เนื่องจากกระทู้
https://www.thaicreate.com/php/php-mysql-add-insert-edit-delete-mysql-same-form.html
https://www.thaicreate.com/php/php-mssql-sql-server-search-record-paging.html
ทำให้ผมลองมาโมดิฟายเพื่อมาใช้ในหน้าเดียวกัน
ผมเขียนโค้ดได้ดังนี้
Code (PHP)
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=tis-620">
</head>
<body>
<form name="frmMain" method="GET" action="<? echo $_SERVER["PHP_SELF"];?>">
<input type="hidden" name="hdnCmd" value="">
<input name="txtKeyword" type="text" id="txtKeyword" value="<?php echo $_GET["txtKeyword"];?>" />
<input type="submit" value="Search"></th>
<?
if( $_GET["txtKeyword"] != "")
{
$objConnect = mssql_connect(".\SQL2008","SA","12349789") or die("Error Connect to Database");
$objDB = mssql_select_db("DEMO");
if($_POST["hdnCmd"] == "Add")
{
$strSQL = "INSERT INTO CSBARCODE ";
$strSQL .="(BARCODE,PRODUCTCODE,BARCODETEXT,UNITCODE,PRICE,STATUS) ";
$strSQL .="VALUES ";
$strSQL .="('".$_POST["txtAddBARCODE"]."','".$_POST["txtAddPRODUCTCODE"]."','".$_POST["txtAddBARCODETEXT"]."' ";
$strSQL .=",'".$_POST["txtAddUNITCODE"]."','".$_POST["txtAddPRICE"]."','".$_POST["txtAddSTATUS"]."') ";
$objQuery = mssql_query($strSQL);
if(!$objQuery)
{
echo "Error Save [".mssql_error()."]";
}
}
if($_POST["hdnCmd"] == "Update")
{
$strSQL = "UPDATE CSBARCODE SET ";
$strSQL .="BARCODE = '".$_POST["txtEditBARCODE"]."' ";
$strSQL .=",PRODUCTCODE = '".$_POST["txtEditPRODUCTCODE"]."' ";
$strSQL .=",BARCODETEXT = '".$_POST["txtEditBARCODETEXT"]."' ";
$strSQL .=",UNITCODE = '".$_POST["txtEditUNITCODE"]."' ";
$strSQL .=",PRICE = '".$_POST["txtEditPRICE"]."' ";
$strSQL .=",STATUS = '".$_POST["txtEditSTATUS"]."' ";
$strSQL .="WHERE BARCODE = '".$_POST["hdnEditBARCODE"]."' ";
$objQuery = mssql_query($strSQL);
if(!$objQuery)
{
echo "Error Update [".mssql_error()."]";
}
}
if($_GET["Action"] == "Del")
{
$strSQL = "DELETE FROM CSBARCODE ";
$strSQL .="WHERE BARCODE = '".$_GET["CusID"]."' ";
$objQuery = mssql_query($strSQL);
if(!$objQuery)
{
echo "Error Delete [".mssql_error()."]";
}
}
$strSQL = "SELECT * FROM CSBARCODE where barcodetext like '%".$_GET["txtKeyword"]."%'";
$objQuery = mssql_query($strSQL) or die ("Error Query [".$strSQL."]");
$Num_Rows = mssql_num_rows($objQuery);
$Per_Page = 15; // Per Page
$Page = $_GET["Page"];
if(!$_GET["Page"])
{
$Page=1;
}
$Prev_Page = $Page-1;
$Next_Page = $Page+1;
$Page_Start = (($Per_Page*$Page)-$Per_Page);
if($Num_Rows<=$Per_Page)
{
$Num_Pages =1;
}
else if(($Num_Rows % $Per_Page)==0)
{
$Num_Pages =($Num_Rows/$Per_Page) ;
}
else
{
$Num_Pages =($Num_Rows/$Per_Page)+1;
$Num_Pages = (int)$Num_Pages;
}
$Page_End = $Per_Page * $Page;
IF ($Page_End > $Num_Rows)
{
$Page_End = $Num_Rows;
}
?>
<table width="600" border="1" align="center">
<tr bgcolor="#FFFF00">
<th width="91"> <div align="center">BARCODE </div></th>
<th width="98"> <div align="center">PRODUCTCODE </div></th>
<th width="198"><div align="center">BARCODETEXT</div></th>
<th width="97"> <div align="center">UNITCODE </div></th>
<th width="59"> <div align="center">PRICE </div></th>
<th width="71"> <div align="center">STATUS </div></th>
<th width="30"> <div align="center">Edit </div></th>
<th width="30"> <div align="center">Delete </div></th>
</tr>
<?
for($i=$Page_Start;$i<$Page_End;$i++)
//while($objResult = mssql_fetch_array($objQuery))
{ ?>
<?
if(mssql_result($objQuery,$i,"BARCODE") == $_GET["CusID"] and $_GET["Action"] == "Edit")
//if($objResult["BARCODE"] == $_GET["CusID"] and $_GET["Action"] == "Edit")
{ ?>
<tr bgcolor="#99CC33">
<td><div align="center">
<input type="text" name="txtEditBARCODE" size="5" value="<?=mssql_result($objQuery,$i,"BARCODE");?>">
<input type="hidden" name="hdnEditBARCODE" size="5" value="<?=mssql_result($objQuery,$i,"BARCODE");?>">
</div></td>
<td><input type="text" name="txtEditPRODUCTCODE" size="20" value="<?=mssql_result($objQuery,$i,"PRODUCTCODE");?>"></td>
<td><input type="text" name="txtEditBARCODETEXT" size="20" value="<?=mssql_result($objQuery,$i,"BARCODETEXT");?>"></td>
<td><div align="center"><input type="text" name="txtEditUNITCODE" size="2" value="<?=mssql_result($objQuery,$i,"UNITCODE");?>"></div></td>
<td align="right"><input type="text" name="txtEditPRICE" size="5" value="<?=mssql_result($objQuery,$i,"PRICE");?>"></td>
<td align="right"><input type="text" name="txtEditSTATUS" size="5" value="<?=mssql_result($objQuery,$i,"STATUS");?>"></td>
<td colspan="2" align="right"><div align="center">
<input name="btnAdd" type="button" id="btnUpdate" value="Update" OnClick="frmMain.hdnCmd.value='Update';frmMain.submit();">
<input name="btnAdd" type="button" id="btnCancel" value="Cancel" OnClick="window.location='<?=$_SERVER["PHP_SELF"];?>';">
</div></td>
</tr>
<?
}
else
{ ?>
<tr bgcolor="#66FFCC">
<td><div align="center"><?=mssql_result($objQuery,$i,"BARCODE");?></div></td>
<td><?=mssql_result($objQuery,$i,"PRODUCTCODE");?></td>
<td><?=mssql_result($objQuery,$i,"BARCODETEXT");?></td>
<td><div align="center"><?=mssql_result($objQuery,$i,"UNITCODE");?></div></td>
<td align="right"><?=mssql_result($objQuery,$i,"PRICE");?></td>
<td align="right"><?=mssql_result($objQuery,$i,"STATUS");?></td>
<td align="center"><a href="<?=$_SERVER["PHP_SELF"];?>?txtKeyword=$_GET[txtKeyword]&Action=Edit&CusID=<?=mssql_result($objQuery,$i,"BARCODE");?>">Edit</a></td>
<td align="center"><a href="JavaScript:if(confirm('Confirm Delete?')==true){window.location='<?=$_SERVER["PHP_SELF"];?>?txtKeyword=$_GET[txtKeyword]&Action=Del&CusID=<?=mssql_result($objQuery,$i,"BARCODE");?>';}">Delete</a></td>
</tr>
<?
}
} ?>
<tr bgcolor="#66FF66">
<td><div align="center"><input type="text" name="txtAddBARCODE" size="5"></div></td>
<td><input type="text" name="txtAddPRODUCTCODE" size="20"></td>
<td><input type="text" name="txtAddBARCODETEXT" size="20"></td>
<td><div align="center"><input type="text" name="txtAddUNITCODE" size="2"></div></td>
<td align="right"><input type="text" name="txtAddPRICE" size="5"></td>
<td align="right"><input type="text" name="txtAddSTATUS" size="5"></td>
<td colspan="2" align="right"><div align="center">
<input name="btnAdd" type="button" id="btnAdd" value="Add" OnClick="frmMain.hdnCmd.value='Add';frmMain.submit();">
</div></td>
</tr>
</table>
<br>
Total <?php echo $Num_Rows;?> Record : <?php echo $Num_Pages;?> Page :
<?php
if($Prev_Page)
{
echo " <a href='$_SERVER[PHP_SELF]?Page=$Prev_Page&txtKeyword=$_GET[txtKeyword]'><< Back</a> ";
}
for($i=1; $i<=$Num_Pages; $i++)
{
if($i != $Page)
{
echo "[ <a href='$_SERVER[PHP_SELF]?Page=$i&txtKeyword=$_GET[txtKeyword]'>$i</a> ]";
}
else
{
echo "<b> $i </b>";
}
}
if($Page!=$Num_Pages)
{
echo " <a href ='$_SERVER[PHP_SELF]?Page=$Next_Page&txtKeyword=$_GET[txtKeyword]'>Next>></a> ";
}
mssql_close($objConnect);
}?>
</FORM>
</body>
</html>
ปรากฏ ว่า ตอนผม กด edit มันไม่สามารถโชว์ค่าใน textbox ได้ทำให้ไม่สามารถทำงานได้ต่ออีก
โดยออพ update add จะไม่สามารถใช้ได้ ทำได้เพิ่งแต่ seach เลือกหน้า ลบข้อมูลเท่านั้น รบกวนลองแก้โค้ดให้หน่อยนะครับ งมมาหลายวันแล้ว
Tag : PHP, Ms SQL Server 2008
|
|
|
|
|
|
Date :
2014-09-19 12:28:45 |
By :
dendeenarat |
View :
989 |
Reply :
4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ลองดูเรื่อง getElementbyID ดูครับ
|
|
|
|
|
Date :
2014-09-19 13:45:58 |
By :
summawat |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
เจอข้อผิดพลาดของการไม่เก็บค่าใน textbox
Code (PHP)
<td align="center"><a href="<?=$_SERVER["PHP_SELF"];?>?txtKeyword=$_GET[txtKeyword]&Action=Edit&CusID=<?=mssql_result($objQuery,$i,"BARCODE");?>">Edit</a></td>
แก้เป็น
Code (PHP)
<td align="center"><a href="<?=$_SERVER["PHP_SELF"];?>?Action=Edit&Page=<? echo $Page;?>&txtKeyword=<? echo $_GET[txtKeyword];?>&CusID=<?=mssql_result($objQuery,$i,"BARCODE");?>">Edit</a></td>
ตอนนี้เหลือข้อผิดพลาดของ การกด button เพื่อเก็บค่าใน Action กด อัพเเล้วไม่อัพแต่อยู่หน้าเดิม แต่กด cancle แล้ว หน้าว่างเหลือแต่ text box เลย
ใครเจอแนวทางแก้ไขหรือแก้โค้ดได้แนะแนวด้วยนะครับ
|
|
|
|
|
Date :
2014-09-19 14:02:22 |
By :
dendeenarat |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ตอนนี้คลิก update แอดข้อมูลไม่ได้ครับ แนะนำทีครับ ลองแล้วไม่ได้เลย
Code (PHP)
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=tis-620">
</head>
<body>
<?
$objConnect = mssql_connect(".\SQL2008","SA","12349789") or die("Error Connect to Database");
$objDB = mssql_select_db("STOCK2015");
if($_GET["hdnCmd"] == "Add")
{
$strSQL = "INSERT INTO CSBARCODE ";
$strSQL .="(BARCODE,PRODUCTCODE,BARCODETEXT,UNITCODE,PRICE,STATUS) ";
$strSQL .="VALUES ";
$strSQL .="('".$_POST["txtAddBARCODE"]."','".$_POST["txtAddPRODUCTCODE"]."','".$_POST["txtAddBARCODETEXT"]."' ";
$strSQL .=",'".$_POST["txtAddUNITCODE"]."','".$_POST["txtAddPRICE"]."','".$_POST["txtAddSTATUS"]."') ";
$objQuery = mssql_query($strSQL);
if(!$objQuery)
{
echo "Error Save [".mssql_error()."]";
}
}
if($_GET["hdnCmd"] == "Update")
{
$strSQL = "UPDATE CSBARCODE SET ";
$strSQL .="BARCODE = '".$_POST["txtEditBARCODE"]."' ";
$strSQL .=",PRODUCTCODE = '".$_POST["txtEditPRODUCTCODE"]."' ";
$strSQL .=",BARCODETEXT = '".$_POST["txtEditBARCODETEXT"]."' ";
$strSQL .=",UNITCODE = '".$_POST["txtEditUNITCODE"]."' ";
$strSQL .=",PRICE = '".$_POST["txtEditPRICE"]."' ";
$strSQL .=",STATUS = '".$_POST["txtEditSTATUS"]."' ";
$strSQL .="WHERE BARCODE = '".$_POST["hdnEditBARCODE"]."' ";
$objQuery = mssql_query($strSQL);
if(!$objQuery)
{
echo "Error Update [".mssql_error()."]";
}
}
if($_GET["Action"] == "Del")
{
$strSQL = "DELETE FROM CSBARCODE ";
$strSQL .="WHERE BARCODE = '".$_GET["CusID"]."' ";
$objQuery = mssql_query($strSQL);
if(!$objQuery)
{
echo "Error Delete [".mssql_error()."]";
}
}
$strSQL = "SELECT * FROM CSBARCODE where barcodetext like '%".$_GET["txtKeyword"]."%'";
$objQuery = mssql_query($strSQL) or die ("Error Query [".$strSQL."]");
?>
<form name="frmMain" method="GET" action="<? echo $_SERVER["PHP_SELF"];?>">
<input type="hidden" name="hdnCmd" value="">
<input name="txtKeyword" type="text" id="txtKeyword" value="<?php echo $_GET["txtKeyword"];?>" />
<input type="submit" value="Search"></th>
<?
if( $_GET["txtKeyword"] != "")
{
$Num_Rows = mssql_num_rows($objQuery);
$Per_Page = 15; // Per Page
$Page = $_GET["Page"];
if(!$_GET["Page"])
{
$Page=1;
}
$Prev_Page = $Page-1;
$Next_Page = $Page+1;
$Page_Start = (($Per_Page*$Page)-$Per_Page);
if($Num_Rows<=$Per_Page)
{
$Num_Pages =1;
}
else if(($Num_Rows % $Per_Page)==0)
{
$Num_Pages =($Num_Rows/$Per_Page) ;
}
else
{
$Num_Pages =($Num_Rows/$Per_Page)+1;
$Num_Pages = (int)$Num_Pages;
}
$Page_End = $Per_Page * $Page;
IF ($Page_End > $Num_Rows)
{
$Page_End = $Num_Rows;
}
?>
<table width="600" border="1" align="center">
<tr bgcolor="#FFFF00">
<th width="91"> <div align="center">BARCODE </div></th>
<th width="98"> <div align="center">PRODUCTCODE </div></th>
<th width="198"><div align="center">BARCODETEXT</div></th>
<th width="97"> <div align="center">UNITCODE </div></th>
<th width="59"> <div align="center">PRICE </div></th>
<th width="71"> <div align="center">STATUS </div></th>
<th width="30"> <div align="center">Edit </div></th>
<th width="30"> <div align="center">Delete </div></th>
</tr>
<?
for($i=$Page_Start;$i<$Page_End;$i++)
//while($objResult = mssql_fetch_array($objQuery))
{ ?>
<?
if(mssql_result($objQuery,$i,"BARCODE") == $_GET["CusID"] and $_GET["Action"] == "Edit")
//if($objResult["BARCODE"] == $_GET["CusID"] and $_GET["Action"] == "Edit")
{ ?>
<tr bgcolor="#99CC33">
<td><div align="center">
<input type="text" name="txtEditBARCODE" size="15" value="<?=mssql_result($objQuery,$i,"BARCODE");?>">
<input type="hidden" name="hdnEditBARCODE" size="5" value="<?=mssql_result($objQuery,$i,"BARCODE");?>">
</div></td>
<td><input type="text" name="txtEditPRODUCTCODE" size="20" value="<?=mssql_result($objQuery,$i,"PRODUCTCODE");?>"></td>
<td><input type="text" name="txtEditBARCODETEXT" size="20" value="<?=mssql_result($objQuery,$i,"BARCODETEXT");?>"></td>
<td><div align="center"><input type="text" name="txtEditUNITCODE" size="7" value="<?=mssql_result($objQuery,$i,"UNITCODE");?>"></div></td>
<td align="right"><input type="text" name="txtEditPRICE" size="7" value="<?=mssql_result($objQuery,$i,"PRICE");?>"></td>
<td align="right"><input type="text" name="txtEditSTATUS" size="7" value="<?=mssql_result($objQuery,$i,"STATUS");?>"></td>
<td colspan="2" align="right"><div align="center">
<input name="btnAdd" type="button" id="btnUpdate" value="Update" OnClick="frmMain.hdnCmd.value='Update';frmMain.submit();">
<input name="btnAdd" type="button" id="btnCancel" value="Cancel" OnClick="window.location='<?=$_SERVER["PHP_SELF"];?>?&Page=<? echo $Page;?>&txtKeyword=<? echo $_GET[txtKeyword];?>';">
</div></td>
</tr>
<?
}
else
{ ?>
<tr bgcolor="#66FFCC">
<td><div align="center"><?=mssql_result($objQuery,$i,"BARCODE");?></div></td>
<td><?=mssql_result($objQuery,$i,"PRODUCTCODE");?></td>
<td><?=mssql_result($objQuery,$i,"BARCODETEXT");?></td>
<td><div align="center"><?=mssql_result($objQuery,$i,"UNITCODE");?></div></td>
<td align="right"><?=mssql_result($objQuery,$i,"PRICE");?></td>
<td align="right"><?=mssql_result($objQuery,$i,"STATUS");?></td>
<td align="center"><a href="<?=$_SERVER["PHP_SELF"];?>?Action=Edit&Page=<? echo $Page;?>&txtKeyword=<? echo $_GET["txtKeyword"];?>&CusID=<?=mssql_result($objQuery,$i,"BARCODE");?>">Edit</a></td>
<td align="center"><a href="JavaScript:if(confirm('Confirm Delete?')==true){window.location='<?=$_SERVER["SCRIPT_NAME"];?>?&txtKeyword=<? echo $_GET["txtKeyword"];?>&Page=<? echo $Page;?>&Action=Del&CusID=<?=mssql_result($objQuery,$i,"BARCODE");?>';}">Delete</a></td>
</tr>
<?
}
} ?>
<tr bgcolor="#66FF66">
<td><div align="center"><input type="text" name="txtAddBARCODE" size="15"></div></td>
<td><input type="text" name="txtAddPRODUCTCODE" size="20"></td>
<td><input type="text" name="txtAddBARCODETEXT" size="20"></td>
<td><div align="center"><input type="text" name="txtAddUNITCODE" size="7"></div></td>
<td align="right"><input type="text" name="txtAddPRICE" size="7"></td>
<td align="right"><input type="text" name="txtAddSTATUS" size="7"></td>
<td colspan="2" align="right"><div align="center">
<input name="btnAdd" type="button" id="btnAdd" value="Add" OnClick="frmMain.hdnCmd.value='Add';frmMain.submit();">
</div></td>
</tr>
</table>
<br>
Total <?php echo $Num_Rows;?> Record : <?php echo $Num_Pages;?> Page :
<?php
if($Prev_Page)
{
echo " <a href='$_SERVER[PHP_SELF]?Page=$Prev_Page&txtKeyword=$_GET[txtKeyword]'><< Back</a> ";
}
for($i=1; $i<=$Num_Pages; $i++)
{
if($i != $Page)
{
echo "[ <a href='$_SERVER[PHP_SELF]?Page=$i&txtKeyword=$_GET[txtKeyword]'>$i</a> ]";
}
else
{
echo "<b> $i </b>";
}
}
if($Page!=$Num_Pages)
{
echo " <a href ='$_SERVER[PHP_SELF]?Page=$Next_Page&txtKeyword=$_GET[txtKeyword]'>Next>></a> ";
}
mssql_close($objConnect);
}?>
</FORM>
</body>
</html>
|
|
|
|
|
Date :
2014-09-19 15:21:36 |
By :
dendeenarat |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ทำได้แว้ว T^T
|
|
|
|
|
Date :
2014-09-19 16:08:27 |
By :
dendeenarat |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 01
|