vendredi 29 janvier 2016

Insert array where checkboxes are checked

Being new to PHP and MySQL, I've picked up quite a few things reading here. This post is about the closest question I can find relative to my issue. I borrowed some from the solution but it's only gotten me so far.

I populate a form with table data from a SELECT statement, currently 15 rows. Each row from the result has a checkbox and corresponding data. There's also a textarea in the form.

I need to insert the text input from the textarea along with the Case_No and Client_No associated with the checkbox, into a different table...only where the checkbox is checked. Only the text will be same for each row. None of the unchecked checkboxes should be included in the INSERT.

I realize the answer is probably some sort of loop, but I don't have much experience with inserting from loops, only displaying data from loops.

Here is my form:

<form method="post" name="group_note" action="group_test.php?Counselor=<?php echo $_GET['Counselor'];?>"/>
<table border="1" width="650" cellpadding="5" cellspacing="0" bordercolor="#999999">
<tr><th>Group Note</th></tr>
<tr><td><textarea name="Note" rows="10" cols="115"></textarea></td></tr>
<tr><th align="left">Select clients below to receive this group note &nbsp; <input type="submit" value="Add Group Note"></th></tr>
</table>
<table border="1" cellpadding="5" cellspacing="0" bordercolor="#999999">
<thead>
  <tr>
    <th bgcolor="#009999">Select</th>
    <th bgcolor="#009999">Name</th>
    <th bgcolor="#009999">Case No.</th>
    <th bgcolor="#009999">Client No.</th>
    </tr>
  </thead>
  <tbody>
<?php foreach ($dbh -> query($sql) as $row) { ?>        
<tr> 
        <td><input type="checkbox" name="groupselect[]" value="<?php echo $row['Case_No'];?>" /></td>
        <td><?php echo $row['FirstName']. " " . $row['LastName']; ?></td>
        <td><?php echo $row['Case_No']; ?></td> 
        <td><?php echo $row['Client_No']; ?><input type="hidden" name="Client_No" value="<?php echo $row['Client_No']; ?>"/></td>

<?php  }  ?>
</tbody>
</table>
    <input type="hidden" name="MM_insert" value="group_note">
</form>

And the Insert statement:

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "group_note")) {
try
    {
        $dbh = new PDO("mysql:host=$mysql_hostname;dbname=$mysql_database; charset=utf8", $mysql_username, $mysql_password);
        $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $insertGroup = array();
        foreach ($_POST['groupselect'] as $i => $value) {
        $GroupNote_CaseNo=$_POST['groupselect'][$i];
        $GroupNote_ClientID=$_POST['Client_No'][$i];
        $GroupNote=$_POST['Note'];
        $insertGroup[]="(".$GroupNote_CaseNo.", ".$GroupNote_ClientID.", ".$GroupNote.")";
        $Note = "INSERT INTO group_note GroupNote_CaseNo, GroupNote_ClientID, GroupNote VALUES ". implode(", ", $insertGroup). "GroupNote_CaseNo=VALUES(GroupNote_CaseNo), GroupNote_ClientID= VALUES(GroupNote_ClientID), GroupNote=VALUES(GroupNote)";
        $stmt->execute();
        $stmt->closeCursor();       
    }
    }
    catch (Exception $e)
    {
    $message = 'Something not working right!!';
    }
}

When I select two or three checkboxes and insert some text, on submit I get this result from var_dump($_POST['groupselect'])

array(3) { [0]=> string(22) "(1518-R, 4, This note)" 1=> string(22) "(1534-R, 3, This note)" [2]=> string(22) "(4062-R, 3, This note)" }

The ClientNo is only showing one digit, and they are three digits long. If I remove the [$i] following $GroupNote_ClientID=$_POST['Client_No'], I'll get all three digits, but only from the first checkbox row selected: array(3) { [0]=> string(22) "(1518-R, 4, This note)" 1=> string(22) "(1534-R, 3, This note)" [2]=> string(22) "(4062-R, 3, This note)" }

In any case, nothing is getting inserted into the table. Depending on the variable syntax, the PHP error log shows "Undefined variable: insertGroup" and/or "Undefined variable: insertGroup".

Also, I'm aware of the dangers of $_POST variables. My database is an internal-use only, on our intranet. No outside access.




Aucun commentaire:

Enregistrer un commentaire