ronankeating Posted September 6, 2015 Share Posted September 6, 2015 (edited) Hi to all Php Mysqli experts, I need a help on how to save many data using one button only. (bulk saving).Attached is my code and my expected output(image). thank you index.php Edited September 6, 2015 by ronankeating Quote Link to comment https://forums.phpfreaks.com/topic/298077-saving-many-itemsradio-button-in-one-button/ Share on other sites More sharing options...
scootstah Posted September 6, 2015 Share Posted September 6, 2015 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. Quote Link to comment https://forums.phpfreaks.com/topic/298077-saving-many-itemsradio-button-in-one-button/#findComment-1520369 Share on other sites More sharing options...
ronankeating Posted September 6, 2015 Author Share Posted September 6, 2015 Thank you so much for helping me. I have another question. How can I save it in another table containing this fields. ID, PersonId, Gender.like in the image above. Quote Link to comment https://forums.phpfreaks.com/topic/298077-saving-many-itemsradio-button-in-one-button/#findComment-1520370 Share on other sites More sharing options...
scootstah Posted September 6, 2015 Share Posted September 6, 2015 So you have a table of persons and then a table for storing their gender? Can you post your database schema for all relevant tables using the output from this query: SHOW CREATE TABLE tablename. Change "tablename" to each of your tables. Quote Link to comment https://forums.phpfreaks.com/topic/298077-saving-many-itemsradio-button-in-one-button/#findComment-1520371 Share on other sites More sharing options...
ronankeating Posted September 6, 2015 Author Share Posted September 6, 2015 (edited) 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_genderid int(PK , int,11) person id(FK, int ,11) gender (varchar,255)You can refer to the image attached sir. Thank you Edited September 6, 2015 by ronankeating Quote Link to comment https://forums.phpfreaks.com/topic/298077-saving-many-itemsradio-button-in-one-button/#findComment-1520376 Share on other sites More sharing options...
ginerjm Posted September 6, 2015 Share Posted September 6, 2015 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. Quote Link to comment https://forums.phpfreaks.com/topic/298077-saving-many-itemsradio-button-in-one-button/#findComment-1520377 Share on other sites More sharing options...
ronankeating Posted September 7, 2015 Author Share Posted September 7, 2015 (edited) 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 September 7, 2015 by ronankeating Quote Link to comment https://forums.phpfreaks.com/topic/298077-saving-many-itemsradio-button-in-one-button/#findComment-1520427 Share on other sites More sharing options...
scootstah Posted September 7, 2015 Share Posted September 7, 2015 I've already shown you how you can save the form results to the database. Why don't you take what I've given you and try to make something work? Post back with your own code and where you are having problems. Quote Link to comment https://forums.phpfreaks.com/topic/298077-saving-many-itemsradio-button-in-one-button/#findComment-1520431 Share on other sites More sharing options...
ronankeating Posted September 7, 2015 Author Share Posted September 7, 2015 Hi @scootstah,Based on your post, I would like to ask how name="gender[<?php echo $row['id'] ?>]work? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/298077-saving-many-itemsradio-button-in-one-button/#findComment-1520432 Share on other sites More sharing options...
ronankeating Posted September 7, 2015 Author Share Posted September 7, 2015 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 Quote Link to comment https://forums.phpfreaks.com/topic/298077-saving-many-itemsradio-button-in-one-button/#findComment-1520433 Share on other sites More sharing options...
scootstah Posted September 7, 2015 Share Posted September 7, 2015 (edited) 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 September 7, 2015 by scootstah Quote Link to comment https://forums.phpfreaks.com/topic/298077-saving-many-itemsradio-button-in-one-button/#findComment-1520436 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.