如何使用php中的复选框更新多条记录



我想在php中为我的表创建一个更新。这里是我的桌子设计:

加班表

下面是显示表格的代码:

<?php 
$db_host = 'localhost:3306'; // Server Name
$db_user = 'root'; // Username
$db_pass = ''; // Password
$db_name = 'hrms'; // Database Name
$conn = mysqli_connect($db_host, $db_user, $db_pass, $db_name);
if (!$conn) {
die ('Failed to connect to MySQL: ' . mysqli_connect_error());    
}
?>
<Caption><b>Overtime Authorization</b></Caption>
<br>
<br>
<form method="GET" action="">
Select Employee:
<?php
$sql ="SELECT `chapano`, concat(`Lname`,',',`fname`,`midname`) as 
`fullname` FROM `201` ORDER BY `fullname` ASC";        
$query = mysqli_query($conn, $sql);
echo "<Select type='text' name='droplistsearch'>";
while ($row=mysqli_fetch_array($query)) {
echo"<option value='".$row['chapano']."'>".$row['chapano']." - " 
.$row['fullname']."</option>";
}
echo"</select>";
?>
&nbsp;
&nbsp;
Date From:<input type="date" name="datefrom" id="datefrom">
&nbsp;
&nbsp;
Date To:<input type="date" name="dateto" id="dateto">
<input type="submit" name="submit" value="Submit">
</form>
<br>
<table id="table1">
<thead>
<tr>
<th></th>
<th>Chapa No.</th>
<th>Employee Name</th>
<th>OT Date</th>
<th> OT Time In</th>
<th> OT Time Out</th>
<th>Work Schedule</th>
<th>Justification</th>
<th>Status</th>
<th>Total hours</th>
</tr>
</thead>
<tbody>
<form method="POST" action="overtime_addconn.php" id="OThours">
<?php
if(!empty($_GET['submit'])){
$id = mysqli_real_escape_string($conn,$_REQUEST['droplistsearch']);
$datefrom = mysqli_real_escape_string($conn,$_REQUEST['datefrom']);
$dateto = mysqli_real_escape_string($conn,$_REQUEST['dateto']);  
$sql="SELECT `rowno`,`chapanoss`,`employee`,`date`,
`ottimein`,`ottimeout`,`designation`,`remarks`,`ottotalhrs`,`status` 
FROM `overtimemulti`  WHERE `chapanoss` =  '$id' AND `date` BETWEEN 
'$datefrom' AND '$dateto' ";
$result=mysqli_query($conn, $sql);
$count =mysqli_num_rows($result);
if($sql===FALSE) { die(mysql_error()); }
while($row=mysqli_fetch_array($result)){
?>
<tr>
<td><input type="checkbox"  name="OTchekB" onClick="selectAll(this)" value=" 
<?php echo $row['rowno'];?>"></td>
<td><input type="text" name="txOTchapa"  size="4px"  value="<?php echo 
$row['chapanoss'];?>" ></td>
<td><input type="text" name="txEmpName"  size="30px" value="<?php echo 
$row['employee'];?>" ></td>
<td><input type="Date" name="txOTDate"  value="<?php echo $row['date'];?>" > 
</td>
<td><input type="time" name="txTIMEIN3"  value="<?php echo 
$row['ottimein'];?>" ></td>
<td><input type="time" name="txTIMEOUT3"  value="<?php echo 
$row['ottimeout'];?>" ></td>
<td><input type="text" name="txSchedule"   size="3px" value="<?php echo 
$row['designation'];?>" ></td>
<td><input type="text" name="txJustify" value="<?php echo $row['remarks'];? 
>"></td>
<td>
<select name="txStatusOT" type="text">
<option value="<?php echo $row['status'];?>">Aproved</option>
<option value="Cancel">Cancel</option>
</select>
</td>
<td><input type="text" name="txTotalHrs" size="3" value="<?php $date = 
$row['date'];
$From = $row['ottimein'];
$To =  $row['ottimeout'];
$starttime = new DateTime($date.''.$From);
$endtime = new DateTime($date.''.$To);
$diff = date_diff($starttime,$endtime);
$totalhrs = $diff->h .'.'. $diff->i;
echo $totalhrs;?>"> 
</td>
</tr>
<?php  } ?>   
</tbody>
</table>
<br>
<input type="submit" name="xOTsubmit" value="Submit" />
<input type="submit" name="xOTUpdate" value="SAVE" />
</form>
<?php }mysqli_close($conn); ?>

这里是我的更新代码:

}elseif(isset($_POST['xOTUpdate']))
{
foreach(($_POST['OTchekB'] as $rowCount))
{
$chapano = $_POST['txOTchapa'];
$employeename = $_POST['txEmpName'];
$OTDate = $_POST['txOTDate'];
$OTtimeIn = $_POST['txTIMEIN3'];
$OTtimeOut = $_POST['txTIMEOUT3'];
$Wrksched = $_POST['txSchedule'];
$Remarks = $_POST['txJustify'];
$OTtotalhrs = $_POST['txStatusOT'];
$OTStatus = $_POST['txTotalHrs'];
$sql2="UPDATE `overtimemulti` SET `date`= '$OTDate', `ottimein`='$OTtimeIn', `ottimeout`='$OTtimeOut', 
`designation`='$Wrksched', `remarks`='$Remarks', `ottotalhrs`='$OTtotalhrs',`status`= '$OTStatus' WHERE `rowno`='$rowCount' ";
$result=mysqli_query($conn, $sql2);

}
}

当试图运行此代码时,我得到了一个错误

分析错误:语法错误,中出现意外的"as"(T_as(C: 第38行上的\examplep\htdocs\examplep\BISCOMHRMS\overtime_addconn.php

但我仍然不知道我的代码是否正确。

试试下面的代码,在foreach循环中添加了两个括号,

foreach($_POST['OTchekB'] as $rowCount)
{
$chapano = $_POST['txOTchapa'];
$employeename = $_POST['txEmpName'];
$OTDate = $_POST['txOTDate'];
$OTtimeIn = $_POST['txTIMEIN3'];
$OTtimeOut = $_POST['txTIMEOUT3'];
$Wrksched = $_POST['txSchedule'];
$Remarks = $_POST['txJustify'];
$OTtotalhrs = $_POST['txStatusOT'];
$OTStatus = $_POST['txTotalHrs'];
$sql2="UPDATE `overtimemulti` SET `date`= '$OTDate', `ottimein`='$OTtimeIn', `ottimeout`='$OTtimeOut', 
`designation`='$Wrksched', `remarks`='$Remarks', `ottotalhrs`='$OTtotalhrs',`status`= '$OTStatus' WHERE `rowno`='$rowCount' ";
$result=mysqli_query($conn, $sql2);

}

最新更新