2017-07-26 184 views
0

我需要使用在以下OtherEventPayment.php需要通過選擇複選框

<form action="OtherEventPayment.php" id="frmSignIn" method="post"> 
    <input type="hidden" name="online_id" value="<?php echo $lid; ?>" >   
    <table class="table"> 
    <thead> 
     <tr> 
      <th>#</th> 
      <th>Item</th> 
      <th>No. of Participants</th> 
      <th>Tick the Items</th> 
     </tr> 
     </thead> 
    <tbody> 

    <tbody> 

<?php 
$sn ="1"; 
$id = $oth_event_id; 
$stmt1 = $DB_con->prepare('SELECT * FROM oth_events_details 
          LEFT JOIN oth_event_category ON (oth_events_details.oth_evcat_id=oth_event_category.oth_evcat_id) 
          WHERE oth_event_id =:uid ORDER BY oth_event_det_id DESC'); 
$stmt1->execute(array(':uid'=>$id)); 
$stmt1->execute(); 
if($stmt1->rowCount() > 0) 
{ 
    while($row1=$stmt1->fetch(PDO::FETCH_ASSOC)) 

    { 

     extract($row1); 
     ?> 
     <tr> 
     <td><?php echo $sn; ?></td> 
     <td> 

     <?php echo $row1['oth_category'];?> - 
     <?php 

      $group =$row1['oth_catgroup_type']; 
        if ($group=="S") 
        { 
         echo "Single"; 
        } 
        elseif ($group=="D") 
        { 
         echo "Doubles"; 
        } 
        else{ 

         echo "Group"; 

        } 


     ?> 

     </td> 

     <td><?php echo $row1['participntno']; ?></td> 

     <td> 
     <b> 



     </b> 

     <input type="checkbox" name="chk[<?php echo $row1['oth_event_det_id'];?>]" value="<?php echo $row1['oth_event_det_id'];?>" id="chk[<?php echo $row1['oth_event_det_id'];?>]" /> 
     &nbsp;&nbsp;&nbsp;Fees:-&nbsp;&nbsp;<?php echo $row1['oth_ev_fee'];?> 

     </td> 




     </tr> 
     <?php $sn++; ?> 

     <?php 
    } 
    } 
    else 
    { 
    ?> 
    <div class="col-xs-12"> 
     <div class="alert alert-warning"> 
      <span class="glyphicon glyphicon-info-sign"></span> &nbsp; No Data Found ... 
     </div> 
    </div> 
    <?php 
    } 

    ?> 

    </tbody> 


    </table>   
    <div class="col-md-6"> 
    <input type="submit" name="selectItems" value="Submit & Proceed" class="btn btn-primary pull-right mb-xl" data-loading-text="Loading..."> 
    </div> 
    </div> 
    <?php echo $sn1=$sn-1; ?> 
    </form> 

給出的形式我寫的代碼中插入數據到數據庫插入多個數據。但不工作。如何正確地插入數據到DB

<?php 
require_once 'dbconfig.php'; 



if(isset($_POST['selectItems'])) 
{ 

echo array[] = $_POST['chk[]']; 

    echo $oth_online_id= $_POST['online_id']; 

    if($oth_event_detid != ""){ 
    for($i=0;$i<sizeof($oth_event_detid);$i++) 
    { 
// oth_event_det_id,oth_online_id 

    $stmt = $DB_con->prepare('INSERT INTO othevents_itemsonline(oth_event_det_id,oth_online_id) VALUES(:oth_event_det_id, :oth_online_id)'); 
    $stmt->bindParam(':oth_event_det_id',$oth_event_det_id); 
    $stmt->bindParam(':oth_online_id',$oth_online_id); 


    if($stmt->execute()) 
      { 
       $lastonlineid= $DB_con->lastInsertId(); 

       $successMSG = "Thank you For Registering with us . Please select the items to be participating..."; 

       // header("refresh:0;OtherEventsOnlineRegistrationThankyou.php");/
      } 
      else 
      { 
       $errMSG = "error while registering...."; 
      } } } 



    } 

?> 
+0

請正確格式化您的代碼,這是一團糟。如果你不清理你的代碼,很難爲你提供幫助。 – user1438038

回答

1

只要改變你的價值複選框和值,它代表,但保留名稱相同與他人,但它應該有[]

<input type="checkbox" id="chk<?php echo $row1['oth_event_det_id'];?>" name="chk[]" value="<?php echo $row1['oth_event_det_id'];?>"> 
<label for="chk<?php echo $row1['oth_event_det_id'];?>"><?php echo $row1['oth_event_det_id'];?></label> 
名稱

有一個名字CHK []這樣會在你的GET或POST函數發送,並作爲數組,以便循環它控制器或功能將在DB

在插入它添加它,

$data = $_GET['chk']; //this is in array form 
foreach($data as $chk){ 
//insert code here 
} 
+0

它顯示錯誤 – MMS

+0

它顯示錯誤注意:第11行中的C:\ Program Files \ EasyPHP-12.1 \ www \ NEWSICASA \ OtherEventPayment.php中的未定義索引:chk [] 致命錯誤:未捕獲異常'PDOException' 'C:\ Program Files \ EasyPHP-12.1 \ www \ NEWSICASA \ OtherEventPayment.php:24堆棧跟蹤:#0 C:\ Program Files錯誤消息'SQLSTATE [23000]:完整性約束違規:1048第24行中的Files \ EasyPHP-12.1 \ www \ NEWSICASA \ OtherEventPayment.php(24):PDOStatement-> execute()#1 {main}拋出C:\ Program Files \ EasyPHP-12.1 \ www \ NEWSICASA \ OtherEventPayment.php – MMS

2

輸入字段的名稱應該相同。使用以下代碼:

<input type="checkbox" name="chk[]" value="<?php echo $row1['oth_event_det_id'];?>" id="chk[<?php echo $row1['oth_event_det_id'];?>]" /> 
    &nbsp;&nbsp;&nbsp;Fees:-&nbsp;&nbsp;<?php echo $row1['oth_ev_fee'];?> 

您可以看到名稱。希望它足夠清晰