我在php中有两个页面,
- php_mysql_multiple_edit2.php //表单
- php_mysql_multiple_edit.php //包含mysql更新命令的脚本
当测试期间数据库中的条目很少时,脚本运行得非常好并更新了表,但当我添加实际表时没有更新。
我收到以下错误
注意:未定义索引:第 6 行 /var/www/ELP/php_mysql_multiple_edit.php 中的 hdnLine 注意:未定义索引:第 25 行 /var/www/ELP/php_mysql_multiple_edit.php 中的 hdnLine 保存完成。
两者的代码是:
php_mysql_multiple_edit2.php
<html>
<head>
<title>Employee Leave Portal</title>
</head>
<body>
<p style="text-align: center">
<a href="index.html">Home</a>
</p>
<?
$objConnect = mysql_connect("localhost","root","root") or die(mysql_error());
$objDB = mysql_select_db("test");
$i=(int)0;
$strSQL = "SELECT * FROM leave_db ORDER BY firstname ASC";
$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
?>
<form name="frmMain" method="post" action="php_mysql_multiple_edit.php">
<table align="center" width="600" border="1">
<tr>
<th bgcolor="#3366CC" width="91"> <div align="center">Employee_id </div></th>
<th bgcolor="#3366CC" width="98"> <div align="center">firstname </div></th>
<th bgcolor="#3366CC" width="198"> <div align="center">lastname </div></th>
<th bgcolor="#3366CC" width="50"> <div align="center">C1 </div></th>
<th bgcolor="#3366CC" width="50"> <div align="center">C2 </div></th>
<th bgcolor="#3366CC" width="50"> <div align="center">C3 </div></th>
<th bgcolor="#3366CC" width="50"> <div align="center">Ct </div></th>
<th bgcolor="#3366CC" width="50"> <div align="center">P1 </div></th>
<th bgcolor="#3366CC" width="50"> <div align="center">P2 </div></th>
<th bgcolor="#3366CC" width="50"> <div align="center">P3 </div></th>
<th bgcolor="#3366CC" width="50"> <div align="center">Pt </div></th>
<th bgcolor="#3366CC" width="50"> <div align="center">M1 </div></th>
<th bgcolor="#3366CC" width="50"> <div align="center">M2 </div></th>
<th bgcolor="#3366CC" width="50"> <div align="center">M3 </div></th>
<th bgcolor="#3366CC" width="50"> <div align="center">Mt </div></th>
<th bgcolor="#3366CC" width="50"> <div align="center">Tt </div></th>
</tr>
<?
$i =0;
while($objResult = mysql_fetch_array($objQuery))
{
$i = $i + 1;
?>
<tr>
<td bgcolor="#3366CC"><div align="center">
<input type="hidden" name="employee_id<?=$i;?>" size="5" value="<?=$objResult["employee_id"];?>">
<input type="text" name="employee_id<?=$i;?>" size="5" value="<?=$objResult["employee_id"];?>">
</div></td>
<td bgcolor="#3366CC"><input type="text" name="firstname<?=$i;?>" size="10" value="<?=$objResult["firstname"];?> "></td>
<td bgcolor="#3366CC"><input type="text" name="lastname<?=$i;?>" size="10" value="<?=$objResult["lastname"];?> "></td>
<td bgcolor="#3366CC"><div align="center"><input type="number" name="C1<?=$i;?>" size="2" value="<?=$objResult["C1"];?>"></div></td>
<td bgcolor="#3366CC"><div align="center"><input type="number" name="C2<?=$i;?>" size="2" value="<?=$objResult["C2"];?>"></div></td>
<td bgcolor="#3366CC"><div align="center"><input type="number" name="C3<?=$i;?>" size="2" value="<?=$objResult["C3"];?>"></div></td>
<td bgcolor="#B2C2F0" width="50"> <div align="center"><?=$objResult["Ct"];?></div></td>
<td bgcolor="#3366CC"><div align="center"><input type="number" name="P1<?=$i;?>" size="2" value="<?=$objResult["P1"];?>"></div></td>
<td bgcolor="#3366CC"><div align="center"><input type="number" name="P2<?=$i;?>" size="2" value="<?=$objResult["P2"];?>"></div></td>
<td bgcolor="#3366CC"><div align="center"><input type="number" name="P3<?=$i;?>" size="2" value="<?=$objResult["P3"];?>"></div></td>
<td bgcolor="#B2C2F0" width="50"> <div align="center"><?=$objResult["Pt"];?></div></td>
<td bgcolor="#3366CC"><div align="center"><input type="number" name="M1<?=$i;?>" size="2" value="<?=$objResult["M1"];?>"></div></td>
<td bgcolor="#3366CC"><div align="center"><input type="number" name="M2<?=$i;?>" size="2" value="<?=$objResult["M2"];?>"></div></td>
<td bgcolor="#3366CC"><div align="center"><input type="number" name="M3<?=$i;?>" size="2" value="<?=$objResult["M3"];?>"></div></td>
<td bgcolor="#B2C2F0" width="50"> <div align="center"><?=$objResult["Mt"];?></div></td>
<td bgcolor="#00FFFF" width="50"> <div align="center"><?=$objResult["Tt"];?></div></td>
</tr>
<?
}
?>
</table>
<input type="submit" name="submit" value="submit">
<input type="hidden" name="hdnLine" value="<?=(int)$i;?>">
</form>
<?
mysql_close($objConnect);
?>
</body>
</html>
php_mysql_multiple_edit.php
<?php
$objConnect = mysql_connect("localhost","root","root") or die(mysql_error());
$objDB = mysql_select_db("test");
$i = (int)0;
//*** Update Condition ***//
for($i=1;$i<=$_POST["hdnLine"];$i++)
{
$strSQL = "UPDATE leave_db SET ";
$strSQL .="employee_id = '".$_POST["employee_id$i"]."' ";
$strSQL .=",firstname = '".$_POST["firstname$i"]."' ";
$strSQL .=",lastname = '".$_POST["lastname$i"]."' ";
$strSQL .=",C1 = '".$_POST["C1$i"]."' ";
$strSQL .=",C2 = '".$_POST["C2$i"]."' ";
$strSQL .=",C3 = '".$_POST["C3$i"]."' ";
$strSQL .=",P1 = '".$_POST["P1$i"]."' ";
$strSQL .=",P2 = '".$_POST["P2$i"]."' ";
$strSQL .=",P3 = '".$_POST["P3$i"]."' ";
$strSQL .=",M1 = '".$_POST["M1$i"]."' ";
$strSQL .=",M2 = '".$_POST["M2$i"]."' ";
$strSQL .=",M3 = '".$_POST["M3$i"]."' ";
$strSQL .="WHERE employee_id = '".$_POST["employee_id$i"]."' ";
$objQuery = mysql_query($strSQL);
}
for($i=1;$i<=$_POST["hdnLine"];$i++)
{
$SUMC1="select (C1+C2-C3) as 'SUMC2' from leave_db where employee_id='".$_POST["employee_id$i"]."'";
$queryC1=mysql_query($SUMC1);
$rowC1=mysql_fetch_array($queryC1);
$final_sumC=$rowC1['SUMC2'];
$update_queryC=" update leave_db set Ct=$final_sumC where employee_id='".$_POST["employee_id$i"]."'";
$objQueryC = mysql_query($update_queryC);
$SUMP1="select (P1+P2-P3) as 'SUMP2' from leave_db where employee_id='".$_POST["employee_id$i"]."'";
$queryP1=mysql_query($SUMP1);
$rowP1=mysql_fetch_array($queryP1);
$final_sumP=$rowP1['SUMP2'];
$update_queryP=" update leave_db set Pt=$final_sumP where employee_id='".$_POST["employee_id$i"]."'";
$objQueryP = mysql_query($update_queryP);
$SUMM1="select (M1+M2-M3) as 'SUMM2' from leave_db where employee_id='".$_POST["employee_id$i"]."'";
$queryM1=mysql_query($SUMM1);
$rowM1=mysql_fetch_array($queryM1);
$final_sumM=$rowM1['SUMM2'];
$update_queryM=" update leave_db set Mt=$final_sumM where employee_id='".$_POST["employee_id$i"]."'";
$objQueryM = mysql_query($update_queryM);
$SUMT1="select (Ct+Pt+Mt) as 'SUMT2' from leave_db where employee_id='".$_POST["employee_id$i"]."'";
$queryT1=mysql_query($SUMT1);
$rowT1=mysql_fetch_array($queryT1);
$final_sumT=$rowT1['SUMT2'];
$update_queryT=" update leave_db set Tt=$final_sumT where employee_id='".$_POST["employee_id$i"]."'";
$objQueryT = mysql_query($update_queryT);
}
//header("location:$_SERVER[PHP_SELF]");
//exit();
$strSQL2 = "SELECT * FROM leave_db ORDER BY firstname ASC";
$objQuery2 = mysql_query($strSQL2) or die ("Error Query [".$strSQL2."]");
echo "Save completed. Click <a href='php_mysql_multiple_edit2.php'>here</a> to view.";
mysql_close($objConnect);
?>`