php - updating multiple rows using singe query -


i working on attendance management system have form of updating attendance particular date. eg. if need make changes in today's attendance, open form, mark attendances, save , attendance gets updated in database. sed followig code it:

<?php  $dbhost = "localhost"; $dbuser = "root"; $dbname = "gail";  $conn = mysql_connect($dbhost, $dbuser,"") or die ('error connecting mysql'); mysql_select_db($dbname); $cnt3 = count($_post['pora']);  if ($cnt3 > 0 ) { $updatearr = array(); $refarr = array(); ($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); ?> 

it work multiple insertions when take attendance first time. doesn't work updating attendance same day. ideas??

i got it! 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 server , select databse. mysql_connect("$host", "$username", "$password")or die("cannot connect");  mysql_select_db("$db_name")or die("cannot select db");  $sql="select * $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> </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]' eid='$eid[$i]'"; $result1=mysql_query($sql1); }  ?> 

Comments