0

我正在研究这个考勤管理系统,我有一种形式可以更新特定日期的考勤。例如。如果我需要更改今天的出勤率,那么我打开表格,标记出勤率,保存并在数据库中更新出勤率。我为它编写了以下代码:

<?php

$dbhost = "localhost";
$dbuser = "root";
$dbname = "gail";

$conn = mysql_connect($dbhost, $dbuser,"") or die ('Error connecting to mysql');
mysql_select_db($dbname);
$cnt3 = count($_POST['pora']);

if ($cnt3 > 0 ) {
$updateArr = array();
$refArr = array();
for ($i=0; $i<$cnt3; $i++) 
{
    $updateArr[] = "('" . mysql_real_escape_string($_POST['pora'][$i]) . "')";
    $refArr[] = "('". mysql_real_escape_string($_POST['eid'][$i]) . "')";
}

$query = "update attendance set pora=" . implode(", ", $updateArr) . "where eid=" .     implode(", ", $refArr) ;
 mysql_query($query) or trigger_error("Insert failed: " . mysql_error());
}

mysql_close($conn);
?>

当我第一次参加时,它确实适用于多次插入。但它不适用于更新当天的出勤率。有任何想法吗??

4

1 回答 1

0

I got it! n all by mysef..have a look..cheers :)

<?php
$host="localhost"; // Host name 
$username="root"; // Mysql username 
$password=""; // Mysql password 
$db_name="gail"; // Database name 
$tbl_name="attendance"; // Table name 

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);

// Count table rows 
$count=mysql_num_rows($result);
?>
<center>
<table width="500" border="0" cellspacing="1" cellpadding="0">
<form name="form1" method="post" action="test3.php">
<tr> 
<td>
<center>
<table width="500" border="1" bgcolor="orange" cellspacing="1" cellpadding="0">
 <tr>
  <td>&nbsp;</td>
  <td align="left"><label style='color:black; font-family: Verdana; font-weight: bold; font-size: 14px;' for="eid" class="label" id="eid"><b> <i>Date </i> </b></label></td> 
  <td align="left"><label style='color:black; font-family: Verdana; font-weight: bold; font-size: 14px;' for="eid" class="label" id="eid"><b> <i>Employee ID </i> </b> </label></td>
  <td align="left"> <label style='color:black; font-family: Verdana; font-weight: bold; font-size: 14px;' for="ename" class="label" id="ename"><b> <i>Employee Name</i> </b></label></td>
  <td align="left"> <label style='color:black; font-family: Verdana; font-weight: bold; font-size: 14px;' for="pora" class="label" id="pora"><b> <i>Attendance</i> </b></label></td>
  </font>
   <td>&nbsp;</td> 
</tr>   

 <?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td>&nbsp;</td>
<td align="center"><input style='color:black; font-family: Verdana; font-weight: bold;  font-size: 14px; background-color:orange; border-width: 0 0 0px 0; ' readonly name="dated[]" type="text" id="name" value="<?php echo $rows['dated']; ?>"></td>
<td align="center"><input style='color:black; font-family: Verdana; font-weight: bold; font-size: 14px; background-color:orange; border-width: 0 0 0px 0; ' readonly name="eid[]" type="text" id="name" value="<?php echo $rows['eid']; ?>"></td>
<td align="center"><input style='color:black; font-family: Verdana; font-weight: bold; font-size: 14px; background-color:orange; border-width: 0 0 0px 0; ' readonly  name="ename[]" type="text" id="lastname" value="<?php echo $rows['ename']; ?>"></td>
<td align="center"><select style='color:black; font-family: Verdana; font-weight: bold; font-size: 14px;' name="pora[]" id="pora">
<option> P </option>
<option> A </option>
<option> 0.5 </option>
<option> L </option>
<option> Lt </option>
</td>
</tr>
<?php
}
?>

<?php

$eid = $_POST['eid'];
$ename = $_POST['ename'];
$pora = $_POST['pora'];
for($i=0;$i<$count;$i++){
$sql1="UPDATE $tbl_name SET pora='$pora[$i]' WHERE eid='$eid[$i]'";
$result1=mysql_query($sql1);
}

?>
于 2013-07-11T21:23:16.773 回答