473,326 Members | 2,655 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Radio buttons and updating multiple rows with an update query

stepterr
157 100+
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 7393
pbmods
5,821 Expert 4TB
Heya, Stepterr.

Looks like your script only outputs one radio box per category.
Dec 22 '07 #2
stepterr
157 100+
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 Expert 4TB
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 100+
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 Expert 4TB
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
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
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
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
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
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
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
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
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
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 =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.