dstnlhhv791576 2014-08-26 19:12
浏览 54
已采纳

使用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.

  • 写回答

2条回答 默认 最新

  • doufen9815 2014-08-26 19:15
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?