PHP ショッピングサイト作成

![20170228.png](https://qiita-image-store.s3.amazonaws.com/0/95187/4dbf8e7c-4d13-9402-1f1e-16416b3ef376.png)


```sql:shp.sql
drop database if exists shop;
create database shop default character set utf8 collate utf8_general_ci;
grant all on shop.* to 'staff'@'localhost' identified by 'password';
use shop;

create table product (
	id int auto_increment primary key, 
	name varchar(200) not null, 
	price int not null
);

create table customer (
	id int auto_increment primary key, 
	name varchar(100) not null, 
	address varchar(200) not null, 
	login varchar(100) not null unique, 
	password varchar(100) not null
);

create table purchase (
	id int not null primary key, 
	customer_id int not null, 
	foreign key(customer_id) references customer(id)
);

create table purchase_detail (
	purchase_id int not null, 
	product_id int not null, 
	count int not null, 
	primary key(purchase_id, product_id), 
	foreign key(purchase_id) references purchase(id), 
	foreign key(product_id) references product(id)
);

create table favorite (
	customer_id int not null, 
	product_id int not null, 
	primary key(customer_id, product_id), 
	foreign key(customer_id) references customer(id), 
	foreign key(product_id) references product(id)
);

insert into product values(null, '松の実', 700);
insert into product values(null, 'くるみ', 270);
insert into product values(null, 'ひまわりの種', 210);
insert into product values(null, 'アーモンド', 220);
insert into product values(null, 'カシューナッツ', 250);
insert into product values(null, 'ジャイアントコーン', 180);
insert into product values(null, 'ピスタチオ', 310);
insert into product values(null, 'マカダミアナッツ', 600);
insert into product values(null, 'かぼちゃの種', 180);
insert into product values(null, 'ピーナッツ', 150);
insert into product values(null, 'クコの実', 400);

insert into customer values(null, '熊木 和夫', '東京都新宿区西新宿2-8-1', 'kumaki', 'BearTree1');
insert into customer values(null, '鳥居 健二', '神奈川県横浜市中区日本大通1', 'torii', 'BirdStay2');
insert into customer values(null, '鷺沼 美子', '大阪府大阪市中央区大手前2', 'saginuma', 'EgretPond3');
insert into customer values(null, '鷲尾 史郎', '愛知県名古屋市中区三の丸3-1-2', 'washio', 'EagleTail4');
insert into customer values(null, '牛島 大悟', '埼玉県さいたま市浦和区高砂3-15-1', 'ushijima', 'CowIsland5');
insert into customer values(null, '相馬 助六', '千葉県地足中央区市場町1-1', 'souma', 'PhaseHorse6');
insert into customer values(null, '猿飛 菜々子', '兵庫県神戸市中央区下山手通5-10-1', 'sarutobi', 'MonkeyFly7');
insert into customer values(null, '犬山 陣八', '北海道札幌市中央区北3西6', 'inuyama', 'DogMountain8');
insert into customer values(null, '猪口 一休', '福岡県福岡市博多区東公園7-7', 'inokuchi', 'BoarMouse9');
```

###**ログイン機能**

```php:login-input.php
<?php require 'header.php'; ?>
<?php require 'menu.php'; ?>
<form action="login-output.php" method="post">
ログイン名<input type="text" name="login"><br>
パスワード<input type="password" name="password"><br>
<input type="submit" value="ログイン">
</form>
<?php require 'footer.php'; ?>
```

```php:login-output.php
<?php require 'header.php'; ?>
<?php require 'menu.php'; ?>
<?php
session_start ();
// unset→指定した変数を破棄する
unset ( $_SESSION ['customer'] );
$pdo = new PDO ( 'mysql:host=localhost;dbname=takahashi1;charset=utf8', 'takahashi', 'takahashi' );
$stmt = $pdo->prepare ( 'select * from customer where login=? and password=?' );
// var_dump($stmt);
$stmt->execute ( array (
		$_POST ['login'],
		$_POST ['password']
) );

// foreachは配列とオブジェクトをループして使うための構文
// 配列ではないが使いやすくするために特別にforeachの反復処理で使えるように作られている
// $rowという変数に入れる
// $rows = $stmt->fetchAll ();
// var_dump($rows)
foreach ( $stmt->fetchAll () as $row ) {
// foreach ( $rows as $row ) {
	// 連想配列$_SESSION['customer']にキーを指定して追加し、値として連想配列を代入
	$_SESSION ['customer'] = array (
			// idというキーで$row['id']を入れる
			'id' => $row ['id'],
			'name' => $row ['name'],
			'address' => $row ['address'],
			'login' => $row ['login'],
			'password' => $row ['password']
	);
}
if (isset ( $_SESSION ['customer'] )) {
	echo 'いらっしゃいませ、', $_SESSION ['customer'] ['name'], 'さん。';
} else {
	echo 'ログイン名またはパスワードが違います。';
}
// var_dump($_SESSION['customer']);
?>
<?php require 'footer.php'; ?>
```

![20170310.png](https://qiita-image-store.s3.amazonaws.com/0/95187/3c1d5397-0f76-5c7b-15e9-fa692e20ad9c.png)

![201703101.png](https://qiita-image-store.s3.amazonaws.com/0/95187/797a6a21-e2ad-fb5e-fbf4-da486ff6fca1.png)

##**ログアウト機能**

```php:logout-input.php
<?php require 'menu.php';?>
<p>ログアウトしますか？</p>
<a href="logout-output.php">ログアウト</a>
```

```php:logout-output.php
<?php require 'menu.php';?>
<?php
session_start ();
if (isset ( $_SESSION ['customer'] )) {
	unset ( $_SESSION ['customer'] );
	echo 'ログアウトしました。';
} else {
	echo 'すでにログアウトしています。';
}
?>
```

![201703102.png](https://qiita-image-store.s3.amazonaws.com/0/95187/a306d380-95f5-ff6a-d927-0cec3d65689d.png)
![201703103.png](https://qiita-image-store.s3.amazonaws.com/0/95187/183f4889-4896-0a7e-6236-91e665f569bc.png)
![201703104.png](https://qiita-image-store.s3.amazonaws.com/0/95187/f3e33e7a-586f-b7eb-e292-5a2bbaa3c00e.png)