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

时间:2022-09-26 08:43:17

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.

我没有收到任何错误,所以我不知道出了什么问题。当我点击保存时,数据库中没有任何更新。

2 个解决方案

#1


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.

添加$ db-> setAttribute(PDO :: ATTR_ERRMODE,PDO :: ERRMODE_EXCEPTION);在连接打开后立即。它将发出代码中发现的任何错误信号。

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.

另外,请确保session_start();确实已加载,因为您正在使用会话变量; $ user_id = $ _SESSION ['user'] ['id'];你的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

#2


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

当你真正命名那个按钮btn-save时

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

请记住,变量名称很重要!如果你犯了一个错字或错误,事情就会完全破裂。

#1


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.

添加$ db-> setAttribute(PDO :: ATTR_ERRMODE,PDO :: ERRMODE_EXCEPTION);在连接打开后立即。它将发出代码中发现的任何错误信号。

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.

另外,请确保session_start();确实已加载,因为您正在使用会话变量; $ user_id = $ _SESSION ['user'] ['id'];你的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

#2


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

当你真正命名那个按钮btn-save时

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

请记住,变量名称很重要!如果你犯了一个错字或错误,事情就会完全破裂。