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

时间:2017-07-26 09:57:31

标签: php pdo

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

input{    file
{
    path => ["C:/input.json"]
    start_position => "beginning"
    sincedb_path => "/dev/null"
}}filter{
mutate
{
    replace => [ "message", "%{message}" ]
    gsub => [ 'message','\n','']
}
json { source => message }
mutate{
    remove_field => ["message"]
}}output {
   elasticsearch {
            hosts => ["localhost:9200"]
            index => "index"
            document_type => "type"
            document_id => "%{id}"
   }
   stdout { codec => rubydebug }
}
在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>

2 个答案:

答案 0 :(得分: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'];?>

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

答案 1 :(得分: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
}