我正经历着最困难的时期才能获得我想要的结果。我做了很多研究,但我就是不明白。我对此很陌生,但在发布这个问题之前做了我的研究。
好的,我有一个包含这些列的表:
user_id、my_music、my_movies、my_weather、my_maps 和 my_news
除 user_id 之外的每一列都将具有 1 或 0。我需要做的是找出特定用户的每一列存储在数据库中的值。
这是我到目前为止所拥有的 - 这是我的 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 文件:
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 = '';
}
我怎样才能以不同的方式编写上面的代码?我知道有一种方法,但我很难找到它。
根据上面的结果,我需要更新一些复选框,例如,如果数据库中的 my_music 为 1,则将复选框设置为选中。所以我正在这样做:
<input type="checkbox" name="mymusic" id="mymusic" <? echo $musicChecked;?> />
<input type="checkbox" name="mymovies" id="mymovies" <? echo $moviesChecked;?> />
如果我需要添加更多信息,请告诉我。非常感谢任何和所有的帮助。