使用单个查询更新多行

时间:2013-07-11 20:32:39

标签: php html mysql forms sql-update

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

<?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);
?>

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

1 个答案:

答案 0 :(得分:0)

我明白了!所有我自己......看看..查看:)。

<?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);
}

?>