Re: Checking radio buttons for multiple records/Updating multiple records...

From: Date: Sun, 29 Jul 2001 16:41:55 +0000
Subject: Re: Checking radio buttons for multiple records/Updating multiple records...
References: 1  Groups: php.db 
Request: Send a blank email to php-db+get-10875@lists.php.net to get a copy of this message
"Olinux" <olnx@yahoo.com> wrote in message news:HNEALCOEACFDPGGFLEAIIEFGCAAA.olnx@yahoo.com... > Simplified: I have a tabble of users with an AUTHORIZE field > [type ENUM - Yes or No]. They sign up and once I authorize > them. I pull up all of the records and display them something > like this: =========== authorize.php ============= <?php // log into database, etc $query = "SELECT id, username, authorized FROM " ."users ORDER BY signupdate DESC"; $result = mysql_query($query); echo "\n<form method='post' action='updater.php'>"; while ($row = mysql_fetch_array($result)) { echo "\n\t<br>" .$row[username] ." - Authorize - " ."<input " ."type='radio' " ."name='authorize[{$row[id]}]' " ."value='Y' " .($row[authorized] == 'Y' : "checked" : "") .">" ."Yes " ."<input " ."type='radio' " ."name='authorize[{$row[id]}]' " ."value='N' " .($row[authorized] == 'N' : "checked" : "") .">" ."No" ."<input type='hidden' name='oldauthorize[{$row[id]}]' " ."value='{$row[authorized]}'>" ; } echo "\n</form>"; ?> =========== end ============= > This works EXCEPT only the last field displays a checked radio button > [because the names are all the same. Voila - HTML is happy because the names are different, and PHP will automatically parse the returned data into an array so it's easy to work with. I use an id field - presumable an integer number - so we don't run into odd characters like ']\?*% that could screw things up; you could use addslashes on $row[username] if you prefer. > After this I need to be able to SUBMIT the info to UPDATE the table. > > I understand that I will have to execute a number of update statements ... > But how can I create these statements that tie the correct username and > AUTHORIZE value together? Note the final hidden field, containing the original state; this makes it easy to check whether the value has changed and only update it if needed: =========== updater.php ========== <?php // log into database, etc foreach($authorize as $id => $state) { if ($state != $oldauthorize[$id]) { $query = "UPDATE users (authorized) " ."VALUES ('{$state}') WHERE id = $id"; $result = mysql_query($query); // should check $result, make sure the update was successful } } ?> =========== end =============== If you felt ambitious, you could parse the data into just two queries - one for authorized='Y' and one for authorized='N', using "... WHERE id IN ( id1, id2, id3... inN )".

« previous php.db (#10875) next »