Jump to content

Saving many items(radio button) in one button.


ronankeating

Recommended Posts

Your form controls need to be an array, instead of just a single value. You do that by using [] notation in the name. By the way, you are not using radio controls properly. All radio controls that are part of the same set need to have the same name, otherwise they just act like permanent checkboxes.

 

Change your form to something like:

<td><input type="radio" name="gender[<?php echo $row['id'] ?>]" value="male"> Male </td>
<td><input type="radio" name="gender[<?php echo $row['id'] ?>]" value="female"> Female </td>
<td><input type="radio" name="gender[<?php echo $row['id'] ?>]" value="transgender"> Transgender </td>
<td><input type="radio" name="gender[<?php echo $row['id'] ?>]" value="gay"> Gay </td>
<td><input type="radio" name="gender[<?php echo $row['id'] ?>]" value="lesbian"> Lesbian </td>
Notice I have renamed "op" to "gender", because it makes more sense. Note also that I have changed the values to correspond to the way you're storing them. By the way, the way you're storing them is not very efficient, but I'll ignore that for now.

 

Then to process it you'd do something like:

if (!empty($_POST)) {
    $genders = $_POST['gender'];

    foreach ($genders as $id => $gender) {
        $sql = sprintf(
            "UPDATE table SET gender='%s' WHERE id=%s",
            mysqli_real_escape_string($gender),
            intval($id)
        );

        mysqli_query($sql);
    }
}
Not the most efficient method, and does not handle error conditions, but that should get you started.
Link to comment
Share on other sites

Hi sir I want to save data like the image above.

this is my table details.

tbl_person

id(PK,INT,11)
name(varchar,50)



tbl_person_gender
id int(PK , int,11)

person id(FK, int ,11)

gender (varchar,255)

You can refer to the image attached sir. Thank you

Edited by ronankeating
Link to comment
Share on other sites

Not the way one is supposed to design a database.

 

You have people - persons as you call them. They each have some qualities - attributes in db lingo - which consist of things like:

name

gender

address?

title?

phone?

 

and other things. These all belong in one table unless you find that you have multiples of them, such as two addresses. (People often have multiple phone numbers but I like keeping them stored in the main table under unique names such as 'home','work','mobile' rather than using a second table.

 

So - in your case - you should not be storing gender in a separate table. Especially since a person can only have one gender and it usually doesn't ever change.

Link to comment
Share on other sites

hi @ginerjm, I just only want on how to save looped radio button value to database. :) 
the database I have posted is only example or dummy. Please help me. Thank you so much.But on otherhand,
I would link to thank you for recalling me on how to make a good database.

Edited by ronankeating
Link to comment
Share on other sites

Hi how can I save radio button value with its related person like this?

table person_gender

ID    person_id          Gender

1             1                 male 

2             2                 female

3             3                 gay

4             4                 lesbian

 

Link to comment
Share on other sites

Hi @scootstah,Based on your post, I would like to ask how name="gender[<?php echo $row['id'] ?>]

work? Thanks

It creates an array of values in the corresponding $_POST variable. Normally you would just have a single value for each form element. So if you did <input name="gender" type="radio" value="male" />, then $_POST['gender'] would just be male. But if you did <input name="gender[1]" type="radio" value="male" />, then now $_POST['gender'] will be an array... like:

array(1 => 'male')
If we use the ID of the row inside the square brackets, name="gender[<?php echo $row['id']", then we can iterate through the $_POST array and determine which gender value matches which row.

 

Since you're using that weird schema you probably want to put the ID of the person in the form name, and then do an INSERT ... ON DUPLICATE KEY UPDATE query.

Edited by scootstah
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.