dapcigar Posted July 6, 2015 Share Posted July 6, 2015 Hello all, Am trying to retrieve a table from the DB, allow user select multiple options and also put the level . i was able to save the Data of the selected checkboxes but i cant get the level to save. my display code <form action="save_comp.php" method="post"> <?php include ('mysql_connect.php'); $sql = mysql_query("SELECT * FROM competency WHERE department = '$department'"); while($row = mysql_fetch_array($sql)) { echo "<tr>"; echo "<td>"; echo"<input type='checkbox' name='comp[]' value= ".$row['id']." /> ".$row['competency']." <br /> </td>"; echo"<td> <select name='level[]'> <option></option> <option>level 1</option> <option>level 2</option> <option>level 3</option> <option>level 4</option> <option>level 5</option> </select> </td> "; } echo "</tr>"; ?> <input name="submit" type="submit" value="submit" /> </form> <?php echo" </table>"; Code to save to DB <?php session_start(); $id = $_SESSION['user_id']; //$id = 3; $hobb = $_POST['comp']; $level = $_POST['level']; include ('mysql_connect.php'); $N = count($hobb); echo("<p>You selected $N Hobbies(s): "); for($i=0; $i < $N; $i++) { $var1=$hobb[$i]; $var2 = $level[$i]; include ('mysql_connect.php'); $table = "INSERT INTO competency_result (user_id,competency_id,level) ". "VALUES ('$id', '$var1', '$var2')"; mysql_query($table) or die(mysql_error()); $inserted_fid = mysql_insert_id(); mysql_close(); } Quote Link to comment Share on other sites More sharing options...
scootstah Posted July 6, 2015 Share Posted July 6, 2015 (edited) Here's how I'd do it. Change your form to this: echo"<input type='checkbox' name='comp[" . $row['id'] . "' value= '1' /> ".$row['competency']." <br /> </td>"; echo"<td> <select name='level[" . $row['id'] . "]'> <option></option> <option value='1'>level 1</option> <option value='2'>level 2</option> <option value='3'>level 3</option> <option value='4'>level 4</option> <option value='5'>level 5</option> </select> </td> ";And then process as such: <?php session_start(); $id = $_SESSION['user_id']; include ('mysql_connect.php'); $hobbies = $_POST['comp']; if (count($hobbies) !== 0) { echo("<p>You selected " . count($hobbies) . " Hobbies(s): "); $insert = array(); foreach (array_keys($hobbies) as $hobbyId) { $level = $_POST['level'][$hobbyId]; $insert[] = "(" . intval($id) . ", " . intval($hobbyId) . ", '" . mysql_real_escape_string($level) . "')"; } if (!empty($insert)) { $query = "INSERT INTO competency_result (user_id, competency_id, level) VALUES " . implode(',', $insert); mysql_query($query) or die (mysql_error()); $insertedId = mysql_insert_id(); } } Edited July 6, 2015 by scootstah Quote Link to comment Share on other sites More sharing options...
dapcigar Posted July 7, 2015 Author Share Posted July 7, 2015 Here's how I'd do it. Change your form to this: echo"<input type='checkbox' name='comp[" . $row['id'] . "' value= '1' /> ".$row['competency']." <br /> </td>"; echo"<td> <select name='level[" . $row['id'] . "]'> <option></option> <option value='1'>level 1</option> <option value='2'>level 2</option> <option value='3'>level 3</option> <option value='4'>level 4</option> <option value='5'>level 5</option> </select> </td> ";And then process as such: <?php session_start(); $id = $_SESSION['user_id']; include ('mysql_connect.php'); $hobbies = $_POST['comp']; if (count($hobbies) !== 0) { echo("<p>You selected " . count($hobbies) . " Hobbies(s): "); $insert = array(); foreach (array_keys($hobbies) as $hobbyId) { $level = $_POST['level'][$hobbyId]; $insert[] = "(" . intval($id) . ", " . intval($hobbyId) . ", '" . mysql_real_escape_string($level) . "')"; } if (!empty($insert)) { $query = "INSERT INTO competency_result (user_id, competency_id, level) VALUES " . implode(',', $insert); mysql_query($query) or die (mysql_error()); $insertedId = mysql_insert_id(); } } Didn't work.. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted July 7, 2015 Share Posted July 7, 2015 When something "didn't work" it usually helps all concerned if the voicer of that meaningless comment provides some backup as to WTH he/she is describing. Someone who was nice enough to provide a goodly chunk of code to you deserves more of a response than this so that he/she can help possibly debug it or to further discuss with you the problem. Jeez! Quote Link to comment Share on other sites More sharing options...
dapcigar Posted July 8, 2015 Author Share Posted July 8, 2015 When something "didn't work" it usually helps all concerned if the voicer of that meaningless comment provides some backup as to WTH he/she is describing. Someone who was nice enough to provide a goodly chunk of code to you deserves more of a response than this so that he/she can help possibly debug it or to further discuss with you the problem. Jeez! So sorry if that sounds like i didn't appreciate his effort. trust me i do. I should really apologize for my comment. it was harsh. i was only giving a feedback not to undermine him. i really need to fix this issue. Quote Link to comment Share on other sites More sharing options...
scootstah Posted July 8, 2015 Share Posted July 8, 2015 So sorry if that sounds like i didn't appreciate his effort. trust me i do. I should really apologize for my comment. it was harsh. i was only giving a feedback not to undermine him. i really need to fix this issue. And yet you still haven't told us what the problem is. We can't see your computer monitor. You need to tell us what you're seeing. Quote Link to comment 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.