Reply to Re: Updating Multiple Rolls Using PHP

Your name:

Reply:


Posted by Jerry Stuckle on 08/23/07 15:00

chimambo@googlemail.com wrote:
> On 23 Aug, 10:07, chima...@googlemail.com wrote:
>> On 17 Aug, 15:58, Rik <luiheidsgoe...@hotmail.com> wrote:
>>
>>
>>
>>
>>
>>> On Fri, 17 Aug 2007 16:25:36 +0200, <chima...@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='right'>";
>>>> echo date('d-m-Y', strtotime($row_md['med_date']));
>>>> echo "</td>";
>>>> echo "<td text align='left'>";
>>>> echo $row_md['drug_desc'];
>>>> echo "</td>";
>>>> echo "<td text align='left'>";
>>>> echo $row_md['dose'];
>>>> echo "</td>";
>>>> echo "<td>";
>>>> echo "<input type='checkbox' name=stop[] value='N'>";
>>> (assuming a post)
>>> echo "<input type='checkbox' name='stop[]' value='{$row['h_number']}'>";
>>> (or whatever the primary key is called)
>>>> echo "</td></tr>";
>>>> }
>>>> while($row_md = mysql_fetch_assoc($md));
>>>> /*
>>>> ---------------------------------------------------------------------------­­--------------------
>>>> */
>>>> /*--------------This should update the records but its not working
>>>> ----------*/
>>>> while (list ($key,$val) = @each ($stop))
>>>> {
>>>> $mysql_query="UPDATE irdb_temp_medication
>>>> SET active = '$val'
>>>> WHERE h_number = '$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' (=stop), else it would be 'Y'
>>> (=start).
>>> if(isset($_POST['stop'])){
>>> $off = array_map('intval',$_POST['stop']);
>>> $string = implode(',',$off);
>>> mysql_query("UPDATE irdb_temp_medication
>>> SET active = '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 = array_map('intval',$_POST['stop']);
>>> $string = implode(',',$off);
>>> mysql_query("UPDATE irdb_temp_medication
>>> SET `active` = IF(`h_number` IN ({$string}),'N','Y')");}
>>> --
>>> Rik Wasmus- Hide quoted text -
>>> - Show quoted text -
>> Thanks but this code does not retrieve the records for updating. How
>> do I proceed?- Hide quoted text -
>>
>> - Show quoted text -
>
> Ok, its not giving me any error, but its not updating either.
>

Your problem is right here:

echo "<input type='checkbox' name=stop[] value='N'>";

If any of the checkboxes are checked, you will get a zero-based index
array $_POST['stop']. This array will contain one element for each
checked box, and the contents of each element will be 'N' (the value=
parameter). Unchecked boxes will not be in the array.

I don't think this is what you want - there's no way to tell which boxes
are checked, since they all have the same name and the same value.

Probably you want something like:

echo
"<input type='checkbox' name=stop[] value='".$row_md['hos_no']."'>";

(sorry for the wrap).

This will put your 'hos_no' in the $_POST['stop'] array for each checked
box.

Now you can update it with:

for each ($stop as $s)
{
$mysql_query="UPDATE irdb_temp_medication
SET active = 'N'
WHERE h_number = '$s')";
}

Or something similar. It's hard to tell from just partial code and no
database definitions.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация