I am trying to update my database with a the value of the checkbox when the user clicks save.
Here is my code:
require("config.php");
$settingsArray = array('my_music', 'my_movies', 'my_weather', 'my_maps', 'my_news');
if(isset($_POST['btn_save']))
{
if(isset( $_POST['mysettings']))
{ $values = array();
foreach($_POST['mysettings'] as $selection )
{ if(in_array($selection, $settingsArray))
{ $values[ $selection ] = 1; }
else
{ $values[ $selection ] = 0; }
} // end of foreach.
try // save user selection to the database
{
$user_id = $_SESSION['user']['id'];
$stmt = $db->prepare("UPDATE user_preferences SET my_music = :mymusic, my_movies = :mymovies, my_weather = :myweather, my_maps = :mymaps, my_news = :mynews WHERE user_id = :userID");
$stmt->bindParam(":userID", $userid, PDO::PARAM_INT);
$stmt->bindParam(':mymusic', $values['mymusic']);
$stmt->bindParam(':mymovies', $values['mymovies']);
$stmt->bindParam(':myweather', $values['myweather']);
$stmt->bindParam(':mymaps', $values['mymaps']);
$stmt->bindParam(':mynews', $values['mynews']);
$stmt->execute();
} catch(PDOException $e) { echo 'Connection failed: ' . $e->getMessage(); }
}
else
{ echo 'No checkbox selection made...'; }
} // End of, if statement from the button check
Here is my HTML:
<form action="admin.php" method="post" role="form">
<input type="checkbox" name="mysettings[]" value="mymusic" <? echo $musicChecked;?> />
<input type="checkbox" name="mysettings[]" value="mymovies" <? echo $movieschecked;?> />
<input type="checkbox" name="mysettings[]" value="myweather" <? echo $weatherChecked;?> />
<input type="checkbox" name="mysettings[]" value="mymaps" <? echo $mapsChecked;?> />
<input type="checkbox" name="mysettings[]" value="mynews" <? echo $newsChecked;?> />
<input type="submit" name="btn-save" class="btn btn-md btn-primary btn-block" data-loading-text="Loading..." value="Save" />
</form>
I am not receiving any errors so I don't know what is wrong. When I hit save nothing gets updated in the database.