2017-07-26 96 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'];?> 

您可以看到名称。希望它足够清晰