简体   繁体   English

使用PHP PDO更新复选框的数据库值

[英]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: 这是我的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); 添加$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: 此外,并插入您的开头<?php标签下方:

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

Plus , make sure session_start(); 另外 ,确保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. 你的WHERE子句依赖它 - 只是一个洞察力。


Another thing to make sure, if this is the case, that the user_id column isn't an AUTO_INCREMENT 另一件事是确保,如果是这种情况, user_id列不是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 当你实际命名该按钮btn-save

Remember, variable names are important! 请记住, 变量名称很重要! If you make a single typo or error, things can break entirely. 如果你犯了一个拼写错误或错误,事情就会彻底破裂。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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