使用PHP PDO从数据库获取值并将输入更新为已选中 [英] Get values from database using PHP PDO and update input to checked

查看:45
本文介绍了使用PHP PDO从数据库获取值并将输入更新为已选中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难获得想要的结果.我做了很多研究,但还没有得到.我对此很陌生,但是在发布此问题之前做了研究.

I am having the most difficult time getting the results that I want. I have done a ton of research and I am just not getting it. I am very new to this, but did my research before posting this question.

好,所以我有一个包含这些列的表: user_id,my_music,my_movies,my_weather,my_maps和my_news

Ok, so I have a table with these columns: user_id, my_music, my_movies, my_weather, my_maps, and my_news

除user_id之外的每个列都将具有1或0.我需要做的是找出数据库中针对特定用户的每一列所存储的值.

Each columns except user_id will have either a 1 or 0. What I need to do is find out the value stored in the database for each column for a specific user.

这是我到目前为止所拥有的-这是我的config.php:

Here is what I have so far - This is my config.php:

// These variables define the connection information for your MySQL database
$username = "dbo12345";
$password = "xxxxxx";
$host = "db12345.db.123.com";
$dbname = "db12345";

$options = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8');
try { $db = new PDO("mysql:host={$host};dbname={$dbname};charset=utf8", $username, $password, $options); }
catch(PDOException $ex){ die("Failed to connect to the database: " . $ex->getMessage());}
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
header('Content-Type: text/html; charset=utf-8');
session_start();

这是我的admin.php文件:

Here is my admin.php file:

require("config.php"); 
if(empty($_SESSION['user'])) 
{
    header("Location: index.php");
    die("Redirecting to index.php"); 
}   

$userid = $_SESSION['user']['id'];
$sql = "SELECT my_music, my_movies, my_weather, my_maps, my_news FROM user_preferences WHERE user_id = :userID"; //Note the removed semi-colon that was probably causing your error
$stmt = $db->prepare($sql);
$stmt = $db->bindParam(":userID", $userid);
$userid = $_SESSION['user']['id'];
$sql = "SELECT my_music, my_movies, my_weather, my_maps, my_news FROM user_preferences WHERE user_id = :userID"; //Note the removed semi-colon that was probably causing your error
$stmt = $db->prepare($sql);
$stmt->bindParam(":userID", $userid, PDO::PARAM_INT);
$stmt->execute();
$result = $stmt->fetch();

if ($result['my_music']) {
    $musicChecked = 'checked="checked"';
} else {
    $musicChecked = '';

}
if ($result['my_movies']) {
    $checked = 'checked="checked"';
} else {
    $checked = '';

}

我该如何不同地编写以上代码?我知道有一种方法,但是我找不到它.

How can I write the above code differently? I know there is a way and I am having trouble finding it.

基于以上结果,我需要更新一些复选框,例如,如果数据库中my_music为1,则将复选框设置为"checked".所以我正在这样做:

Based on the results above I need to update some checkboxes, for example if my_music is 1 in the database then set the checkbox to checked. So I am doing this:

<input type="checkbox" name="mymusic" id="mymusic" <? echo $musicChecked;?> />
<input type="checkbox" name="mymovies" id="mymovies" <? echo $moviesChecked;?> />

如果我需要添加更多信息,请告诉我.任何帮助都将不胜感激.

If I need to add more info please let me know. Any and all help is greatly appreciated.

推荐答案

您真的很亲密,未正确获取:

You were really close, you did not fetch properly:

require("config.php"); 
if(empty($_SESSION['user']['id'])) 
{
    header("Location: index.php");
    die("Redirecting to index.php"); 
}   

$userid = $_SESSION['user']['id'];

$sql = "SELECT my_music, my_movies, my_weather, my_maps, my_news 
        FROM user_preferences 
        WHERE user_id = :userID";

$stmt = $db->prepare($sql);
$stmt->bindParam(":userID", $userid);
$stmt->execute();

$result = $stmt->fetch();

  • 您将Params绑定到语句对象而不是连接上
  • 您也可以从语句中获取数据,而不是从连接中获取数据
  • 如果您要查看内容使用var_dump而不是echo
  • fetchAll将返回一个二维数组.

    • You bind Params on the statement object not the connection
    • You fetch on the statement as well not the connection
    • fetchAll returns a 2 dimensional array if you want to see the content use var_dump not echo
    • <input id="mymusic"
             type="checkbox" 
             name="mymusic" 
             <?php echo ($result['my_music']==1 ? 'checked' : '');?>     
      />
      
      <input id="mymovies"
             type="checkbox" 
             name="mymovies"  
             <?php echo ($result['mymovies']==1 ? 'checked' : '');?>
      />
      

      这篇关于使用PHP PDO从数据库获取值并将输入更新为已选中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆