INSERT multiple rows using one button

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jonnyfortis
Forum Contributor
Posts: 462
Joined: Tue Jan 10, 2012 6:05 am

INSERT multiple rows using one button

Post 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>
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: INSERT multiple rows using one button

Post 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?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: INSERT multiple rows using one button

Post 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>
(#10850)
jonnyfortis
Forum Contributor
Posts: 462
Joined: Tue Jan 10, 2012 6:05 am

Re: INSERT multiple rows using one button

Post 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
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: INSERT multiple rows using one button

Post by Celauran »

My point was that you've got no mechanism for keeping track of the individual users.
jonnyfortis
Forum Contributor
Posts: 462
Joined: Tue Jan 10, 2012 6:05 am

Re: INSERT multiple rows using one button

Post 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?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: INSERT multiple rows using one button

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: INSERT multiple rows using one button

Post 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.
(#10850)
Post Reply