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

时间:2018-11-01 03:19:49

标签: php mysql

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

Overtime Table

然后这是显示表格的代码:

public function actionCreate()
{
    $session = Yii::$app->session;
    $model = new Memoriam(['scenario'=>'create']);

    if ($model->load(Yii::$app->request->post())) {
        //$model->owner_id = $session['UserID'];
        $model->picture1 = UploadedFile::getInstance($model, 'picture1');
        //$model->imageFiles = UploadedFile::getInstances($model, 'imageFiles');
            if ($model->validate()) {
                if($model->picture1 != null)
                {
                    $dir = "img/".$session['UserID']."/memoriam/";

                    if(!file_exists($dir)) 
                    {
                        $old = umask(0);
                        mkdir($dir, 0777, true);
                        umask($old);
                    }
                    $path = md5(date("YmdHis")) . '.' . $model->picture1->extension;
                    $model->picture1->saveAs($dir.$path);
                    $model->picture1 = $path;
                    $model->page = 1;
                }



            if($model->save(false)){
                return $this->redirect(['site/profile', 'id' => $model->ID]);
            }
        }
        else {
            return $this->render('create', ['model' => $model]);
        }
    }

    return $this->render('create', [
        'model' => $model,
    ]);
}

public function actionCreateDeparted()
{
    $session = Yii::$app->session;
    $model = new Memoriam(['scenario'=>'create']);

    if ($model->load(Yii::$app->request->post())) {
        //$model->owner_id = $session['UserID'];
        $model->picture1 = UploadedFile::getInstance($model, 'picture1');
        //$model->imageFiles = UploadedFile::getInstances($model, 'imageFiles');
            if ($model->validate()) {
                if($model->picture1 != null)
                {
                    $dir = "img/".$session['UserID']."/memoriam/";

                    if(!file_exists($dir)) 
                    {
                        $old = umask(0);
                        mkdir($dir, 0777, true);
                        umask($old);
                    }
                    $path = md5(date("YmdHis")) . '.' . $model->picture1->extension;
                    $model->picture1->saveAs($dir.$path);
                    $model->picture1 = $path;
                    $model->page = 8;
                }



            if($model->save(false)){
                return $this->redirect(['site/profile', 'id' => $model->ID]);
            }
        }
        else {
            return $this->render('create-departed', ['model' => $model]);
        }
    }

    return $this->render('create-departed', [
        'model' => $model,
    ]);
}

public function actionCreateAnniversary()
{
    $session = Yii::$app->session;
    $model = new Memoriam(['scenario'=>'create']);
    $modelAnn = new Anniversary();

    if ($model->load(Yii::$app->request->post()) && $modelAnn->load(Yii::$app->request->post())) {
        //$model->owner_id = $session['UserID'];
        $model->picture1 = UploadedFile::getInstance($model, 'picture1');
        //$model->imageFiles = UploadedFile::getInstances($model, 'imageFiles');
            if ($model->validate()) {
                if($model->picture1 != null)
                {
                    $dir = "img/".$session['UserID']."/memoriam/";

                    if(!file_exists($dir)) 
                    {
                        $old = umask(0);
                        mkdir($dir, 0777, true);
                        umask($old);
                    }
                    $path = md5(date("YmdHis")) . '.' . $model->picture1->extension;
                    $model->picture1->saveAs($dir.$path);
                    $model->picture1 = $path;

                }
            $model->page = 4;
            $model->save();
            $modelAnn->decease_id = $model->ID;
            $modelAnn->save();


            if($model->save(false)){
                return $this->redirect(['site/profile', 'id' => $model->ID]);
            }
        }
        else {
            return $this->render('create-departed', [
                'model' => $model,
                'anniversary' => $modelAnn]);

        }
    }

    return $this->render('create-anniversary', [
        'model' => $model,
        'anniversary' =>$modelAnn, 
    ]);
}

这里是我的更新代码:

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

尝试运行此代码时出现错误

  

解析错误:语法错误,出现意外的“ as”(T_AS)   C:\ xampp \ htdocs \ xampp \ BISCOMHRMS \ overtime_addconn.php在第38行

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

1 个答案:

答案 0 :(得分:0)

尝试下面的代码,您在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);


    }