For some reason when I check my itesm for example question 1 CHECKED question 2 NOT CHECKED question 3 CHECKED question 4 NOT CHECKED I then hit save... and reload the page and get question 1 CHECKED question 2 CHECKED question 3 NOT CHECKED question 4 NOT CHECKED mysql - -- -- Table structure for table `questions` -- CREATE TABLE IF NOT EXISTS `questions` ( `id` int(100) NOT NULL, `question` varchar(255) NOT NULL, `image` varchar(255) NOT NULL, `selected` varchar(5) NOT NULL ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8; -- -- Dumping data for table `questions` -- INSERT INTO `questions` (`id`, `question`, `image`, `selected`) VALUES (1, 'How many beans in this jar?', '', 'on'), (2, 'how many apples in this basket?', '', ''), (3, 'How many grapes on this vine?', '', ''), (4, 'test', 'image3.png', ''), (5, 'test2', 'image3.png', ''); -- -- Indexes for dumped tables -- -- -- Indexes for table `questions` -- ALTER TABLE `questions` ADD PRIMARY KEY (`id`); -- -- AUTO_INCREMENT for dumped tables -- -- -- AUTO_INCREMENT for table `questions` -- ALTER TABLE `questions` MODIFY `id` int(100) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=6; Code (markup): PHP >> if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "save")) { // Build the SQL query to insert the details try { $sqlupdate = $db->prepare("UPDATE `questions` SET `selected`=:checkbox WHERE `id`=:qid"); foreach ($_POST['checkboxhidden'] as $id) { $checkbox = $_POST['checkbox'][$id]; $qid = $_POST['qid'][$id]; $sqlupdate->bindParam(":checkbox", $checkbox, PDO::PARAM_STR); $sqlupdate->bindParam(":qid", $qid, PDO::PARAM_STR); $sqlupdate->execute(); } } catch(PDOException $e) { echo "Some sort of error has occurred! Here are the details! "; echo $e->getMessage(); } $insertGoTo = "home.php"; if (isset($_SERVER['QUERY_STRING'])) { $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?"; $insertGoTo .= $_SERVER['QUERY_STRING']; } header(sprintf("Location: %s", $insertGoTo)); } $editFormAction = $_SERVER['PHP_SELF']; if (isset($_SERVER['QUERY_STRING'])) { $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']); } $load = $db->prepare("SELECT `id`,`question`,`image`,`selected` FROM `questions`"); $load->execute(); echo '<form action="'. $editFormAction.'" id="save" name="save" method="POST"> '; $i=0; while($show_load = $load->fetch()){ echo'<input type="hidden" name="qid[]" value="'.$show_load['id'].'"><img src='."'../photos/".$show_load['image']."' width='200px' height='200px'><BR> ".$show_load['question']; echo'<input type="hidden" name="checkboxhidden[]" value="'.$show_load['id'].'" >'; echo'<input type="checkbox" name="checkbox[]"'; if ($show_load['selected'] == 'on') { echo'checked="checked" ';}else {echo '';} echo ' >'; echo '<BR><BR><BR>'; $i++; } echo '<input type="hidden" name="MM_update" value="save"> <input type="submit" value="save"> <BR><BR><BR><BR><BR><BR><BR> </form>'; PHP:
php or mysql ? What browser are you using? Its loading fine for me.. -- Table structure for table `questions` -- CREATE TABLE IF NOT EXISTS `questions` ( `id` int(100) NOT NULL, `question` varchar(255) NOT NULL, `image` varchar(255) NOT NULL, `selected` varchar(5) NOT NULL ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8; -- -- Dumping data for table `questions` -- INSERT INTO `questions` (`id`, `question`, `image`, `selected`) VALUES (1, 'How many beans in this jar?', '', 'on'), (2, 'how many apples in this basket?', '', ''), (3, 'How many grapes on this vine?', '', ''), (4, 'test', 'image3.png', ''), (5, 'test2', 'image3.png', ''); -- -- Indexes for dumped tables -- -- -- Indexes for table `questions` -- ALTER TABLE `questions` ADD PRIMARY KEY (`id`); -- -- AUTO_INCREMENT for dumped tables -- -- -- AUTO_INCREMENT for table `questions` -- ALTER TABLE `questions` MODIFY `id` int(100) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=6; if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "save")) { // Build the SQL query to insert the details try { $sqlupdate = $db->prepare("UPDATE `questions` SET `selected`=:checkbox WHERE `id`=:qid"); foreach ($_POST['checkboxhidden'] as $id) { $checkbox = $_POST['checkbox'][$id]; $qid = $_POST['qid'][$id]; $sqlupdate->bindParam(":checkbox", $checkbox, PDO:ARAM_STR); $sqlupdate->bindParam(":qid", $qid, PDO:ARAM_STR); $sqlupdate->execute(); } } catch(PDOException $e) { echo "Some sort of error has occurred! Here are the details! "; echo $e->getMessage(); } $insertGoTo = "home.php"; if (isset($_SERVER['QUERY_STRING'])) { $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?"; $insertGoTo .= $_SERVER['QUERY_STRING']; } header(sprintf("Location: %s", $insertGoTo)); } $editFormAction = $_SERVER['PHP_SELF']; if (isset($_SERVER['QUERY_STRING'])) { $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']); } $load = $db->prepare("SELECT `id`,`question`,`image`,`selected` FROM `questions`"); $load->execute(); echo '<form action="'. $editFormAction.'" id="save" name="save" method="POST"> '; $i=0; while($show_load = $load->fetch()){ echo'<input type="hidden" name="qid[]" value="'.$show_load['id'].'"><img src='."'../photos/".$show_load['image']."' width='200px' height='200px'><BR> ".$show_load['question']; echo'<input type="hidden" name="checkboxhidden[]" value="'.$show_load['id'].'" >'; echo'<input type="checkbox" name="checkbox[]"'; if ($show_load['selected'] == 'on') { echo'checked="checked" ';}else {echo '';} echo ' >'; echo '<BR><BR><BR>'; $i++; } echo '<input type="hidden" name="MM_update" value="save"> <input type="submit" value="save"> <BR><BR><BR><BR><BR><BR><BR> </form>';
Cleaned up your php/html a bit: <?php if (isset($_POST["MM_update"]) && $_POST["MM_update"] == "save") { try { $sqlupdate = $db->prepare("UPDATE `questions` SET `selected`=:checkbox WHERE `id`=:qid"); foreach ($_POST['checkboxhidden'] as $id) { $sqlupdate->execute(array(':checkbox'=>$_POST['checkboxhidden'][$id]),':qid'=>$_POST['qid'][$id]); } } catch(PDOException $e) { echo 'Some sort of error has occurred! Here are the details!'; echo $e->getMessage(); } $insertGoTo = 'home.php'; if (isset($_SERVER['QUERY_STRING'])) { $insertGoTo .= (strpos($insertGoTo, '?')) ? '&' : '?'; $insertGoTo .= $_SERVER['QUERY_STRING']; } header(sprintf('Location: %s', $insertGoTo)); } $editFormAction = $_SERVER['PHP_SELF']; if (isset($_SERVER['QUERY_STRING'])) { $editFormAction .= '?'.htmlentities($_SERVER['QUERY_STRING']); } $load = $db->prepare("SELECT `id`,`question`,`image`,`selected` FROM `questions`"); $load->execute(); echo '<form action="'. $editFormAction.'" id="save" name="save" method="post">'; // $i=0; while($show_load = $load->fetch()){ echo'<input type="hidden" name="qid[]" value="'.$show_load['id'].'"> <img src="../photos/'.$show_load['image'].'" width="200px" height="200px"><br>'.$show_load['question'].' <input type="hidden" name="checkboxhidden[]" value="'.$show_load['id'].'"> <input type="checkbox" name="checkbox[]"'.(($show_load['selected'] == 'on') ? 'checked' : '').'> <br><br><br>'; } echo '<input type="hidden" name="MM_update" value="save"> <input type="submit" value="save"> </form>'; PHP:
Thank you but its not working.. I changed this $sqlupdate->execute(array(':checkbox'=>$_POST['checkboxhidden'][$id],':qid'=>$_POST['qid'][$id])); However I am getting On, 2 3 4 5 oor if I change it to this $sqlupdate->execute(array(':checkbox'=>$_POST['checkbox'][$id],':qid'=>$_POST['qid'][$id])); then I get on on on x x its something my rows but I am not sure what I am missing.. My goal is to ON on X on x but its still updating in order...
Right, it was untested, and there was a couple bugs: <?php if (isset($_POST['submit'])) { try { $sqlupdate = $db->prepare("UPDATE `questions` SET `selected`=:checkbox WHERE `id`=:qid"); foreach ($_POST['checkbox'] as $id => $value) { $sqlupdate->execute(array(':checkbox'=>$value,':qid'=>$id)); } } catch(PDOException $e) { echo 'Some sort of error has occurred! Here are the details!'; echo $e->getMessage(); } $insertGoTo = 'home.php'; if (isset($_SERVER['QUERY_STRING'])) { $insertGoTo .= (strpos($insertGoTo, '?')) ? '&' : '?'; $insertGoTo .= $_SERVER['QUERY_STRING']; } //header(sprintf('Location: %s', $insertGoTo)); } $editFormAction = $_SERVER['PHP_SELF']; if (isset($_SERVER['QUERY_STRING'])) { $editFormAction .= '?'.htmlentities($_SERVER['QUERY_STRING']); } $load = $db->prepare("SELECT `id`,`question`,`image`,`selected` FROM `questions`"); $load->execute(); echo '<form action="'. $editFormAction.'" id="save" name="save" method="post">'; // $i=0; while($show_load = $load->fetch()){ echo'<img src="../photos/'.$show_load['image'].'" width="200px" height="200px"><br>'.$show_load['question'].' <input type="checkbox" name="checkbox['.$show_load['id'].']"'.(($show_load['selected'] == 'on') ? 'checked' : '').'> <br><br><br>'; } echo '<input type="submit" name="submit" value="save"> </form>'; ?> PHP: Removed a LOT of the hidden inputs (no need), redid the check, etc. Tested and works
Thank you so much!! Yea i tried about 6 hours yesterday... However I was not trying a array. It works.. but I have one last question. When I bring up the page and something is already checked. I go to uncheck it but it still stays checked when I go to save it.
That's STILL filled with some real nonsense. If you aren't passing parameters, use PDO::query instead of :repare. TRY to comma delimit your output instead of using the slower and longer string additions, do not waste multiple echo statements to do the job of one, the width attribute only accepts and integer (As pixels) or a percentage, so using width="200px" is gibberish... It might REALLY help if you had a properly formed form instead of an invalid one (where's your FIELDSET and LABEL's?) As to not being able to uncheck them, this is why you NEVER trust client-side element names. Not only could I bullshit some field with any value I wanted, checkboxes do not EXIST if they are unchecked in the return array. I'm guessing wildly but: <?php /* Warning, uses PHP 5.4+ style arrays. Throws an error on those, your IPS inhales the proverbial equine of short stature. First, let's REALLY check what we're coming from. NEVER trust user input to be complete. Assumes $db is a connected PDO object. That's where the try/catch should have been done, we hopefully shouldn't need to try/catch at this point. I'm going to assume that questions.selected is boolean/TINYINT 0..1 As with PoPSiCLe's first post, untested so there may be typo's but should be enough to show how to go about this. */ if ( isset($_POST['submit']) && ($_POST['submit'] == 'save') && isset($_POST['checkbox']) ) { /* We HAVE to pull a list of the valid questions to check the $_POST values against. NEVER trust that the name attributes or array indexes returned by a query are valid or complete! Use ::query since we are passing no parameters! */ $questions = $db->query('SELECT id FROM questions'); $updateQuestions = $db->prepare(' UPDATE questions SET selected = :selected WHERE id = :id '); while ($id = $questions->fetchColumn()) $updateQuestions->execute([ ':selected' => array_key_exists($_POST['checkbox'], $id), ':id' => $id ]); } /* unless you are using Nyetscape 4 style scripttardery there is NO reason for a FORM to have a NAME attribute! You should be using SCRIPT_NAME, as it's more secure than PHP_SELF Let's kill off some unneccesary global vars while we're at it. */ echo ' <form action="', $_SERVER['SCRIPT_NAME'], ( isset($_SERVER['QUERY_STRING']) ? '?' . htmlentities($_SERVER['QUERY_STRING']) : '' ), ' method="post" id="save" > <fieldset>'; // no parameters, don't use prepare! $questionStmt = $db->query(' SELECT id, question, image, selected FROM questions '); /* Let's actually make that text be LABELS!!! With proper FOR attributes targeting the corresponding INPUT tag. Likewise, comma delimit the blasted output instead of wasting memory and time adding it together as a string before it's sent! */ while ($question = $questionStmt->fetch(PDO::FETCH_ASSOC)) echo ' <img src="../photos/', $question['image'], '" width="200" height="200" alt="GET SOME FREAKING ALT TEXT ON THESE!!!" ><br> <label for="', $tempId = 'save_' . $question['id'], '"> ', $question['question'], ' </label> <input type="checkbox" name="checkbox[', $question['id'], ']" id="', $tempId, '"', ( $question['selected'] ? ' checked' : '' ), ' ><br>'; /* you need more than one BR at the end of the above label/input pair, you probably don't know what padding or margin are. Don't put *** in the markup that doesn't belong there! */ echo ' </fieldset> <div class="submitsAndHiddens"> <input type="submit" name="submit" value="save"> <!-- .submitsAndHiddens --></div> </form>'; ?> Code (markup): Would make a hell of a lot more sense.