3

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.

iamthestreets
  • 733
  • 1
  • 15
  • 38

2 Answers2

3

Your submit button is (with a hyphen)

<input type="submit" name="btn-save"...

yet your conditional statement is (with an underscore) and is based on the code's execution:

if(isset($_POST['btn_save'])){...}

change it to

<input type="submit" name="btn_save"...

If you're not already using this:

Add $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); right after the connection is opened. It will signal any errors found in code.

Also, and inserted just below your opening <?php tag:

error_reporting(E_ALL);
ini_set('display_errors', 1);

Plus, make sure session_start(); is indeed loaded, since you are using session variables; $user_id = $_SESSION['user']['id']; your WHERE clause is relying on it --- just an insight.


Another thing to make sure, if this is the case, that the user_id column isn't an AUTO_INCREMENT

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • +1 our two answers are disjoint, yet they're both quite correct. Lots of errors in OP's problem – sjagr Aug 26 '14 at 19:26
  • +1 for yours also, thanks. OP could look through both and get the solution, *cheers* @sjagr – Funk Forty Niner Aug 26 '14 at 19:27
  • @iamthestreets That's great, am glad to hear it. If `sjagr`'s answer also helped, you can accept his answer, *cheers*. – Funk Forty Niner Aug 26 '14 at 19:37
  • I am running into another issue where one of the checkboxes is not changing on the page based on the database vaule - should I create a new question for that? Here is a [link](http://stackoverflow.com/questions/25507747/get-values-from-database-using-php-pdo-and-update-input-to-checked/25510053?noredirect=1#comment39826652_25510053) to one of my other questions with info showing how I am updating the checkbox to checked – iamthestreets Aug 26 '14 at 19:39
  • 1
    @iamthestreets Here's a [meta SO article](http://meta.stackoverflow.com/questions/255150/how-to-choose-between-multiple-correct-answers) that addresses this situation. As for your new problem, yes, that deserves a new question. – sjagr Aug 26 '14 at 19:40
  • 1
    @Fred-ii- Technically your answer is more correct, because it solves the immediate problem/symptom that OP was having. He would've ran into my point afterwards. I added your point after I saw your answer in the interest of making my answer more complete - so you deserve credit for the original solution. – sjagr Aug 26 '14 at 19:44
  • 1
    @sjagr That's quite nice of you, thanks. We'll call this a *group effort* ;) – Funk Forty Niner Aug 26 '14 at 19:45
  • Thanks for all the help! I fixed my other problem. I wish I could accept both answers. – iamthestreets Aug 26 '14 at 19:46
  • @iamthestreets You're very much welcome, and am sure I can speak for the both of us that we were happy to have helped and a solution was found. – Funk Forty Niner Aug 26 '14 at 19:48
  • @Fred-ii- I am running into a problem when I get to the last selection in I uncheck 1 at a time or uncheck all at once. For example I have 3 rows if I uncheck all the rows and click save I receive the error I put in the else statement. Tried debugging, but not really finding anything - still new to debugging. – iamthestreets Oct 02 '14 at 15:02
  • @iamthestreets You'd be best to make up a new question. Your original question is a few months old ;) – Funk Forty Niner Oct 02 '14 at 15:04
  • 1
    @Fred-ii- Thanks. I created a new question [here](http://stackoverflow.com/questions/26164244/update-database-values-for-checkboxes-using-php-pdo-not-updating-all-checkboxes) – iamthestreets Oct 02 '14 at 15:31
  • @iamthestreets You're welcome. I upvoted the question since I won't have time to look at it. Work just came in and I have to tend to that. *Cheers* – Funk Forty Niner Oct 02 '14 at 15:32
2

This

$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 are also checking for this

if(isset($_POST['btn_save']))

when you've actually named that button btn-save

Remember, variable names are important! If you make a single typo or error, things can break entirely.

sjagr
  • 15,983
  • 5
  • 40
  • 67