PHPをブラウザ上でプレビューすると、ソースのみが表示されてしまう。
ここに質問の内容を詳しく書いてください。
以前にPHPで本棚サイトを為すシステムを作り、動作も大丈夫でした。
久しぶりにcloud9を開いてプレビューをしたときに以下の問題が発生しました。
発生している問題・エラーメッセージ
プログラムの一切が表示されず、ソースコードがそのまま出力。
該当のソースコード
PHP
1<?php 2 function h($str) { 3 return htmlspecialchars($str, ENT_QUOTES, 'UTF-8'); 4 } 5 6 // MySQLサーバ接続に必要な値を変数に代入 7 $host = 'localhost'; 8 $username = 'root'; 9 $password = ''; 10 $db_name = 'bookshelf'; 11 12 // 変数を設定して、MySQLサーバに接続 13 $database = mysqli_connect($host, $username, $password, $db_name); 14 15 // 接続を確認し、接続できていない場合にはエラーを出力して終了する 16 if ($database == false) { 17 die('Connect Error (' . mysqli_connect_errno() . ') ' . mysqli_connect_error()); 18 } 19 20 // MySQL に utf8 で接続するための設定をする 21 $charset = 'utf8'; 22 mysqli_set_charset($database, $charset); 23 24 // ここにMySQLを使ったなんらかの処理を書く 25 26 // bookshelf_form.phpから送られてくる書籍データの登録 27 if ($_POST['submit_add_book']) { 28 // まずは送られてきた画像をuploadsフォルダに移動させる 29 $file_name = $_FILES['add_book_image']['name']; 30 $image_path = './uploads/' . $file_name; 31 move_uploaded_file($_FILES['add_book_image']['tmp_name'], $image_path); 32 33 // データベースに書籍を新規登録する 34 $sql = 'INSERT INTO books (title, image_url, status) VALUES(?, ?, "unread")'; 35 $statement = mysqli_prepare($database, $sql); 36 mysqli_stmt_bind_param($statement, 'ss', $_POST['add_book_title'], $image_path); 37 mysqli_stmt_execute($statement); 38 mysqli_stmt_close($statement); 39 } 40 41 // ステータス変更の処理 42 if ($_POST['submit_book_unread']) { 43 // 未読へ変更 44 $sql = 'UPDATE books SET status="unread" WHERE id=?'; // 実行するSQLを作成 45 $statement = mysqli_prepare($database, $sql); // セキュリティ対策をする 46 mysqli_stmt_bind_param($statement, 'i', $_POST['book_id']); // id=?の?の部分に代入する 47 mysqli_stmt_execute($statement); // SQL文を実行する 48 mysqli_stmt_close($statement); // SQL文を破棄する 49 } 50 elseif ($_POST['submit_book_reading']) { 51 // 読中へ変更 52 $sql = 'UPDATE books SET status="reading" WHERE id=?'; 53 $statement = mysqli_prepare($database, $sql); 54 mysqli_stmt_bind_param($statement, 'i', $_POST['book_id']); 55 mysqli_stmt_execute($statement); 56 mysqli_stmt_close($statement); 57 } 58 elseif ($_POST['submit_book_finished']) { 59 // 読了へ変更 60 $sql = 'UPDATE books SET status="finished" WHERE id=?'; 61 $statement = mysqli_prepare($database, $sql); 62 mysqli_stmt_bind_param($statement, 'i', $_POST['book_id']); 63 mysqli_stmt_execute($statement); 64 mysqli_stmt_close($statement); 65 } 66 67 if ($_POST['submit_book_delete']) { 68 //【課題】削除実行 69 $sql = 'DELETE FROM bookshelf.books WHERE id = ?'; 70 $statement = mysqli_prepare($database, $sql); 71 mysqli_stmt_bind_param($statement, 'i', $_POST['book_id']); 72 mysqli_stmt_execute($statement); 73 mysqli_stmt_close($statement); 74 } 75 // 未読数のカウント 76 $sql = 'SELECT COUNT(*) as count FROM books where status = "unread"'; 77 $result = mysqli_query($database, $sql); 78 $record = mysqli_fetch_assoc($result); 79 $count_unread = $record['count']; 80 // 読中数のカウント 81 $sql = 'SELECT COUNT(*) as count FROM books where status = "reading"'; 82 $result = mysqli_query($database, $sql); 83 $record = mysqli_fetch_assoc($result); 84 $count_reading = $record['count']; 85 // 読了数のカウント 86 $sql = 'SELECT COUNT(*) as count FROM books where status = "finished"'; 87 $result = mysqli_query($database, $sql); 88 $record = mysqli_fetch_assoc($result); 89 $count_finished = $record['count']; 90 91 // どれも$resultにレコードが代入されるので、どの場合でも$resultを使う 92 if ($_POST['submit_only_unread']) { 93 // 未読ステータスの書籍だけを取得する 94 $sql = 'SELECT * FROM books WHERE status="unread" ORDER BY created_at DESC'; 95 } 96 elseif ($_POST['submit_only_reading']) { 97 // 読中ステータスの書籍だけを取得する 98 $sql = 'SELECT * FROM books WHERE status="reading" ORDER BY created_at DESC'; 99 } 100 elseif ($_POST['submit_only_finished']) { 101 // 読了ステータスの書籍だけを取得する 102 $sql = 'SELECT * FROM books WHERE status="finished" ORDER BY created_at DESC'; 103 } 104 else { 105 // 登録されている書籍を全て取得する 106 $sql = 'SELECT * FROM books ORDER BY created_at DESC'; 107 $statement = mysqli_prepare($database, $sql); 108 mysqli_stmt_bind_param($statement, 'i', $_POST['book_id']); 109 mysqli_stmt_execute($statement); 110 mysqli_stmt_close($statement); 111 } 112 113 // if-elseif-else 文なので、 $sql には必ず上記いずれかの SQL 文が入る 114 // いずれかの $sql を実行して $result に代入する 115 $result = mysqli_query($database, $sql); 116 117 // MySQLを使った処理が終わると、接続は不要なので切断する 118 mysqli_close($database); 119 120?> 121<!DOCTYPE html> 122<html lang="ja"> 123 <head> 124 <meta charset="utf-8"> 125 <title>Bookshelf | カンタン!あなたのオンライン本棚</title> 126 <link rel="stylesheet" href="bookshelf.css"> 127 </head> 128 <body> 129 <header> 130 <div id="header"> 131 <div id="logo"> 132 <a href="./bookshelf_index.php"><img src="./images/logo.png" alt="Bookshelf"></a> 133 </div> 134 <nav> 135 <a href="./bookshelf_form.php"><img src="./images/icon_plus.png" alt=""> 書籍登録</a> 136 </nav> 137 </div> 138 </header> 139 <div id="cover"> 140 <h1 id="cover_title">カンタン!あなたのオンライン本棚</h1> 141 <form action="bookshelf_index.php" method="post"> 142 <div class="book_status unread active"> 143 <input type="submit" name="submit_only_unread" value="未読"> 144 <div class="book_count"><?php print h($count_unread); ?></div> 145 </div> 146 <div class="book_status reading active"> 147 <input type="submit" name="submit_only_reading" value="読中"><br> 148 <div class="book_count"><?php print h($count_reading); ?></div> 149 </div> 150 <div class="book_status finished active"> 151 <input type="submit" name="submit_only_finished" value="読了"><br> 152 <div class="book_count"><?php print h($count_finished); ?></div> 153 </div> 154 </form> 155 </div> 156 <div class="wrapper"> 157 <div id="main"> 158 <div id="book_list" class="clearfix"> 159<?php 160 if ($result) { 161 while ($record = mysqli_fetch_assoc($result)) { 162 // 1レコード分の値をそれぞれ変数に代入する 163 $id = $record['id']; 164 $title = $record['title']; 165 $image_url = $record['image_url']; 166 $status = $record['status']; 167?> 168 <div class="book_item"> 169 <div class="book_image"> 170 <img src="<?php print h($image_url); ?>" alt=""> 171 </div> 172 <div class="book_detail"> 173 <div class="book_title"> 174 <?php print h($title); ?> 175 </div> 176 <form action="bookshelf_index.php" method="post"> 177 <input type="hidden" name="book_id" value="<?php print h($id); ?>"> 178 <div class="book_status unread <?php if ($status == "unread") print "active"; ?>"> 179 <input type="submit" name="submit_book_unread" value="未読"> 180 </div> 181 <div class="book_status reading <?php if ($status == "reading") print "active"; ?>"> 182 <input type="submit" name="submit_book_reading" value="読中"> 183 </div> 184 <div class="book_status finished <?php if ($status == "finished") print "active"; ?>"> 185 <input type="submit" name="submit_book_finished" value="読了"> 186 </div> 187 </form> 188 <form action="bookshelf_index.php" method="post"> 189 <input type="hidden" name="book_id" value="<?php print h($id); ?>"> 190 <div class="book_delete"> 191 <input type="submit" name="submit_book_delete" value="削除する"><img src="images/icon_trash.png" alt="icon trash"> 192 </div> 193 </form> 194 </div> 195 </div> 196<?php 197 } 198 mysqli_free_result($result); 199 } 200?> 201 </div> 202 </div> 203 </div> 204 <footer> 205 <small>© 2016 Bookshelf.</small> 206 </footer> 207 </body> 208</html> 209
試したこと
cloud9、Mysqlの再起動。
cloud9上とブラウザの別タブでプレビュー
補足情報(FW/ツールのバージョンなど)
あなたの回答
tips
プレビュー