0

PHP novice here. Goal is to:

a) Display a table of booking options (fitness classes) dynamically from database records, allow user to select multiple options with checkboxes against each row - this bit's working.

b) Pass the checkbox selection to a table listing the selected data on a confirmation page. I'm getting an error here: Invalid argument supplied for foreach().

c) Update the database when user hits the second page's 'Confirm' button.

Research so far uncovered this advice on using $_GET and $_POST to achieve this with an array.

My checkbox code on the initial page:

    echo '<form action="makebooking.php" method="get">';
    echo '<td><input type="checkbox" name="class_id[]" value=' . $row['class_id'] . '</td></tr>';

The foreach statement error comes from this code that generates the table of choices on the second page:

    //Check if the GET is set from classes.php

    if (isset($_GET['class_id'])) {
    // Grab the score data from the GET
    foreach($_GET['class_id'] as $class_id) {
      $_GET['class_id'] = $class_id;
        }

    }
    //table header
    echo '<table class="table table-bordered table-hover">';
    echo '<thead><tr><th>Date</th><th>Time</th><th>Venue</th><th>Who\'s going?</th>                                            <th>Add someone</th></tr></thead>';

      //create the form
      echo '<form method="post" action="' . $_SERVER['PHP_SELF'] . '">';

      //Get the class IDs from the GET to use in the POST
      foreach ($_GET['class_id'] as $class_id) {
      $sql = "SELECT class_id, DATE_FORMAT(date, '%a, %d %b') AS new_date,           DATE_FORMAT(time, '%H:%i') AS new_time, venue FROM classes WHERE class_id = '$class_id'";
      $data = mysqli_query($dbc, $sql);

      //---------------------------------------------------------------------------------
      //get table data
          while ($row = mysqli_fetch_array($data)) {
            $date = $row["new_date"];
            $time = $row["new_time"];
            $venue = $row["venue"];
            $class_id = $row["class_id"];
          }
//---------------------------------------------------------------------------------
      //Show a table of the selected classes
          echo '<input type="hidden" name="id" value= ' . $class_id . ' />';
            echo '<td>' . $date . '</td>';
            echo '<td>' . $time . '</td>';
            echo '<td>' . $venue . '</td>';
            echo '<td>' . $username . '</td>';
            echo '<td><button class="btn btn-mini" type="button"><i class="icon-user"></i><i class="icon-plus"</i></button></td></tr>';
      }
       echo'</table>';
          // Make booking button
            echo '<input type="submit" name="submit" class="btn btn-large btn-primary pull-right" value="Confirm">';
            echo '</form>';
        }

Full code of both pages at this pastebin. All error-fixing advice gratefully accepted!

4

1 回答 1

0

发现我一直在 if/else 循环中声明变量,这使得代码的其他部分无法访问它们。

我还在两个表中添加了一些额外的隐藏输入数组来验证输入。这需要将类 ID 进行字符串到整数的转换,以便使用所选数据更新数据库。

完整的固定代码适用于任何遇到类似问题的人。

于 2013-07-23T12:26:06.230 回答