需要通过选中复选框插入多个数据

  • 本文关键字:插入 数据 复选框 php pdo
  • 更新时间 :
  • 英文 :


我需要使用下面给出的表格将数据插入数据库

<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>

在OtherEventPayment中.php我已经编写了代码。但不起作用.如何将数据正确插入数据库

<?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....";
} } }

}

?>

输入字段的名称应相同。使用以下代码:

<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'];?>

你可以看到名字。希望它足够清楚

只需更改复选框的值及其表示的值,但保持名称与其他名称相同,但它的名称应带有 []

<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 函数中的数组,因此在控制器或函数上循环它,将其添加到数据库中

插入后,

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

最新更新