doubingjiu3199 2018-11-01 03:19
浏览 83

如何使用php中的复选框更新多个记录? [重复]

This question already has an answer here:

I would like to create an update for my table in php. Here the design of my table:

Overtime Table

Then Here are the codes in displaying the table:

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

Here my Code for Update:

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


    }
}

When trying to run this code I'm getting an error

Parse error: syntax error, unexpected 'as' (T_AS) in C:\xampp\htdocs\xampp\BISCOMHRMS\overtime_addconn.php on line 38

But still I don't know if my codes are correct or not.

</div>
  • 写回答

1条回答 默认 最新

  • dougehe2022 2018-11-01 03:29
    关注

    Try below code, You have added two bracket in foreach loop,

      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);
    
    
        }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算