简体   繁体   中英

Update database values for checkboxes using PHP PDO

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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM