Updating database values for checkboxes using PHP PDO
I am trying to update my database with a checkbox value when the user clicks on 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 getting any errors, so I don’t know what is wrong. When I click, nothing is updated in the database.
source to share
Your submit button (hyphenated)
<input type="submit" name="btn-save"...
but your conditional statement (with underscore) and is based on code execution:
if(isset($_POST['btn_save'])){...}
change it to
<input type="submit" name="btn_save"...
If you are not already using this:
Add $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
immediately after opening a connection. It will signal any errors found in the code.
Also inserted just below your tag <?php
:
error_reporting(E_ALL);
ini_set('display_errors', 1);
Also, make sure to session_start();
actually be loaded since you are using session variables; $user_id = $_SESSION['user']['id'];
your proposal WHERE
relies on it - just discernment.
One more thing to make sure that in this case the column is user_id
notAUTO_INCREMENT
source to share
it
$settingsArray = array('my_music', 'my_movies', 'my_weather', 'my_maps', 'my_news');
does not match the input values of this
<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;?> />
You also check this
if(isset($_POST['btn_save']))
when you actually named this button btn-save
Remember variable names are important! If you make one typo or mistake, things can break completely.
source to share