php – Foreach仅在选中时才选中第一个复选框

前端之家收集整理的这篇文章主要介绍了php – Foreach仅在选中时才选中第一个复选框前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在处理以下代码,以便从表单中选择复选框.如果我检查第一个复选框,一切都很好.如果我选中另一个复选框,则在发送批量复制表单时会出现“未定义索引”错误.请记住,我正在使用post方法获取复选框,并且由于表单位置和字段的复杂性,提交按钮位于复选框上方.我需要的是选择多个复选框并将某些值添加数据库.
<?PHP //bulkcopy.PHP

  session_start();
  if($_SESSION['admin_logged_in'] != true){
    header("Location:login.html");
    exit();
  }
  include 'db.PHP';

  $from = MysqL_real_escape_string($_GET['from']);
  $room = MysqL_real_escape_string($_POST['room']);

  if(!empty($_POST['id'])) {
    foreach($_POST['id'] as $check) {
      $id = $check;

      $sel = MysqL_query("select * from $from where id = '$id' limit 1 ") or die(MysqL_error());

      while($row = MysqL_fetch_array($sel)){
        $preview = $row['preview'];
        $text = $row['text'];
        $title = $row['title'];
        $images = $row['images'];
      }

      $ins = MysqL_query("insert into $room (id,preview,text,title,images) values (' ','$preview','$text','$title','$images') ") or die(MysqL_error());

    }

    header("Location:admin.PHP");
  }

?>

表格的代码可以在下面找到:

<form class="form-inline" name="bulkcopy" method="post" action="bulkcopy.PHP?from=sights"> <b>Bulk Copy:</b> 
    <select name='room' class="form-control">
        <option>Select...</option>
        <option value="Orhan">Orhan</option>
        <option value="Deniz">Deniz</option>
        <option value="Irini">Irini</option>
        <option value="Katina">Katina</option>
        <option value="Gulbin">Gulbin</option>
        <option value="Mihalis">Mihalis</option>
    </select>
    <input class="btn btn-primary" type="submit" name="submit" value="Go"><br /><br />
</div>
<table class="table table-bordered table-striped">
    <th>Entry Name</th>
    <th>Display Order</th>
    <th>Copy to...</th>
    <th>Status</th>
    <th>Image</th>
    <th>Edit</th>
    <th>Delete</th>
    <th>Duplicate</th>

    <?PHP while($row = MysqL_fetch_array($sel)) { ?>
    <tr>
        <td>
            <input type="checkBox" name="id[]" value="<?PHP echo $row['id']; ?>">
            </form>
            <?PHP echo $row['title']; ?>
        </td>
        <td>
            <form name="order" method="post" action="sightorder.PHP?id=<?PHP echo htmlspecialchars($row['id']); ?>">
                <div class="col-md-4">
                    <input class="form-control" type="number" name="order" value="<?PHP echo htmlspecialchars($row['ordernum']); ?>">
                </div>
                <div class="col-sm-3">
                    <input type="submit" name="submit" value="Set Order" class="btn btn-primary">
                </div>
            </form>
        </td>
        <td>

            <form name="copyto" method="post" action="copyto.PHP?from=sights&id=<?PHP echo htmlspecialchars($row['id']); ?>">
                <input type="checkBox" name="room[]" value="Orhan"> O -
                <input type="checkBox" name="room[]" value="Deniz"> D -
                <input type="checkBox" name="room[]" value="Irini"> I -
                <input type="checkBox" name="room[]" value="Katina"> K -
                <input type="checkBox" name="room[]" value="Gulbin"> G -
                <input type="checkBox" name="room[]" value="Mihalis"> M 
                <input type="submit" name="submit" value="Copy" class="btn btn-primary">
            </form>

        </td>
        <td>
            <a href="sightstatus.PHP?id=<?PHP echo htmlspecialchars($row['id']); ?>&status=<?PHP echo $row['status']; ?>"><?PHP if($row['status'] == 1){ ?><i class="fa fa-check fa-lg"></i><?PHP }else{ ?><i class="fa fa-times fa-lg"></i><?PHP } ?></a>
        </td>
        <td>
            <a href="sightimages.PHP?id=<?PHP echo $row['id']; ?>"><i class="fa fa-image fa-lg"></i></a>
        </td>
        <td>
            <a href="editsight.PHP?id=<?PHP echo htmlspecialchars($row['id']); ?>"><i class="fa fa-edit fa-lg"></i></a>
        </td>
        <td>
            <a onclick="return confirmDelete()" href="delsight.PHP?id=<?PHP echo htmlspecialchars($row['id']); ?>"><i class="fa fa-trash fa-lg"></i></a>
        </td>
        <td>
            <a href="duplicatesight.PHP?id=<?PHP echo htmlspecialchars($row['id']); ?>"><i class="fa fa-copy fa-lg"></i></a>
        </td>
    </tr>
    <?PHP } ?>
</table>

任何帮助将不胜感激.
谢谢.

@H_502_13@ 你这里有问题
<?PHP
    while($row = MysqL_fetch_array($sel)){ ?>
        <tr><td><input type="checkBox" name="id[]" value="<?PHP echo $row['id']; ?>"> <?PHP echo $row['title']; ?></td></form>

while循环没有关闭括号,并且在添加第一个复选框后关闭表单.因此,如果未选中该复选框,则不会发布输入,因此未定义索引.确保在添加完所有行之后才关闭表单,如下所示

<?PHP
    while($row = MysqL_fetch_array($sel)){ ?>
        <tr><td><input type="checkBox" name="id[]" value="<?PHP echo $row['id']; ?>"> <?PHP echo $row['title']; ?></td></tr>
<?PHP } ?>
  </table>
</form>

猜你在找的PHP相关文章