2017-04-10 29 views
1

我正在處理一個項目,我想通過使用複選框中的值更新表來爲用戶分配一個角色。我想用複選框更新多個記錄。我想從下拉菜單中選擇用戶時更新新的服務表。在這個階段我需要幫助。當我使用php pdo檢查複選框時,如何更新多個記錄?

#assign estimator 
     $gp = $userDetails->pgroup; 
     $users = $db->prepare('SELECT 
     dbo.[GWCL$west_users].name AS fullname, 
     dbo.[GWCL$west_users].user_group AS pgroup, 
     dbo.[GWCL$west_users].username, 
     dbo.[GWCL$west_users].uid, 
     dbo.[GWCL$west_users].region, 
     dbo.[GWCL$west_users].district 

     FROM dbo.[GWCL$west_users] 
     WHERE 
     dbo.[GWCL$west_users].user_group = 10 
     '); 
     $users->execute(); 



     #update and assign estimating officer 

     if($_POST['process']){ 

      foreach($_POST['dm_approve'] as $id) 
      { 



       $update =$db->prepare('UPDATE dbo.[GWCL$NewServiceConnection] SET dbo.[GWCL$NewServiceConnection].staffID = '.$_POST['staffID'].', dbo.[GWCL$NewServiceConnection].dm_approve = 1 WHERE dbo.[GWCL$NewServiceConnection].Application_ID ='.$id.''); 
       $update->execute(); 
      $result = $update->rowCount(); 
     if ($result ===false) { 
       echo "Failed"; 
       }else{ 
       echo "Status " .$id. " is assigned to ".$_POST['staffID'].". <br>"; 
       } 

     } 
     } 
     ?> 
     <?php include('header.php'); ?> 
     <body class="with-side-menu"> 

     <header class="site-header"> 
      <?php require_once('topnav.php');?> 
      <!--.container-fluid--> 
     </header><!--.site-header--> 

     <div class="mobile-menu-left-overlay"></div> 
     <?php require_once('nav.php');?> 
      <!--.side-menu--> 

     <div class="page-content"> 
      <div class="container-fluid"> 
       <header class="section-header"> 
        <div class="tbl"> 
         <div class="tbl-row"> 
          <div class="tbl-cell"> 
           <h2>NSC Applications Pending Estimation </h2> 
           <div class="subtitle"><?php #echo $aName['Name'];?></div> 
          </div> 
         </div> 
        </div> 
       </header><?php if(isset($_GET['msg'])) 
      { 
       $Message = "<div class='alert alert-success alert-dismissible' role='alert'> 
     <button type='button' class='close' data-dismiss='alert' aria-label='Close'><span aria-hidden='true'>&times;</span></button> 
     <strong>Successful!</strong> Estimation is made on Applicant's account 
     </div>"; 
       echo $Message; 
      }?> 
       <section class="card"> 
        <div class="card-block"> 
         <div class="col-sm-4"><form name="nmc" id="wrapped" method="POST" action="assignedcust.php"> 
           </div> <div class="col-sm-4"><select name="staffID" autofocus class="form-control" id="staffID" > 
           <option value="" selected>Select Estimator</option> 
           <?php foreach($users as $user){?> 

           <option value="<?php echo $user['username'];?>" ><?php echo $user['fullname'];?></option> 
           <?php }?> 
           </select></div> <input type="submit" name="process" id="submit" value="Assign" class="btn btn-primary"/> 
         <table id="example" class="display table table-striped table-bordered" cellspacing="0" width="100%"> 
          <thead> 

          <tr> 
           <th> 
           <input type="checkbox" class="chk_boxes" label="check all" align="left"/> Select All         
                                   </th> 
           <th>Applicant Name </th> 
           <th>Activity</th> 
           <th>Mobile</th> 
           <th>Service Type</th> 
           <th>Purpose</th> 
           <th>House Number</th> 
            <th>Estimate</th> 
          </tr> 
          </thead> 

          <tbody> 
          <?php foreach ($asscust as $acust){?><tr> 
           <td> 
            <input type="hidden" name="dm_approve[]" id="dm_approve[]" value="<?php echo $acust['Application_ID']; ?>"> 
            <input type="checkbox" name="dm_approve[]" id="dm_approve[]" value="<?php echo $acust['Application_ID']; ?>" class="chk_boxes1"><?php echo $acust['Application_ID']; ?> 
           </td> 
           <td><?php echo $acust['Applicant_Name'];?></td> 
           <td><?php echo $acust['Activity'];?></td> 
           <td><?php echo $acust['Mobile_No'];?></td> 
           <td><?php echo $acust['Service_Category'];?></td> 
           <td><?php echo $acust['Purpos_Supply'];?></td> 
           <td><?php echo $acust['House_No'];?></td> 
            <td><a class="btn btn-info" href="estimater.php?ID=<?php echo $acust['Application_ID'];?>"><i class="glyphicon glyphicon-pencil"></i> Estimate</a> | <a class="btn btn-default" href="assignesti.php?ID=<?php echo $acust['Application_ID'];?>"><i class="glyphicon glyphicon-user"></i> Assign</a>              </td> 
          </tr><?php }?> 
          </tbody> 
         </table></form> 
        </div> 

回答

0

這就是我所做的,如果你需要我解釋它只是留下評論。它只是在btn按下時檢查所有檢查過的輸入,然後它發出ajax請求,並更新數據庫中的值。我做到了,所以如果你檢查exmaple第四個輸入,它不會再更新它,直到你刷新頁面。

`<html> 
<head> 

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script> 
    <script> 
    var arr=[]; 

function send() 
{ 
var neww=[]; 
    //get all of the inputs that are checked 
$("input[type=checkbox]").each(function (input){ 


    if ($(this).is(":checked")==true) 
    { 
    if (arr.indexOf($(this).attr('value'))<0) 
    { 
     arr.push($(this).attr('value')); 
     neww.push($(this).attr('value')); 
    } 
    } 


}) 
var string = JSON.stringify(neww); 
$.ajax({ 
type:"GET", 
url:"up.php", 
dataType:"json", 
data:{"values": string}, 
success: function (res) 
{ 
    //do what u want when this complete 
} 
}) 
} 
    </script> 
</head> 
<body> 


<input type="checkbox" id="check1" value="1">1</input><br> 
<input type="checkbox" id="check2" value="2">2</input><br> 
<input type="checkbox" id="check3" value="3">3</input><br> 
<input type="checkbox" id="check4" value="4">4</input><br> 

<input type="button" onclick="send()" value="Click me"></input> 

</body> 
</html> 
` 

和up.php是在這裏:

<?php 
$yes = array(); 
$string=json_decode($_GET["values"]); 

//db connect 
$con = new mysqli("localhost","yourusername","yourpass","yourdb"); 

foreach ($string as $val) { 

$sql="INSERT INTO yourtabel Values ('',{$val})"; 
$con->query($sql); 
} 
//echo json_encode("bv"); 
?> 
相關問題