セッション変数の一部が引き継がれない問題
Array ( [login] => 1
[u_name] => aaa
[timezone_id] => (空白、きてない)
[u_icon] => 1
[u_timezone] => (空白、きてない)
)
Array ( [timezone_id] => 66
[a_date] => 2002-02-02
[a_time] => 10:02
[alert_kind] => 1
[alert_comment] => 1
[alert_type] => 1
[alert_level] => 2
[alert_public] => 1
)
Notice: Undefined index: u_id in C:\xampp\htdocs\php\makino\alert\exec.php on line 25
先生に解決してもらった
1.set Alertの画面で、userのtimezoneを第一候補にしてselectでだしたかったので、session変数からひきたかったが
2.①if文が==じゃなく=になってたので代入されて上書きされた
それで消えてた
3.そもそもif文はいらない
4.その前のSELECT文もおかしかった
$sql2 = "SELECT timezone_id,timezone_name FROM timezones WHERE ".$_SESSION["u_timezone"]."=timezone_id";
$res2 = $pdo->query($sql2);
必要なのはimezone_id,timezone_nameで、
WHERE句のあとが間違ってた
①$_SESSION["u_timezone"]とtimezone_idが一致したのを引きたい
②しかし$_SESSIONはphp文でtimezone_idの方はmysql文だ
③よって文字列結合をする
これ、最初の部分の「"」から「”」がひとかたまりのmysql文、たす、php文、たす、mysql文という構造になっている
セッション情報と一致するのを引いてくるにはこういう書き方をする!
5.<select name="timezone_id">に「ユーザが設定したタイムゾーン」を最初の項目として表示したい
<!--ここにuserのtimezone名を出すには??-->
<?php $row2 = $res2->fetch(PDO::FETCH_ASSOC); ?>
<option value="<?php echo $row2["timezone_id"]; ?>">
<?php echo($row2["timezone_name"]); ?>
</option>
①先にfetchする。先にかくことがあるのだ。
②valueはvalue="<?php echo $row["timezone_id"]; ?>">
valueの値はintのidでいいからさ。
表示する内容はtimezone_nameであってほしい(数字じゃわからん)ので
<?php echo htmlspecialchars($row["timezone_name"],ENT_QUOTES); ?>
③while文は1つなのでいらない
--->set_alert.php
<?php
session_start();
print_r($_SESSION);
if(empty($_SESSION["login"])) {
header("Location: login.php");
exit();
/*print_r("this");*/
}
require_once("config.php");
$sql = "SELECT timezone_id,timezone_name FROM timezones ORDER BY timezone_name";
$res = $pdo->query($sql);
$sql2 = "SELECT timezone_id,timezone_name FROM timezones WHERE ".$_SESSION["u_timezone"]."=timezone_id";
$res2 = $pdo->query($sql2);
?>
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
<title>User sign up</title>
<link rel="stylesheet" href="css/reset.css">
<link rel="stylesheet" href="css/style.css">
<link href="https://fonts.googleapis.com/css?family=Oxanium:500,700&display=swap" rel="stylesheet">
</head>
<body>
<div id="container">
<div id="sign_up flex">
<p class="green_box"><a href="#">My Alert</a></p>
<p class="green_box"><a href="#">Help</a></p>
<div><p class="icons"><span class="text_white"><?php echo htmlspecialchars($_SESSION["u_name"],ENT_QUOTES); ?></span> <img src="img/icon<?php echo htmlspecialchars($_SESSION["u_icon"],ENT_QUOTES); ?>.jpg" alt="icon"></p></div>
</div>
<h1 class="text_orange">Set New Alert</h1>
<form method="post" action="exec.php">
<table align="center">
<tr>
<th><label for="timezone_id">timezone</label></th>
<td>
<select name="timezone_id">
<!--ここにuserのtimezone名を出すには??-->
<?php $row2 = $res2->fetch(PDO::FETCH_ASSOC); ?>
<option value="<?php echo $row2["timezone_id"]; ?>">
<?php echo($row2["timezone_name"]); ?>
</option>
<?php while($row = $res->fetch(PDO::FETCH_ASSOC)): ?>
<option value="<?php echo $row["timezone_id"]; ?>">
<?php echo htmlspecialchars($row["timezone_name"],ENT_QUOTES); ?>
</option>
<?php endwhile; ?>
</select>
</td>
</tr>
<tr>
<th><label for="a_date">Set Date</label></th>
<td><input type="date" name="a_date" id="a_date"></td>
</tr>
<tr>
<th><label for="a_time">Set time</label></th>
<td><input type="time" name="a_time" id="a_time"></td>
</tr>
<tr>
<th><label for="alert_kind">alert_kind</label></th>
<td>
<select name="alert_kind">
<option value="1">1: Meeting</option>
<option value="2">2: Departure</option>
<option value="3">3: Deadline</option>
<option value="4">4: Etc.</option>
</select>
</td>
</tr>
<tr>
<th><label for="alert_comment">Alert Comment</label></th>
<td>
<textarea type="text" name="alert_comment" id="alert_comment" cols="30" rows="4" maxlength="120">
</textarea>
</td>
</tr>
<tr>
<th class="bg_gray"><label for="alert_type">alert_type</label></th>
<td>
<p>
<label for="alert_type"><input type="radio" name="alert_type" value="1" id="alert_type" checked><span class="text_blue"> 1: Inform on the day</span></label>
</p>
<p>
<label for="alert_type2"><input type="radio" name="alert_type" value="2" id="alert_type2"><span class="text_blue"> 2: Notice 24 hours before</span></label>
</p>
</td>
</tr>
<tr>
<th class="bg_gray"><label for="alert_level">alert_level</label></th>
<td>
<p>
<label for="alert_level1"><input type="radio" name="alert_level" value="1" id="alert_level1"><span class="text_red"> 1: important</span></label>
</p>
<p>
<label for="alert_level2"><input type="radio" name="alert_level" value="2" id="alert_level2" checked><span class="text_orange"> 2: normal</span></label>
</p>
<p>
<label for="alert_level3"><input type="radio" name="alert_level" value="3" id="alert_level3"><span class="text_green"> 3: light</span></label>
</p>
</td>
</tr>
<tr>
<th class="bg_gray"><label for="alert_public">alert_public</label></th>
<td>
<p>
<label for="alert_public1"><input type="radio" name="alert_public" value="1" id="alert_type1" checked><span class="text_blue"> 1: private</span></label>
</p>
<p>
<label for="alert_public2"><input type="radio" name="alert_public" value="2" id="alert_type2"><span class="text_blue"> 2: public</span></label>
</p>
</td>
</tr>
</table>
<p><button type="submit" class="button_orange">Set Alert</button></p>
</form>
</div>
</body>
</html>
わかってきました
Array ( [login] => 1 [u_name] => 123 [u_timezone] => 89 [u_icon] => 3 )
Notice: Undefined index: u_id in C:\xampp\htdocs\php\makino\alert\exec.php on line 25
$_SESSION["u_id"]はないから
またさっきのselectでu_idに相当するのをu_nameからもってくるやつだな
ぎゃー
session:Array ( [login] => 1 [u_name] => 123 [u_timezone] => 103 [u_icon] => 3 ) -----123PDOStatement Object ( [queryString] => SELECT u_id,u_name FROM users WHERE 123=u_id )
sessionはくるようになった
アラートが入らないな
以下次号
おもうにselect文が間違っててsessionのu_nameと一致するu_nameをとって、
fetch部分がu_idなのでは。