473,513 Members | 2,419 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Radio buttons and updating multiple rows with an update query

stepterr
157 New Member
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
5 7417
pbmods
5,821 Recognized Expert Expert
Heya, Stepterr.

Looks like your script only outputs one radio box per category.
Dec 22 '07 #2
stepterr
157 New Member
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
5,821 Recognized Expert Expert
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
157 New Member
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
5,821 Recognized Expert Expert
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

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

Similar topics

2
10636
by: scott | last post by:
Hi, I'm having some trouble with something that should be relatively easy. I want to update multiple rows in one of my database tables simultaneously. In my table I have these values: ...
2
1789
by: Ben Allen | last post by:
I have the following code which connects to a database and sets the value of counter to '0' and the date to be either the current date or a users own date. The database connects via an included...
2
3126
by: Jeff | last post by:
I'm trying to create a dynamic form that can have multiple groups of radio buttons (each group has two buttons) with the same name. Essentially, the form allows a user to enter as many names as...
2
5410
by: Rob | last post by:
Hi all, I've got multiple sets of radio button that are dynamically created in code and populated by a database query. The query returns about 20 recordsets with 3 radio buttons per recordset and...
5
12536
by: sam | last post by:
Hi all, I am dynamically creating a table rows and inerting radio buttons which are also dynamically created. Everything works fine in Firefox as expected. But I am not able to select radio...
5
1905
by: Dabbler | last post by:
What is the best way to handle data interpolation between form controls and the actual sql data field. I have radio buttons that need to be interpreted, form values that have to be translated.....
6
2712
by: sgottenyc | last post by:
Hello, If you could assist me with the following situation, I would be very grateful. I have a table of data retrieved from database displayed on screen. To each row of data, I have added...
7
3616
by: help4me | last post by:
Hope someone can help me out with this. I'm having trouble getting my radio button values into a MySQL database. And after I do get them inserted I'm going to want to be able to show them back in...
5
6681
by: satyabhaskar | last post by:
hi all, In my web page i have created radio buttons dynamically on to the page .....following is my code string Course, Semester, Section; int rowsCount; string con =...
1
7106
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7534
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5689
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5094
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4749
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3236
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1601
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
805
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
459
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.