doudu6100 2014-08-26 14:01
浏览 65
已采纳

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

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

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.

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();

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.

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.

  • 写回答

3条回答 默认 最新

  • doukong1897 2014-08-26 15:49
    关注

    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();
    
    • 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' : '');?>
    />
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b