Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/42.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在php中动态填充复选框?_Php_Checkbox - Fatal编程技术网

如何在php中动态填充复选框?

如何在php中动态填充复选框?,php,checkbox,Php,Checkbox,我想动态填充复选框,从数据库中获取其值并存储在复选框中,但不会显示值的显示方式 这是我的密码: <div> <? //echo $eventid=$_POST['events']; $count=count($_POST['events']); for($i=0; $i<$count; $i++){ $select="select b.first_name,b.last_name from buyers b,registra

我想动态填充复选框,从数据库中获取其值并存储在复选框中,但不会显示值的显示方式

这是我的密码:

<div>
    <?
    //echo $eventid=$_POST['events'];
    $count=count($_POST['events']);
    for($i=0; $i<$count; $i++){
        $select="select b.first_name,b.last_name from buyers b,registrations r where b.buyer_id=r.buyer_id and r.event_id='".$_POST['events'][$i]."' group by r.buyer_id";
        $res = $GLOBALS ['mysqli']->query ($select) or die ($GLOBALS ['mysqli']->error . __LINE__);
        if ($res->num_rows > 0)
        {           
            while($row = $res->fetch_assoc ())
            {
            ?>  
                <input type="checkbox" name="receptionts" checked="checked" value="<? echo $row['first_name'];$row['last_name']?>"/><br />              
            <?
            }
        }
    }
    ?>
</div>

像这样给予价值

    <input type="checkbox" name="receptionts" checked="checked" value="<?php echo $row['first_name'].' '.$row['last_name'];?>"/><br />  

复选框的值属性应为

value=”“

试试这个

  <input type="checkbox" name="receptionts" checked="checked" value="<?php echo $row['first_name']." ".$row['last_name']?>"/><br />  

在您的评论中(您希望将名字设置为值):

改变

<input type="checkbox" name="receptionts" checked="checked" value="<? echo $row['first_name'];$row['last_name']?>"/><br />
(可能没有必要)

另外,请查看如何防止SQL注入。

尝试以下方法:

<div>
    <?php /* <-- Don't use PHP short tags, it's a bad practice, they are deprecated */

        /* Making select statement inside a loop is a very bad idea. 
           Also, you should make sure to properly escape
           $_POST['events']), as inserting unescaped inputs into a query 
           is very unsafe   */

        $post = array_map(
            array($GLOBALS['mysqli'], "escape_string"), 
            $_POST['events']
        );

         /* We apply mysqli::escape_string to every element of 
            $_POST['events'] and save result as $post */

        $select = "SELECT `b`.`first_name`, `b`.`last_name` FROM `buyers` `b` ".
                  "JOIN `registrations`.`r` ON `r`.`buyer_id` = `b`.`buyer_id` ".
                  "WHERE `r`.`event_id` IN ('".implode("','", $post)."') ".
                  "GROUP BY `r`.`buyer_id`";
        $res = $GLOBALS ['mysqli']->query ($select) 
               or die ($GLOBALS ['mysqli']->error . __LINE__);

        while($row = $res->fetch_assoc()) : ?>
           <label>
               <input type="checkbox" name="receptionts[]" checked="checked" value="<?= $row['first_name']." ".$row['last_name']?>"/> 
               <?= $row['first_name']." ".$row['last_name'] ?>
           </label>
        <?php endwhile; ?>

</div>


value=“”/-您想要什么格式?如果没有错误,则回显只显示$row['first_name']我想用$row['first_name']值显示复选框我使用了您的代码,但只选中了chekbox显示的值,而没有用复选框显示
$select="select b.first_name,b.last_name from buyers b,registrations r where b.buyer_id=r.buyer_id and r.event_id='".$_POST['events'][$i]."' group by r.buyer_id";
<input type="checkbox" name="receptionts" checked="checked" value="<? echo $row['first_name'];$row['last_name']?>"/><br />
<input type="checkbox" name="receptionts" checked="checked" value="<?php echo $row['first_name'];?>"/><br />
$select="select b.first_name AS first_name,b.last_name AS last_name from buyers b,registrations r where b.buyer_id=r.buyer_id and r.event_id='".$_POST['events'][$i]."' group by r.buyer_id";
<div>
    <?php /* <-- Don't use PHP short tags, it's a bad practice, they are deprecated */

        /* Making select statement inside a loop is a very bad idea. 
           Also, you should make sure to properly escape
           $_POST['events']), as inserting unescaped inputs into a query 
           is very unsafe   */

        $post = array_map(
            array($GLOBALS['mysqli'], "escape_string"), 
            $_POST['events']
        );

         /* We apply mysqli::escape_string to every element of 
            $_POST['events'] and save result as $post */

        $select = "SELECT `b`.`first_name`, `b`.`last_name` FROM `buyers` `b` ".
                  "JOIN `registrations`.`r` ON `r`.`buyer_id` = `b`.`buyer_id` ".
                  "WHERE `r`.`event_id` IN ('".implode("','", $post)."') ".
                  "GROUP BY `r`.`buyer_id`";
        $res = $GLOBALS ['mysqli']->query ($select) 
               or die ($GLOBALS ['mysqli']->error . __LINE__);

        while($row = $res->fetch_assoc()) : ?>
           <label>
               <input type="checkbox" name="receptionts[]" checked="checked" value="<?= $row['first_name']." ".$row['last_name']?>"/> 
               <?= $row['first_name']." ".$row['last_name'] ?>
           </label>
        <?php endwhile; ?>

</div>