By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,214 Members | 2,066 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,214 IT Pros & Developers. It's quick & easy.

Radio buttons and updating multiple rows with an update query

stepterr
100+
P: 157
I have a form that is built based on a query. Everything is working except when I submit the form the radio buttons are only updating the first row in my database. dcategory and dthumbnail are two radio buttons that I have for each one of the rows in the table. I've tried using a hidden input object for each of the radio button groups to store the values in but clearly I'm having a mental block because I can't get that to work right either. So any new ideas would be appreciated. Thanks!

[PHP]

// form
$result = mysql_query("Select * from portfolio where projectname='$projectname' order by photoid");
if ($result)
{
?>
<form name='Proj' action='<?php echo $_SERVER['PHP_SELF']?>' method='post'>
<?php
print "<table><tr><td></td><td></td><tr>";
$count=mysql_num_rows($result);
while ($array= mysql_fetch_assoc($result))
{
$photoid = $array['photoid'];
$category = $array['category'];
$projectname = $array['projectname'];
$subname = $array['subname'];
$location = $array['location'];
$desc1 = $array['desc1'];
$dcategory = $array['dcategory'];
$dthumbnail = $array['dthumbnail'];

print "<input type='hidden' name=photoid[] value=$photoid />";
print "<tr><td><img src=$location style='cursor: pointer;' name=location[] width='216' height='144' border'0'/></td><td>";
if($dcategory=='Y')
{
print "Default Photo for Category?<input name='dcategory[]' type='radio' value='Y' checked='checked'/>";
}
else
{
print "Default Photo for Category?<input name='dcategory[]' type='radio' value='N' />";
}
if($dthumbnail=='Y')
{
print "Default Photo for Project?<input name='dthumbnail[]' type='radio' value='Y' checked='checked'/>";
}
else
{
print "Default Photo for Project?<input name='dthumbnail[]' type='radio' value='N' />";
}
print "<table><tr><td>Category:</td>";
print "<td><input type='text' name=category[] value=$category></td></tr>";
print "<tr><td>Project:</td>";
print "<td><input type='text' name=projectname[] value=$projectname></td></tr>";
print "<tr><td>Sub Title:</td>";
print "<td><input type='text' name=subname[] value=$subname></td></tr>";
print "<tr><td>Description:</td>";
print "<td><input type='text' name=desc1[] value=$desc1></td></tr>";
print "</table>";
}
print "</tr></table>";
print "<input type='submit' name='SubmitProj' value='Submit'>";
print "</form>";
}
}
// If user has submitted
if(isset($_POST["SubmitProj"]) && trim($_POST["SubmitProj"])!=='')
{
// Itirate the objects of the form
foreach($_POST['photoid'] as $key=>$photoid)
{
$update ="UPDATE portfolio SET category='".$_POST['category'][$key]."', projectname='".$_POST['projectname'][$key]."', subname='".$_POST['subname'][$key]."', desc1='".$_POST['desc1'][$key]."', dcategory='".$_POST['dcategory'][$key]."', dthumbnail='".$_POST['dthumbnail'][$key]."' WHERE photoid='$photoid'";
$result = mysql_query($update);
}
}[/PHP]
Dec 16 '07 #1
Share this Question
Share on Google+
5 Replies


pbmods
Expert 5K+
P: 5,821
Heya, Stepterr.

Looks like your script only outputs one radio box per category.
Dec 22 '07 #2

stepterr
100+
P: 157
Heya, Stepterr.

Looks like your script only outputs one radio box per category.
Basically what this code is trying to do is ouput a photo, radio button1, radio button2, and a few text boxes for each photo that is part of the group that the user has selected. I'm looping through all of the photos that meet the selection that they made and returning that to the page. The two radio buttons need to be grouped across all the rows of the returned recordset. I realize that the way radio buttons and check boxes work is that it only sends the information for the one that is selected but I need a way to keep track of which one they choose and make sure that the user can only select only one of the photos to be the default within the group for rb1 and same for the rb2 group. So for example I have these records returned from my database.

photo1 rb1 rb2
photo2 rb1 rb2
photo3 rb1 rb2
photo4 rb1 rb2

If the user has chosen rb1 for photo2 then photo2 becomes the default for the category that this photo belongs to.

I need the users to be able to click on both rb1 and rb2 within a row so I don't want those grouped together. But I do not want the user to be able to click rb1 for both photo1 and photo2. The way I have the code setup above it is controlling which ones they can click just fine. The problem is just that I can't figure out how to determine which one they have actually selected when it comes time to update my database since it only returns one value for radiobuttons instead of an array like what textboxes do.

Does that help clarify a little bit better? Hopefully I haven't just made it more confusing! :-)
Dec 31 '07 #3

pbmods
Expert 5K+
P: 5,821
Heya, Stepterr.

So you want to do something like this:
Expand|Select|Wrap|Line Numbers
  1. echo '<input type="radio" name="dcategory" value="', $photoid, '" ';
  2. if( $dCategory == 'Y' )
  3. {
  4.     echo ' checked="checked" ';
  5. }
  6. echo '/>';
  7.  
Dec 31 '07 #4

stepterr
100+
P: 157
Heya, Stepterr.

So you want to do something like this:
Expand|Select|Wrap|Line Numbers
  1. echo '<input type="radio" name="dcategory" value="', $photoid, '" ';
  2. if( $dCategory == 'Y' )
  3. {
  4.     echo ' checked="checked" ';
  5. }
  6. echo '/>';
  7.  

Thanks pbmods! I was clearly overlooking the simple solution and trying to make it more difficult than it should've been. Your solution worked perfectly.

For anyone else that may come across this thread and is having the same problem... I then just checked the value of the radio button within my foreach loop right before my update statement in order to actually update my database with a 'Y' for the photo that I want to be my default.
Jan 1 '08 #5

pbmods
Expert 5K+
P: 5,821
Heya, Stepterr.

Glad to hear you got it working! Good luck with your project, and if you ever need anything, post back anytime :)
Jan 2 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.