StuHannah Posted September 1, 2016 Share Posted September 1, 2016 Morning, I am looking for some help. What I am trying to do is for each week of the year assign a letter to it, either an A or B week for rotas. I am able to print on screen the weeks for 52 weeks and this part works fine and lists the weeks, and what I want to do from there is use a combobox next to each date so we can allocate a week to it selecting either A or B. The code I have been using and working on I have put on the bottom. Just come a bit stuck and need a little help please. This bit gets me all of the weeks for the next 52 weeks. for($i=1; $i<=52; $i++){ $num = date("Y-m-d", strtotime('Last Monday +'.$i.' week')); echo $num I am now trying to get the below working and can't figure it out... // This is the part to read the submission <?php if(isset($_POST['formSubmit']) ) { $varWeekNow = $_POST['{$num}']; // SQL Bit $sql = "INSERT INTO weeks (Date, Week) VALUES ('$num', '$varWeekNow')"; if (mysqli_query($link, $sql)) { echo "New record created successfully"; } else { echo "Error: " . $sql . "<br>" . mysqli_error($link); }} ?> // this is the part to print the weeks and a dropbown box. <?php for($i=1; $i<=52; $i++){ $num = date("Y-m-d", strtotime('Last Monday +'.$i.' week')); echo "<form action=\"index.php\" method=\"post\"> What is this week? " . $num . " <select name=\"{$num}\"> <option value=\"\">Select...</option> <option value=\"A\">A Week</option> <option value=\"B\">B Week</option> </select> <input name=\"formSubmit\" type=\"hidden\" value=\1\" /> <input name=\"submit\" type=\"submit\" /> </p></form>"; } ?> Kind Regards, Stuart Quote Link to comment https://forums.phpfreaks.com/topic/302057-php-option-select-to-mysql/ Share on other sites More sharing options...
NotionCommotion Posted September 1, 2016 Share Posted September 1, 2016 In the top part of your code, you are using $num, but it isn't defined yet, '{$num}' is just a string, and $varWeekNow is thus NULL. Don't use $num as your name of your select menu, but make it the same name for all forms, and put another hidden input in each form with name "num", and value $num. PS. Take steps to prevent SQL injection. PSS. Use PDO. Quote Link to comment https://forums.phpfreaks.com/topic/302057-php-option-select-to-mysql/#findComment-1536947 Share on other sites More sharing options...
mac_gyver Posted September 1, 2016 Share Posted September 1, 2016 (edited) your design has some user experience UX problems. by using a select/option menu, the user must first click the select button to see the two choice to pick from and since each week is its own form, only one selection can be submitted at a time. if someone was setting up all 52 weeks, it would take 156 clicks and 52 form submissions and re-displays of the page. if you instead use radio buttons and a single form, the user can go through and select or change any of the week values by simply clicking on the choices and hitting one submit button. that would take only 53 clicks and one form submission to set up all 52 weeks. p.s. you are missing a " in your html markup, probably lost due to the escaping \" going on. you should use single-quotes ' inside of a double-quoted string so that the code is not cluttered up with escape characters. Edited September 1, 2016 by mac_gyver Quote Link to comment https://forums.phpfreaks.com/topic/302057-php-option-select-to-mysql/#findComment-1536954 Share on other sites More sharing options...
Barand Posted September 1, 2016 Share Posted September 1, 2016 Do the A and B weeks alternate? If they do then just setting whether the first is A or B should suffice. Quote Link to comment https://forums.phpfreaks.com/topic/302057-php-option-select-to-mysql/#findComment-1536956 Share on other sites More sharing options...
Solution Psycho Posted September 1, 2016 Solution Share Posted September 1, 2016 Using several of the recommendations above and some of my ideas on improving <?php $responseMessage = ''; if(isset($_POST['date']) ) { //Get list of ALL submitted dates with valid value $insertValues = array(); foreach($_POST['date'] as $date => $value) { if($value=='A' or $value=='B') { $insertValues[] = "('{$date}', '{$value}')"; } } //Run a a single query to inset all submitted values // *** The query should be a prepared statement to guard against SQL injection $sql = "INSERT INTO weeks (Date, Week) VALUES " . implode(',', $insertValues); if (mysqli_query($link, $sql)) { $responseMessage = "New record created successfully"; } else { // *** For debugging only. Should not display actual errors to user $responseMessage = "SQL: " . $sql . "<br>Error: " . mysqli_error($link); } } // *** Should run query to get already saved values from the database // *** and use those values in the loop below to set any already saved values //Create the form fields for each date $formFields = ''; $weeksOut = 52; for($weekNo=1; $weekNo<=$weeksOut; $weekNo++) { $date = date("Y-m-d", strtotime('Last Monday +'.$weekNo.' week')); $formFields .= "<p>What is this week?: {$date}\n"; $formFields .= "<input type='radio' name='date[{$date}]' value='A'>A</input>\n"; $formFields .= "<input type='radio' name='date[{$date}]' value='B'>B</input></p>\n"; } ?> <html> <body> <?php echo $responseMessage; ?> <br> <form action="test.php" method="post"> <?php echo $formFields; ?> <button type="submit">Submit</button> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/302057-php-option-select-to-mysql/#findComment-1536957 Share on other sites More sharing options...
StuHannah Posted September 5, 2016 Author Share Posted September 5, 2016 Thank you so much for all your help, i've been able to understand your code and adapt elements to make this work for the system I am developing.. I cannot thank you enough. Stuart Quote Link to comment https://forums.phpfreaks.com/topic/302057-php-option-select-to-mysql/#findComment-1537110 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.