Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

board2 (平井杏子) #20

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
The diff you're trying to view is too large. We only load the first 3000 changed files.
6 changes: 6 additions & 0 deletions board1/db_connection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
try {
$pdo=new PDO('mysql:host=localhost;dbname=board1_db;charset=utf8','root', '');
} catch ( PDOException $e ) {
print "接続エラー:{$e->getMessage()}";
}
41 changes: 41 additions & 0 deletions board1/insert.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
/**
* Created by PhpStorm.
* User: kyoko
* Date: 2017/12/05
* Time: 18:05
*/
$name=$_POST['name'];
$contents=$_POST['contents'];

if($name==''){
session_start();
$_SESSION['error']='名前を入れてください';
header('Location: http://localhost/tech-aca5_kyoko_hirai/board1/show.php');
exit;
}elseif($contents==''){
session_start();
$_SESSION['error']='投稿を入力してください';
header('Location: http://localhost/tech-aca5_kyoko_hirai/board1/show.php');
exit;
}else{
$_SESSION['error']=null;
//接続
require('db_connection.php');
Copy link

@NaoyaNishioka NaoyaNishioka Jan 17, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

requireはファイルの戦闘にまとめてかくようにして、何をつかっているかまとめて管理できるようにしときましょう。


try {
//insert
$stt = $pdo->prepare("insert into post_table(name, contents) values(:name, :contents)");
$stt->bindValue(':name', $name);
$stt->bindValue(':contents', $contents);
$stt->execute();
} catch (PDOException $e) {
print "エラー:{$e->getMessage()}";
}

//接続を切る
$pdo = null;

header('Location: http://localhost/tech-aca5_kyoko_hirai/board1/show.php');
exit;
}
41 changes: 41 additions & 0 deletions board1/show.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<!doctype html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>ESS1</title>
</head>
<body>
<h2>掲示板1</h2><br>
<?php
session_start();
//var_dump($_SESSION['error']);
if (isset($_SESSION['error'])){
print $_SESSION['error'];
}
?>
<form method="post" action="insert.php">
名前:<input type="text" name="name"><br>
投稿:<br>
<textarea name="contents" row="8" cols="40"></textarea><br>
<input type="submit">
</form>

</body>
</html>

<?php
//接続
require('db_connection.php');

try {
$statement = $pdo->query("select * from post_table");
//var_dump($stt);
foreach ($statement as $row) {
echo $row['id'].':'.$row['name'].'<br>';
echo $row['contents'];
echo '<br>';
//var_dump($row);
}
} catch ( PDOException $e ) {
print "エラー:{$e->getMessage()}";
}
6 changes: 6 additions & 0 deletions board2/db_connection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
try {
$pdo=new PDO('mysql:host=localhost;dbname=board2_db;charset=utf8','root', '');
} catch ( PDOException $e ) {
print "接続エラー:{$e->getMessage()}";
}
56 changes: 56 additions & 0 deletions board2/login.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

require_once './smarty/Smarty.class.php';

$smarty = new Smarty();
$smarty -> template_dir = 'templates/';
$smarty -> compile_dir = 'templates_c/';


if (isset($_POST['login'])){

$name = $_POST['name'];
$password = $_POST['password'];


if ($name === '') {

$smarty -> assign('error' , 'ユーザー名を入力してください');

} elseif ($password === '') {

$smarty -> assign('error' , 'パスワードを入力してください。');

}else{

try {

require('db_connection.php');


$users = $pdo->query("SELECT * from member_table");

session_start();
$_SESSION['username'] = null;
$_SESSION['id'] = null;
$smarty -> assign('error' , 'ユーザ名かパスワードが違います。');

foreach ($users as $user) {

if($name === $user['name'] and $password === $user['password']) {

$_SESSION['username'] = $user['name'];
$_SESSION['id'] = $user['id'];
header('Location: ./show.php');
exit;

}
//var_dump($_SESSION['username']);
}
} catch (PDOException $e) {
echo $e->getMessage();
}
}
}

$smarty->display('login.html');
6 changes: 6 additions & 0 deletions board2/logout.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
session_start();
$_SESSION['username']=null;
$_SESSION['id']=null;
header('Location: ./show.php');
exit;
57 changes: 57 additions & 0 deletions board2/post_update.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

require_once './smarty/Smarty.class.php';

$smarty = new Smarty();
$smarty -> template_dir = 'templates/';
$smarty -> compile_dir = 'templates_c/';


require('db_connection.php');

if(isset($_POST['update'])){

try{

$post = $pdo->prepare('UPDATE post_table set contents=:contents where id = :id');
$post -> bindValue(':id' ,(int) $_POST['id'], PDO::PARAM_INT);
$post -> bindValue(':contents' ,$_POST['contents']);
$post -> execute();
header('Location: ./show.php');
exit;

} catch ( PDOException $e ) {
print "エラー:{$e->getMessage()}";
}

}elseif (isset($_POST['delete'])){

try{

$post = $pdo->prepare('DELETE FROM post_table where id = :id');
$post -> bindValue(':id' ,(int) $_POST['id'], PDO::PARAM_INT);
$post -> execute();
header('Location: ./show.php');
exit;

} catch ( PDOException $e ) {
print "エラー:{$e->getMessage()}";
}

}else{

try{

$post = $pdo->prepare('SELECT contents FROM post_table where id = :id');
$post -> bindValue(':id' ,(int) $_POST['id'], PDO::PARAM_INT);
$post -> execute();
$contents = $post -> fetch();

} catch ( PDOException $e ) {
print "エラー:{$e->getMessage()}";
}
$smarty->assign('id', $_POST['id']);
$smarty->assign('contents', $contents);
}

$smarty->display('post_update.html');
83 changes: 83 additions & 0 deletions board2/show.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

require_once './smarty/Smarty.class.php';

$smarty = new Smarty();
$smarty -> template_dir = 'templates/';
$smarty -> compile_dir = 'templates_c/';

session_start();

if(isset($_POST['insert'])) {

$id = $_SESSION['id'];
$contents = $_POST['contents'];


if ($contents == '') {

$smarty->assign('error', '投稿を入力してください');

} else {

require('db_connection.php');

try {

//insert
$post = $pdo->prepare("insert into post_table(user_id, contents) values(:user_id, :contents)");
$post->bindValue(':user_id', $id);
$post->bindValue(':contents', $contents);
$post->execute();

} catch (PDOException $e) {
print "エラー:{$e->getMessage()}";
}

//接続を切る
$pdo = null;

}
}


if(isset($_SESSION['username'])) {
$smarty -> assign('name',$_SESSION['username']);
}

require('db_connection.php');

try {

$posts = $pdo->query("select * from post_table");
$posts = $posts ->fetchAll(PDO::FETCH_ASSOC);

foreach ($posts as $post) {

//post_tableのuser_idからmember_tableのnameを引き出す
$sql = 'SELECT name FROM member_table where id = :user_id';
$name = $pdo->prepare($sql);
$name -> bindValue(':user_id' ,(int) $post['user_id'], PDO::PARAM_INT);
$name -> execute();
$username = $name -> fetch(PDO::FETCH_ASSOC);
//var_dump($username);

$post['name'] = $username['name'];

$allposts[] = array($post);
}

//var_dump($allposts);
$smarty -> assign('posts',$allposts);


if(isset($_SESSION['id'])) {
$smarty->assign('user_id', $_SESSION['id']);
}

} catch ( PDOException $e ) {
print "エラー:{$e->getMessage()}";
}


$smarty->display('show.html');
Loading