|
|
|
ช่วยดู code หน่อยค่ะ adapt จากบทความ Create Dynamic Elements และ PHP MySQL Add/Insert Multiple Record |
|
|
|
|
|
|
|
ลองมาหลายวันแล้วค่ะ ไม่ได้สักที
Code mysql_insertpaymentPlan.php (PHP)
<?php
include("mysql_connect.php");
for($i=1;$i<=(int)$_POST["hdnMaxLine"];$i++)
{
if(isset($_POST["submit"]))
{
$paymentplandate=$_POST["paymentplandate_".$i];
$paymentplanamount=$_POST["paymentplanamount_".$i];
$jobplanpercent=$_POST["jobplanpercent_".$i];
$contractId=$_POST["contractId_".$i];
echo $_POST["paymentplandate_".$i];
echo $_POST["paymentplanamount_".$i];
echo $_POST["jobplanpercent_".$i];
$query="INSERT INTO paymentplan(paymentplandate,paymentplanamount,jobplanpercent,contractId) VALUES('$paymentplandate','$paymentplanamount','$jobplanpercent','$contractId')";
$result=mysql_query($query) or die(mysql_error());
if($result)
{
echo"Your payment actual's detail recorded";
}else{
echo"cannot record";
}
}
}
?>
paymentplanform.php Code (PHP)
<script language="javascript">
function CreateNewRow()
{
var intLine = parseInt(document.frm.hdnMaxLine.value);
intLine++;
var theTable = document.getElementById("tbExp");
var newRow = theTable.insertRow(theTable.rows.length)
newRow.id = newRow.uniqueID
var newCell
//*** Column 1 ***//
newCell = newRow.insertCell(0);
newCell.id = newCell.uniqueID;
newCell.setAttribute("className", "css-name" );
newCell.innerHTML = "<center><INPUT TYPE=\"DATE\" SIZE=\"5\" CLASS=\"DatePicker\" NAME=\"paymentplandate<?=$i;?>_"+intLine+"\" ID=\"date_"+intLine+"\" VALUE=\"\" ></center>";
//*** Column 2 ***//
newCell = newRow.insertCell(1);
newCell.id = newCell.uniqueID;
newCell.setAttribute("className", "css-name");
newCell.innerHTML = "<center><INPUT TYPE=\"INT\" SIZE=\"20\" NAME=\"paymentamount<?=$i;?>_"+intLine+"\" ID=\"paymentplanamount_"+intLine+"\" VALUE=\"\" ></center>";
//*** Column 3 ***//
newCell = newRow.insertCell(2);
newCell.id = newCell.uniqueID;
newCell.setAttribute("className", "css-name");
newCell.innerHTML = "<center><INPUT TYPE=\"VARCHAR\" SIZE=\"5\" NAME=\"jobplanpercent<?=$i;?>_"+intLine+"\" ID=\"paymentplanpercent_"+intLine+"\" VALUE=\"\"></center>";
//*** Column 4 ***//
newCell = newRow.insertCell(3);
newCell.id = newCell.uniqueID;
newCell.setAttribute("className", "css-name");
newCell.innerHTML = "<center><INPUT TYPE=\"HIDDEN\" SIZE=\"5\" NAME=\"contractId<?=$i;?>_"+intLine+"\" ID=\"contractId_"+intLine+"\" VALUE=\"\"></center>";
document.frm.hdnMaxLine.value = intLine;
}
function RemoveRow()
{
intLine = parseInt(document.frm.hdnMaxLine.value);
if(parseInt(intLine) > 0)
{
theTable = document.getElementById("tbExp");
theTableBody = theTable.tBodies[0];
theTableBody.deleteRow(intLine);
intLine--;
document.frm.hdnMaxLine.value = intLine;
}
}
function GenerateRow()
{
var intRows = parseInt(document.frm.txtCount.value);
for(i=0;i<intRows;i++)
{
CreateNewRow();
}
$$('input.DatePicker').each( function(el) {
new DatePicker(el);
});
}
</script>
<body>
<!-------- search form-------------->
<form name="searchcontract" method="get" action="<?=$_SERVER['SCRIPT_NAME'];?>">
<table width="90%" border=0 cellpadding=0 cellspacing=0>
<tr>
<td align="right">Search>>>
เลขที่สัญญา : <input name="txtKeyword" type="varchar" id="txtKeyword" value="<?=$_GET["txtKeyword"];?>"size="6" />
<input name="GO" type="submit" value="GO" />
</td>
</tr>
</table>
</form>
<!----------function search----------->
<?
if($_GET["txtKeyword"] != "")
{
include("mysql_connect.php");
$objDB = mysql_select_db("jobstatus");
// Search By contractId or contractcode
$strSQL = "SELECT * FROM contract WHERE (contractId LIKE '%".$_GET["txtKeyword"]."%' or contractcode LIKE '%".$_GET["txtKeyword"]."%' )";
$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."] " );
?>
<?
while($objResult=mysql_fetch_array($objQuery))
{
$contractId=$objResult["contractId"];
?>
<table bgcolor="#99FFFF" width="35%" align="center">
<tr>
<td width="39%" height="22"><p class="style1"><b>เลขที่สัญญา : <b><?=$objResult["contractcode"];?></b></b></td></td>
<td width="61%"><p class="style1"><b>ชื่องาน : <b><?=$objResult["jobname"];?></b></b></td>
</tr>
<td width="39%" height="22"><p class="style1"><b>Job No. :</b></td></td>
<td width="61%"><p class="style1"><b>ชื่องาน :</b></td>
</tr>
<tr>
<td height="22" width="39%"><p class="style1"><b>SubJob No. :</b></td></td>
<td height="22" width="61%"><p class="style1"><b>ชื่องานย่อย :</b></td>
</tr>
<?
}
?>
<?
mysql_close($connect);
}
?>
</table>
<!---insert form--->
<form name="frm" id="frm" action="mysql_insertpaymentPlan.php" method="post" >
<table width="35%" border="0" align="center" bgcolor="#FF99FF">
<tr>
<td align="right"><input type="hidden" name="hdnMaxLine" value="0">
<input type="text" name="txtCount" value="1" size="5"><input name="btnCreate" type="button" id="btnCreate" value="เพิ่มจำนวนแถว" onClick="GenerateRow();">
<input name="btnDel" type="button" id="btnDel" value="ลบ" onClick="RemoveRow();">
</td>
</table>
<table width="35%" border="1" id="tbExp" align="center" bgcolor="#FFCCFF">
<tr>
<td><div align="center" >วันที่ </div></td>
<td><div align="center">แผนงบประมาณ (บาท) </div></td>
<td><div align="center">%แผนงาน</div></td>
</tr>
</table>
<table width="35%" border=0 cellpadding=0 cellspacing=0 align="center">
<tr >
<td height="57" colspan="2" ><div align="center">
<input name="submit" value="Add" type="submit">
<input name="reset" value="Cancel" onClick="" type="reset">
</div>
</td>
</tr>
</table>
</form>
</body>
error ที่เกิดขึ้น "Cannot add or update a child row: a foreign key constraint fails (`jobstatus/paymentplan`, CONSTRAINT `contractId_2` FOREIGN KEY (`contractId`) REFERENCES `contract` (`contractId`))"
พอลอง echo ดู จะแสดงค่าของแถวแรกที่ insert ไป ส่วนแถว 2 3 4....... ไม่ปรากฎ
ขอบคุณมากค่ะ
Tag : PHP, MySQL, HTML/CSS, JavaScript
|
|
|
|
|
|
Date :
2012-12-04 09:14:13 |
By :
shushu |
View :
934 |
Reply :
2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
มันติดตรง FK ค่ะ
|
|
|
|
|
Date :
2012-12-04 09:38:04 |
By :
shushu |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 04
|