2013-07-11 50 views
0

我正在爲此考勤管理系統工作,其中我有一種更新特定日期出席的形式。例如。如果我需要在今天的出勤時進行更改,那麼我打開表格,標記出席人數,保存並在數據庫中更新出席人數。我的sed的followig代碼是:使用單詞查詢更新多行

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

它的工作完美地進行多項插入時,我採取的第一次出庭。但它不適用於更新當天的出勤率。有任何想法嗎??

回答

0

我明白了! n的所有由mysef..have一個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); 
} 

?> 
+0

只是一個建議,請使用mysqli的,而不是mysql_,因爲有安全問題(SQL注入)與mysql_擴展 – xsearingheaven

+0

雅ok..i會用mysqli編碼下一個tym..thnks fr提示:) –