|
 |
|
ขอดูตัวอย่างโค๊ดตัวอย่าง update แบบวน loop หน่อยครับ |
|
 |
|
|
 |
 |
|
Code (PHP)
for ($i = 0; $i < count($_POST['id'] ; $i++){
คำสั่ง update
}
* count($_POST['id'] คือ การนับจำนวน id ที่จะทำการอัพเดท
หรอจะรับค่าก่อนเป็น
Code (PHP)
$id = count($_POST['id'];
for ($i = 0; $i < $id ; $i++){
คำสั่ง update
}
|
ประวัติการแก้ไข 2010-11-25 16:35:18
 |
 |
 |
 |
Date :
2010-11-25 16:34:55 |
By :
ไวยวิทย์ |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (PHP)
<html>
<head>
<title>ThaiCreate.Com PHP & MySQL Tutorial</title>
</head>
<body>
<?
$objConnect = mysql_connect("localhost","root","root") or die("Error Connect to Database");
$objDB = mysql_select_db("mydatabase");
//*** Update Condition ***//
if($_GET["Action"] == "Save")
{
for($i=1;$i<=$_POST["hdnLine"];$i++)
{
$strSQL = "UPDATE customer SET ";
$strSQL .="CustomerID = '".$_POST["txtCustomerID$i"]."' ";
$strSQL .=",Name = '".$_POST["txtName$i"]."' ";
$strSQL .=",Email = '".$_POST["txtEmail$i"]."' ";
$strSQL .=",CountryCode = '".$_POST["txtCountryCode$i"]."' ";
$strSQL .=",Budget = '".$_POST["txtBudget$i"]."' ";
$strSQL .=",Used = '".$_POST["txtUsed$i"]."' ";
$strSQL .="WHERE CustomerID = '".$_POST["hdnCustomerID$i"]."' ";
$objQuery = mysql_query($strSQL);
}
//header("location:$_SERVER[PHP_SELF]");
//exit();
}
$strSQL = "SELECT * FROM customer ORDER BY CustomerID ASC";
$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
?>
<form name="frmMain" method="post" action="phpMySQLEditRecord.php?Action=Save">
<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>
</tr>
<?
$i =0;
while($objResult = mysql_fetch_array($objQuery))
{
$i = $i + 1;
?>
<tr>
<td><div align="center">
<input type="hidden" name="hdnCustomerID<?=$i;?>" size="5" value="<?=$objResult["CustomerID"];?>">
<input type="text" name="txtCustomerID<?=$i;?>" size="5" value="<?=$objResult["CustomerID"];?>">
</div></td>
<td><input type="text" name="txtName<?=$i;?>" size="20" value="<?=$objResult["Name"];?>"></td>
<td><input type="text" name="txtEmail<?=$i;?>" size="20" value="<?=$objResult["Email"];?>"></td>
<td><div align="center"><input type="text" name="txtCountryCode<?=$i;?>" size="2" value="<?=$objResult["CountryCode"];?>"></div></td>
<td align="right"><input type="text" name="txtBudget<?=$i;?>" size="5" value="<?=$objResult["Budget"];?>"></td>
<td align="right"><input type="text" name="txtUsed<?=$i;?>" size="5" value="<?=$objResult["Used"];?>"></td>
</tr>
<?
}
?>
</table>
<input type="submit" name="submit" value="submit">
<input type="hidden" name="hdnLine" value="<?=$i;?>">
</form>
<?
mysql_close($objConnect);
?>
</body>
</html>
Go to : PHP MySQL Multiple Rows Edit/Update Record
|
 |
 |
 |
 |
Date :
2010-11-25 17:30:01 |
By :
webmaster |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
อธิบายตรงนี้ทีครับว่า
Code (PHP)
if($_GET["Action"] == "Save")
ว่ามันทํางานยังไง ที่เข้าใจมันดูเงื่อนไข ถ้า รับ ค่า ACtion เท่ากับ SAve แต่ACtionมันมาได้ยังไงครับ
|
 |
 |
 |
 |
Date :
2010-11-25 17:52:17 |
By :
kenghockey |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (PHP)
<form name="frmMain" method="post" action="phpMySQLEditRecord.php?Action=Save">
|
 |
 |
 |
 |
Date :
2010-11-25 20:10:41 |
By :
webmaster |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 00
|