|
Posted by Rik on 08/17/07 14:58
On Fri, 17 Aug 2007 16:25:36 +0200, <chimambo@googlemail.com> wrote:
> Hi All,
> I have a little problem. I am retrieving records from a table and I
> want to update the records using checkboxes. I am able to display the
> database record quite alright and I have created an array of
> checkboxes, but my update loop is not working. Here is my code:
> /*---------This retrieves the records -------------*/
> if($row_md)
> {
> do{
> echo "<tr><td text align=3D'right'>";
> echo date('d-m-Y', strtotime($row_md['med_date']));
> echo "</td>";
> echo "<td text align=3D'left'>";
> echo $row_md['drug_desc'];
> echo "</td>";
> echo "<td text align=3D'left'>";
> echo $row_md['dose'];
> echo "</td>";
> echo "<td>";
> echo "<input type=3D'checkbox' name=3Dstop[] value=3D'N'>";
(assuming a post)
echo "<input type=3D'checkbox' name=3D'stop[]' value=3D'{$row['h_number'=
]}'>";
(or whatever the primary key is called)
> echo "</td></tr>";
> }
> while($row_md =3D mysql_fetch_assoc($md));
> /*
> ----------------------------------------------------------------------=
-------------------------
> */
> /*--------------This should update the records but its not working
> ----------*/
>
> while (list ($key,$val) =3D @each ($stop))
> {
> $mysql_query=3D"UPDATE irdb_temp_medication
> SET active =3D '$val'
> WHERE h_number =3D '$hos_no')";
> }
Assumptions:
1. Form is posted.
2. Primary key is h_number
3. h_number is an integer
4. You're trying to set `active` to 'N' (=3Dstop), else it would be 'Y' =
=
(=3Dstart).
if(isset($_POST['stop'])){
$off =3D array_map('intval',$_POST['stop']);
$string =3D implode(',',$off);
mysql_query("UPDATE irdb_temp_medication
SET active =3D 'N'
WHERE `h_number` IN ({$string})");
}
(If not-selected checkboxes should all be set to Y also:)
if(isset($_POST['stop']) && is_array($_POST['stop'])){
$off =3D array_map('intval',$_POST['stop']);
$string =3D implode(',',$off);
mysql_query("UPDATE irdb_temp_medication
SET `active` =3D IF(`h_number` IN ({$string}),'N','Y')");
}
-- =
Rik Wasmus
Navigation:
[Reply to this message]
|