|
|
|
ขอ Code PHP ในการ Search ข้อมูล SQL Server 2008 โดยที่สามารถ Insert/Edit/Delete ได้ |
|
|
|
|
|
|
|
Code (PHP)
<html>
<head>
<title>ThaiCreate.Com PHP & MySQL Tutorial</title>
</head>
<body>
<?
mssql_connect("localhost","sa","");
$objDB = mssql_select_db("mydatabase");
//*** Add Condition ***//
if($_POST["hdnCmd"] == "Add")
{
$strSQL = "INSERT INTO customer ";
$strSQL .="(CustomerID,Name,Email,CountryCode,Budget,Used) ";
$strSQL .="VALUES ";
$strSQL .="('".$_POST["txtAddCustomerID"]."','".$_POST["txtAddName"]."' ";
$strSQL .=",'".$_POST["txtAddEmail"]."' ";
$strSQL .=",'".$_POST["txtAddCountryCode"]."','".$_POST["txtAddBudget"]."' ";
$strSQL .=",'".$_POST["txtAddUsed"]."') ";
$objQuery = mssql_query($strSQL);
if(!$objQuery)
{
echo "Error Save [".mssql_error()."]";
}
//header("location:$_SERVER[PHP_SELF]");
//exit();
}
//*** Update Condition ***//
if($_POST["hdnCmd"] == "Update")
{
$strSQL = "UPDATE customer SET ";
$strSQL .="CustomerID = '".$_POST["txtEditCustomerID"]."' ";
$strSQL .=",Name = '".$_POST["txtEditName"]."' ";
$strSQL .=",Email = '".$_POST["txtEditEmail"]."' ";
$strSQL .=",CountryCode = '".$_POST["txtEditCountryCode"]."' ";
$strSQL .=",Budget = '".$_POST["txtEditBudget"]."' ";
$strSQL .=",Used = '".$_POST["txtEditUsed"]."' ";
$strSQL .="WHERE CustomerID = '".$_POST["hdnEditCustomerID"]."' ";
$objQuery = mssql_query($strSQL);
if(!$objQuery)
{
echo "Error Update [".mssql_error()."]";
}
//header("location:$_SERVER[PHP_SELF]");
//exit();
}
//*** Delete Condition ***//
if($_GET["Action"] == "Del")
{
$strSQL = "DELETE FROM customer ";
$strSQL .="WHERE CustomerID = '".$_GET["CusID"]."' ";
$objQuery = mssql_query($strSQL);
if(!$objQuery)
{
echo "Error Delete [".mssql_error()."]";
}
//header("location:$_SERVER[PHP_SELF]");
//exit();
}
$strSQL = "SELECT * FROM customer";
$objQuery = mssql_query($strSQL) or die ("Error Query [".$strSQL."]");
?>
<form name="frmMain" method="post" action="<?=$_SERVER["PHP_SELF"];?>">
<input type="hidden" name="hdnCmd" value="">
<table width="600" border="1">
<tr>
<th width="91"> <div align="center">CustomerID </div></th>
<th width="98"> <div align="center">Name </div></th>
<th width="198"> <div align="center">Email </div></th>
<th width="97"> <div align="center">CountryCode </div></th>
<th width="59"> <div align="center">Budget </div></th>
<th width="71"> <div align="center">Used </div></th>
<th width="30"> <div align="center">Edit </div></th>
<th width="30"> <div align="center">Delete </div></th>
</tr>
<?
while($objResult = mssql_fetch_array($objQuery))
{
?>
<?
if($objResult["CustomerID"] == $_GET["CusID"] and $_GET["Action"] == "Edit")
{
?>
<tr>
<td><div align="center">
<input type="text" name="txtEditCustomerID" size="5" value="<?=$objResult["CustomerID"];?>">
<input type="hidden" name="hdnEditCustomerID" size="5" value="<?=$objResult["CustomerID"];?>">
</div></td>
<td><input type="text" name="txtEditName" size="20" value="<?=$objResult["Name"];?>"></td>
<td><input type="text" name="txtEditEmail" size="20" value="<?=$objResult["Email"];?>"></td>
<td><div align="center"><input type="text" name="txtEditCountryCode" size="2" value="<?=$objResult["CountryCode"];?>"></div></td>
<td align="right"><input type="text" name="txtEditBudget" size="5" value="<?=$objResult["Budget"];?>"></td>
<td align="right"><input type="text" name="txtEditUsed" size="5" value="<?=$objResult["Used"];?>"></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>
<td><div align="center"><?=$objResult["CustomerID"];?></div></td>
<td><?=$objResult["Name"];?></td>
<td><?=$objResult["Email"];?></td>
<td><div align="center"><?=$objResult["CountryCode"];?></div></td>
<td align="right"><?=$objResult["Budget"];?></td>
<td align="right"><?=$objResult["Used"];?></td>
<td align="center"><a href="<?=$_SERVER["PHP_SELF"];?>?Action=Edit&CusID=<?=$objResult["CustomerID"];?>">Edit</a></td>
<td align="center"><a href="JavaScript:if(confirm('Confirm Delete?')==true){window.location='<?=$_SERVER["PHP_SELF"];?>?Action=Del&CusID=<?=$objResult["CustomerID"];?>';}">Delete</a></td>
</tr>
<?
}
?>
<?
}
?>
<tr>
<td><div align="center"><input type="text" name="txtAddCustomerID" size="5"></div></td>
<td><input type="text" name="txtAddName" size="20"></td>
<td><input type="text" name="txtAddEmail" size="20"></td>
<td><div align="center"><input type="text" name="txtAddCountryCode" size="2"></div></td>
<td align="right"><input type="text" name="txtAddBudget" size="5"></td>
<td align="right"><input type="text" name="txtAddUsed" 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>
</form>
<?
mssql_close($objConnect);
?>
</body>
</html>
Go to : PHP SQL Server Connect to Database (mssql)
การ Search ข้อมูลบทความนี้ครับ
Go to : PHP SQL Server Search Record (mssql)
นำมา Apply กันได้ครับ ไม่ยาก
|
|
|
|
|
Date :
2013-01-10 10:29:26 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ผมทำแล้วมัน Search ไม่ออกอ่ะคับ ดู Code ผมให้หน่อยนะคับ
Code (PHP)
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=tis-620">
<title>จำนวนลูกค้าตามอนุมัติการดำเนินการด้านการตลาด ของ สายงาน น.</title>
<style type="text/css">
body,td {
font-size: small;
}
th {
font-size: small;
}
</style>
</head>
<body bgcolor=#8593e3>
<?include("function/topmenu.php");?><BR>
<?php
function DateThai($strDate)
{
if($strDate<>"")
{
$strYear = date("Y",strtotime($strDate))+543;
$strMonth= date("n",strtotime($strDate));
$strDay= date("j",strtotime($strDate));
//$strHour= date("H",strtotime($strDate));
//$strMinute= date("i",strtotime($strDate));
//$strSeconds= date("s",strtotime($strDate));
$strMonthCut = Array("","ม.ค.","ก.พ.","มี.ค.","เม.ย.","พ.ค.","มิ.ย.","ก.ค.","ส.ค.","ก.ย.","ต.ค.","พ.ย.","ธ.ค.");
$strMonthThai=$strMonthCut[$strMonth];
return "$strDay $strMonthThai $strYear";
}
else
{ return "-"; }
}
?>
<?
$keep_Cust_Type=$_GET["Cust_Type"];
$keep_Sector=$_GET["Sector"];
$keep_Payee=$_GET["Payee"];
//echo $keep_Sector;
//echo $keep_Cust_Type;
//echo $keep_Payee;
?>
<!--************************************************************FORM**************************************************************-->
<CENTER>
<TABLE width="550">
<TR height="120">
<TD bgcolor=#eaeaea>
<FONT SIZE="3"><CENTER><B>ลูกค้าตามอนุมัติการดำเนินการด้านการตลาด ของ สายงาน น.</B></CENTER></FONT>
<CENTER>
<form name="frmSearch" method="GET" action="<?$_SERVER['SCRIPT_NAME']?>";>
<!-- <CENTER><form name="frmSearch" method="post" action="<? print($_SERVER['PHP_SELF']); ?>" onSubmit="return allSelect();"> -->
<TABLE width="400" BORDER=1 RULES=NONE FRAME=BOX>
<TR>
<TD width="150" align="right">ส่วนงาน</FONT></TD>
<TD width="250">
<select name="Sector" id="Sector" style="width:200px">
<option value="ทั้งหมด">- ทั้งหมด -</option>
<?
include("connectdb/connect_10057159.php");
$strSQL1="SELECT DISTINCT Sector
FROM [skndata].[dbo].[Customer_kbn]";
$objQuery1 = mssql_query($strSQL1) or die ("Error Query [".$strSQL1."]");
while($result1=mssql_fetch_array($objQuery1)){
?>
<option value="<?=$result1[Sector];?>" <?if($keep_Sector==$result1[Sector]){ echo "SELECTED";}?>><?=$result1[Sector];?></option>
<?
}
?>
</select>
</TD>
</TR>
<TR>
<TD align="right">ประเภท</FONT></TD>
<TD width="250">
<select name="Cust_Type" id="Cust_Type" style="width:200px">
<option value="ทั้งหมด">- ทั้งหมด -</option>
<?
include("connectdb/connect_10057159.php");
$strSQL2="SELECT DISTINCT Cust_Type FROM [skndata].[dbo].[Customer_kbn]";
$objQuery2 = mssql_query($strSQL2) or die ("Error Query [".$strSQL2."]");
while($result2=mssql_fetch_array($objQuery2)){
?>
<option value="<?=$result2[Cust_Type];?>" <?if($keep_Cust_Type==$result2[Cust_Type]){ echo "SELECTED";} ?>><?=$result2[Cust_Type];?></option>
<?
}
?>
</select>
</TD>
</TR>
<TR>
<TD colspan="2">
<CENTER>
<FONT SIZE="2"><input type="submit" value="ค้นหา"></FONT>
</CENTER>
</TD>
</TR>
</TABLE>
</form>
</CENTER>
</form>
<div align=right><FONT SIZE="2">Managed By สขน.</FONT></div>
</TD>
</TR>
</TABLE>
</CENTER>
<!--************************************************************STORE**************************************************************-->
<?
IF(($keep_Sector != "ทั้งหมด")||($keep_Cust_Type != "ทั้งหมด"))
$objConnect = mssql_connect("10.0.57.159","sa","oF,9yllt") or die("Error Connect to Database");
$objDB = mssql_select_db("skndata");
$strSQL= "EXEC USP_Dealer '".$_GET["Sector"]."', '".$_GET["Cust_Type"]."'";
$objQuery = mssql_query($strSQL) or die ("Error Query [".$strSQL."]");
$Num_Rows = mssql_num_rows($objQuery);
//echo $_GET["Sector"];
//echo $_GET["Cust_Type"];
?>
<!--************************************************************PAGE CAL**************************************************************-->
<center><table width="1000" BORDER=0>
<tr><td width="700">
<?
$Per_Page = 20; // 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;
}
?>
<!--************************************************************PAGE NUMBER**************************************************************-->
<? for($i=$Page_Start;$i<$Page_End;$i++){?>
<FONT SIZE="2">Total <?= $Num_Rows;?> Record | <?=$Num_Pages;?> Page | </FONT>
<?
if($Prev_Page)
{
echo " <a href='$_SERVER[SCRIPT_NAME]?Page=$Prev_Page&Sector=$_GET[Sector]&Cust_Type=$_GET[Cust_Type]'><< Back</a> ";
}
for($i=1; $i<=$Num_Pages; $i++){
if($i != $Page)
{
echo " <a href='$_SERVER[SCRIPT_NAME]?Page=$i&Sector=$_GET[Sector]&Cust_Type=$_GET[Cust_Type]'>$i</a> ";
}
else
{
echo "<b> $i </b>";
}
}
if($Page!=$Num_Pages)
{
echo " <a href ='$_SERVER[SCRIPT_NAME]?Page=$Next_Page&Sector=$_GET[Sector]&Cust_Type=$_GET[Cust_Type]'>Next>></a> ";
}
?>
</td>
</tr>
</table></center>
<!--************************************************************TABLE**************************************************************-->
<CENTER><table width="1050" BORDER=1 RULES=ALL FRAME=BOX>
<tr bgcolor="#003366">
<th width="30" rowspan="2"> <FONT COLOR="#FFFFFF"><div align="center">ที่</div></FONT></th>
<th width="70" rowspan="2"> <FONT COLOR="#FFFFFF"><div align="center">เลขหมาย</div></FONT></th>
<th width="50" rowspan="2"> <FONT COLOR="#FFFFFF"><div align="center">ส่วนงาน</div></FONT></th>
<th width="200" rowspan="2"> <FONT COLOR="#FFFFFF"><div align="center">ประเภทลูกค้า</div></FONT></th>
<th width="180" rowspan="2"> <FONT COLOR="#FFFFFF"><div align="center">ชื่อลูกค้า</div></FONT></th>
<th width="270" colspan="3"> <FONT COLOR="#FFFFFF"><div align="center">ลูกค้าโครงการจ้างตัวแทนจำหน่ายฯ</div></FONT></th>
<th width="140" colspan="2"> <FONT COLOR="#FFFFFF"><div align="center">ระยะเวลาขออนุมัติ</div></FONT></th>
</tr>
<tr bgcolor="#003366">
<th width="140"> <FONT COLOR="#FFFFFF"><div align="center">ผู้รับเงิน</div></FONT></th>
<th width="60"> <FONT COLOR="#FFFFFF"><div align="center">จำนวน (บาท)</div></FONT></th>
<th width="70"> <FONT COLOR="#FFFFFF"><div align="center">วันทีจ่ายเงิน</div></FONT></th>
<th width="70"> <FONT COLOR="#FFFFFF"><div align="center">วันที่เริ่มต้น</div></FONT></th>
<th width="70"> <FONT COLOR="#FFFFFF"><div align="center">วันที่สิ้นสุด</div></FONT></th>
</tr>
<?
while($objResult = mssql_fetch_array($objQuery))
{
?>
<tr bgcolor=#FFFFFF>
<td><FONT SIZE="2"><div align="center"><? echo $j+(20*($Page-1)); echo ".";?></div></FONT></td>
<td><FONT SIZE="2"><div align="center"><?=mssql_result($objQuery["Telno"]);?></div></FONT></td>
<td><FONT SIZE="2"><div align="center"><?=mssql_result($objQuery["Sector"]);?></div></FONT></td>
<td><FONT SIZE="2"><?=mssql_result($objQuery["Cust_Type"]);?></FONT></td>
<td><FONT SIZE="2"><?=mssql_result($objQuery["Name_Customer"]);?></FONT></td>
<td><FONT SIZE="2"><?=mssql_result($objQuery["Payee"]);?></FONT></td>
<td><FONT SIZE="2"><div align="right"><?=number_format(mssql_result($objQuery["Amount_Pay"]), 2, '.', ',');?></div></FONT></td>
<? $Date_Pay2=mssql_result($objQuery["Date_Pay2"]); ?>
<td><FONT SIZE="2"><div align="center"><?=DateThai($Date_Pay2);?></div></FONT></td>
<?$Date_Start2=mssql_result($objQuery["Date_Start2"]);?>
<td><FONT SIZE="2"><div align="center"><?=DateThai($Date_Start2);?></div></FONT></td>
<?$Date_End2=mssql_result($objQuery["Date_End2"]);?>
<td><FONT SIZE="2"><div align="center"><?=DateThai($Date_End2);?></div></FONT></td>
<?
}
?>
</TABLE></CENTER>
<!--************************************************************END TABLE**************************************************************-->
<?
mssql_close($objConnect);
}
?>
</body>
</html>
|
|
|
|
|
Date :
2013-01-10 11:31:20 |
By :
Monkey-Topby |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ดันคับ
|
|
|
|
|
Date :
2013-01-10 13:32:51 |
By :
Monkey-Topby |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 05
|