Page 1 of 1

INSERT multiple rows using one button

Posted: Mon Apr 04, 2016 5:59 pm
by jonnyfortis
i have a checks in form to check people in to an office it is currently a basic insert form but i want to be able to just tick the relevant check boxes for who has attended then confirm all at once rather than doing them individually by seperate buttons

Code: Select all

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  $insertSQL = sprintf("INSERT INTO sd_attendance (attendance_date, attendance_cust_id, attendance) VALUES (%s, %s, %s)",
                       GetSQLValueString($_POST['attendance_date'], "date"),
                       GetSQLValueString($_POST['attendance_cust_id'], "int"),
                       GetSQLValueString(isset($_POST['attendance']) ? "true" : "", "defined","1","0"));

  mysql_select_db($database_sdma, $sdma);
  $Result1 = mysql_query($insertSQL, $sdma) or die(mysql_error());

  $insertGoTo = "register-list.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));
}
the form looks like this

Code: Select all

<form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1">
          <input type="checkbox" name="attendance" value="" />
          <input type="hidden" name="attendance_date" value="<?php echo date("Y-m-d");?>" />
          <input type="hidden" name="attendance_cust_id" value="<?php echo $row_rsLesson['custID']; ?>" />
          <input type="hidden" name="MM_insert" value="form1" />
          <input type="submit" value="CONFIRM" />
        </form>

Re: INSERT multiple rows using one button

Posted: Mon Apr 04, 2016 6:19 pm
by Celauran
You've got a single checkbox with no value assigned to it. How is this meant to work? Display a list of everyone with a checkbox next to each name?

Re: INSERT multiple rows using one button

Posted: Mon Apr 04, 2016 10:14 pm
by Christopher
You probably want to get an array back that has values for each User ID. And set attendance to 1 if checked.

Code: Select all

<form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1">
<?php
foreach ($people as $person) {
?>
          <div>
          <?php echo $person['first_name'] . " " . $person['first_name']; ?>
          <input type="checkbox" name="attendance[<?php echo $person['id']; ?>]" value="1" />
          <input type="hidden" name="attendance_date[<?php echo $person['id']; ?>]" value="<?php echo date("Y-m-d");?>" />
          <input type="hidden" name="attendance_cust_id[<?php echo $person['id']; ?>]" value="<?php echo $row_rsLesson['custID']; ?>" />
          </div>
<?php
}
?>
          <input type="hidden" name="MM_insert" value="form1" />
          <input type="submit" value="CONFIRM" />
        </form>

Re: INSERT multiple rows using one button

Posted: Tue Apr 05, 2016 7:00 am
by jonnyfortis
Celauran wrote:You've got a single checkbox with no value assigned to it. How is this meant to work? Display a list of everyone with a checkbox next to each name?
sorry that should have had a ''1'' in the value

Re: INSERT multiple rows using one button

Posted: Tue Apr 05, 2016 7:05 am
by Celauran
My point was that you've got no mechanism for keeping track of the individual users.

Re: INSERT multiple rows using one button

Posted: Tue Apr 05, 2016 7:09 am
by jonnyfortis
Celauran wrote:My point was that you've got no mechanism for keeping track of the individual users.
the <input type="hidden" name="attendance_cust_id" value="<?php echo $row_rsLesson['custID']; ?>" /> will keep track of individual users?

Re: INSERT multiple rows using one button

Posted: Tue Apr 05, 2016 7:43 am
by Celauran
Not really. That's going to contain a single value.
but i want to be able to just tick the relevant check boxes for who has attended then confirm all at once rather than doing them individually by seperate buttons
You need to store the IDs in an array for this to work.

Re: INSERT multiple rows using one button

Posted: Tue Apr 05, 2016 11:47 am
by Christopher
jonnyfortis wrote:the <input type="hidden" name="attendance_cust_id" value="<?php echo $row_rsLesson['custID']; ?>" /> will keep track of individual users?
If $row_rsLesson['custID'] identifies the person, then modify the code I posted above to change $person['id'] to $row_rsLesson['custID']. Then you can get rid of input attendance_cust_id because it will be the array key in the array sent.