demeritrious Posted March 16, 2016 Share Posted March 16, 2016 I feel like this should be easy but I am stuck. I am attempting to create a form that will dynamically update rows using radiobuttons selected by the user. It is a questionnaire. Next will be figuring out the best way to use pagination and update only the records displayed on that page. Thanks in advanced Assessment.php <form action="Process/Process.php" method="post"> <?php do { ?> <label><?php echo $row_Questions['Questions']; ?></label> <label> <input <?php if (!(strcmp($row_Questions['Value'],"1"))) {echo "checked=\"checked\"";} ?> type="radio" name="Value[<?php echo $row_Questions['QuestionID']; ?>]" class="icheck" data-radio="iradio_square-blue" value="1"> Strongly Disagree </label> <label> <input <?php if (!(strcmp($row_Questions['Value'],"2"))) {echo "checked=\"checked\"";} ?> type="radio" name="Value[<?php echo $row_Questions['QuestionID']; ?>]" class="icheck" data-radio="iradio_square-blue" value="2"> Disagree </label> <label> <input <?php if (!(strcmp($row_Questions['Value'],"3"))) {echo "checked=\"checked\"";} ?> type="radio" name="Value[<?php echo $row_Questions['QuestionID']; ?>]" class="icheck" data-radio="iradio_square-blue" value="3"> Neutral </label> <label> <input <?php if (!(strcmp($row_Questions['Value'],"4"))) {echo "checked=\"checked\"";} ?> type="radio" name="Value[<?php echo $row_Questions['QuestionID']; ?>]" class="icheck" data-radio="iradio_square-blue" value="4"> Agree </label> <label> <input <?php if (!(strcmp($row_Questions['Value'],"5"))) {echo "checked=\"checked\"";} ?> type="radio" name="Value[<?php echo $row_Questions['QuestionID']; ?>]" class="icheck" data-radio="iradio_square-blue" value="5"> Strongly Agree </label> <input type="hidden" name="AnswerID[]" value="<?php echo $row_Questions['AnswerID']; ?>"> <input type="hidden" name="QuestionID[]" value="<?php echo $row_Questions['QuestionID']; ?>"> <input type="hidden" name="UserID[]" value="User"> <?php } while ($row_Questions = mysql_fetch_assoc($Questions)); ?> <input type="submit" name="ValueAssessmen" value="Submit"> </form> Process.php if(isset($_POST['ValueAssessment'])) { $AnswerID = count($_POST['AnswerID']); $i = 0; while ($i < $AnswerID) { $AnswerID= $_POST['AnswerID'][$i]; $Value= $_POST['Value'][$i]; $QuestionID= $_POST['QuestionID'][$i]; $UserID= $_POST['UserID'][$i]; $query = "UPDATE answer SET Value = '$Value', QuestionID = '$QuestionID', UserID = '$UserID' WHERE AnswerID = '$AnswerID'"; mysql_query($query) or die ("Error in update query: $query"); ++$i; } } Quote Link to comment https://forums.phpfreaks.com/topic/301011-update-multiple-rows-with-radiobuttons/ Share on other sites More sharing options...
mac_gyver Posted March 16, 2016 Share Posted March 16, 2016 I am attempting to create a form that will dynamically update rows using radiobuttons selected by the user. i would start by storing/defining the QuestionID and Questions (text) in one table and the user's choices in a different table. you would store the user_id, QuestionID, and the choice value (1-5) in the second table. the user_id/QuestionID (together) would be defined as a unique composite index to enforce uniqueness. this will also let you use an INSERT ... ON DUPLICATE KEY UPDATE query to either insert new data or update existing data. Quote Link to comment https://forums.phpfreaks.com/topic/301011-update-multiple-rows-with-radiobuttons/#findComment-1532038 Share on other sites More sharing options...
demeritrious Posted March 16, 2016 Author Share Posted March 16, 2016 That method did not work with my current insert statement. If i can get this Update multiple records query working then all would be well for me. I would also like to make that work in a page that I will make in the future that requires multiple records to be updated. Any suggestions on how I can get the code that I posted earlier to work? I've even tried a foreach loop still no luck. Thank you for helping Quote Link to comment https://forums.phpfreaks.com/topic/301011-update-multiple-rows-with-radiobuttons/#findComment-1532039 Share on other sites More sharing options...
ginerjm Posted March 16, 2016 Share Posted March 16, 2016 This code: $i = 0; while ($i < $AnswerID) { $AnswerID= $_POST['AnswerID'][$i]; ... ... has got to be part of the problem. How can you use a certain var as a control for your loop and then immediately change its value inside that loop? Quote Link to comment https://forums.phpfreaks.com/topic/301011-update-multiple-rows-with-radiobuttons/#findComment-1532055 Share on other sites More sharing options...
mac_gyver Posted March 16, 2016 Share Posted March 16, 2016 the only thing your form should submit is the radio button data. it should tell you which database table row the data corresponds to, as the name='Value[...]' key, and which radio button was selected, as the submitted value='1-5'. the only thing the UPDATE query should be SET'ing is the Value field, based on which row is being updated and which radio button was selected. the rest of your hidden form fields don't make any sense, mostly because they don't relate the submitted data to the row it corresponds to. if you define the table holding the user's choices as i suggested, the user_id/QuestionID pair identifies which row to UPDATE and your existing radio button form field is correct. if you instead have an auto increment column defined for that table, which i'm guessing is what the AnswerID is, you would use it as the name= 'Value[...]' key, instead of the QuestionID. in either case, the keys of the $_POST['Value'] field will tell you which row the submitted radio button data corresponds to. your loop would look like - foreach($_POST['Value'] as $key=>$value) { // $key will tell you which row to update, $value will be the selected radio button value 1-5 } the UserID value you use in the database query should be coming from your user-system php code. it shouldn't be coming from the form since that would allow a user to alter someone else's data. Quote Link to comment https://forums.phpfreaks.com/topic/301011-update-multiple-rows-with-radiobuttons/#findComment-1532056 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.