まず簡単なログイン画面をつくりました。そこから違うファイルを開いてMysqlのデータを取得したいのですが、うまくできません。
接続状態を維持すれば…などと思い継続接続できないか探していたのですが、できない状況です。
その方法、または他にいい方法などございますでしょうか?
ご回答お願いいたします…
現状まずFailed to load resource: the server responded with a status of 500 (Internal Server Error)で起動しません…
login.php
php
1<?php 2if(isset($_POST['login_username'])){ 3 if(isset($_POST['login_password'])){ 4 $login_name = $_POST['login_username']; 5 $login_pass = $_POST['login_password']; 6 try{ 7 $pdo = new PDO( 8 'mysql:host=localhost;dbname=testdb;charset=utf8', 9 $login_name, 10 $login_pass 11 ); 12 $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 13 $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); 14 $pdo->setAttribute(PDO::ATTR_PERSISTENT, true); 15 $connect_err = "false"; 16 }catch(PDOException $Exception){ 17 $login_pass = ''; 18 $login_name = ''; 19 $connect_err = "true"; 20 } 21 $errlog_json = json_encode($connect_err , JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT); 22 } 23} 24?> 25<!DOCTYPE html> 26<html> 27<head> 28 <title>ログイン</title> 29</head> 30<body> 31 <center><p style="margin:30px;"><font size="7">ログイン</font></p> 32 <form method="post"> 33 <div style="width:500px;height:250px; border: solid 1px;"> 34 <p style="margin:20px;">ユーザーネーム<br><input type="text" name="login_username" size="40"></p><br> 35 <p style="margin:20px;">パスワード<br><input type="password" name="login_password" size="40"></p> 36 </div> 37 <input type="submit" name="login"> 38 </form> 39 </center> 40 <script type="text/javascript"> 41 var phperr_connect = JSON.parse('<?php echo $errlog_json; ?>'); 42 if(phperr_connect == "true"){ 43 alert("正しいユーザーネーム、パスワードを入力してください"); 44 }else{ 45 window.location.href = 'main.php'; 46 } 47 </script> 48 49</body> 50</html>
main.php
php
1<!DOCTYPE html> 2<html> 3<head> 4 <title>メニュー選択</title> 5</head> 6<body> 7 <center><font size="7">艦娘一覧</font></center> 8 <?php 9 $sql = "SELECT * FROM testdb.test"; 10 $stmh = $pdo->prepare($sql); 11 $stmh->execute(); 12 ?> 13 <table><tbody> 14 <tr><th>ID</th><th>商品名</th><th>価格</th></tr> 15 <?php 16 while($row = $stmh->fetch(PDO::FETCH_ASSOC)){ 17 ?> 18 <tr> 19 <th><?=htmlspecialchars($row['id'])?></th> 20 <th><?=htmlspecialchars($row['name'])?></th> 21 <th><?=htmlspecialchars($row['price'])?></th> 22 </tr> 23</tbody></table> 24</body> 25</html>
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/11/01 15:37
2018/11/01 15:42
2018/11/01 15:52
2018/11/02 15:13