さくらサーバを申し込んだら
レッツエンクリプトをするといい(無料)
というかやるべきらしい。
*2週間のお試し期間が終わったらすぐ申し込む
クリプトの暗号化をつかったSSL HTTPS
CSS Grid Layout
先週のlunchのつづき
ここまで
form.php
<?php
//新規でも編集でもこのファイルを使いたい
require_once("class/lunch.class.php");
$obj = new Lunch();
if(!empty($_GET["id"])){
//編集の場合
//getで送られてくるidを事前につかまえる
$id=intval($_GET["id"]);
$title="編集";//タイトルも分岐によって変わる
$arr = $obj->getMenuById($id);
$menu = htmlspecialchars($arr["menu"],ENT_QUOTES);
$price = $arr["price"];
$c_id = $arr["c_id"];
}else{
//新規作成の場合
$title="新規作成";
$menu = "";
$price ="";
$c_id="";
}
?>
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>メニュー<?php echo $title; ?></title>
<link rel="stylesheet" href="css/style.css">
<script src="../jquery-3.4.1.min.js"></script>
</head>
<body>
<div id="container">
<h1>メニュー<?php echo $title; ?></h1>
<form action="exec.php" method="post">
<table>
<tr>
<th><label for="menu">メニュー</label></th>
<td><input type="text" name="menu" id="menu" value="<?php echo $menu; ?>"></td>
</tr>
<tr>
<th><label for="price">価格</label></th>
<td><input type="text" name="price" id="price" value="<?php echo $price; ?>"></td>
</tr>
<tr>
<th><label for="c_id">カテゴリ</label></th>
<td>
セレクト予定
</td>
</tr>
</table>
<p><input type="submit" value="編集"></p>
</form>
<script src="js/lunch.js"></script>
</body>
</html>
style.php
body{
background:#eff;
}
#container{
width:80%;
margin:auto;
}
table{
border-collapse:collapse;
}
th,td{
padding:0.3rem 0.5rem;
text-align:left;
background:#fff;
border:solid 1px #555;
}
th{
background:#ccc;
}
この2つは先週から変化なし
lunch.class.php
<?php
class Lunch{
private $pdo;//外から直接アクセスはできないように
public function __construct(){//コンストラクタ。初期化されたときに動く
// -----------------
//環境によって変更
// -----------------
$host="localhost";
$dbuser="root";
$dbpass="";
$dbname="lunch";
// -----------------
$dsn = "mysql:host={$host}; dbname={$dbname}; charset=utf8";
$this->pdo=new PDO($dsn,$dbuser,$dbpass); //$pdoとは書かない
}//constructの終わり
public function getAllMenu(){//publicでないと外から呼び出せない
$sql = "SELECT * FROM foods";
$rs = $this->pdo->query($sql);
$rows = $rs->fetchAll(PDO::FETCH_ASSOC);
return $rows;//戻り値
}
public function getMenubyId($id=1){//指定したid番号を取得、引き取るid番号が仮引数の$id
$sql="SELECT * FROM foods WHERE id=:id";
$stmt = $this->pdo->prepare($sql);
$stmt->bindvalue(":id",$id,PDO::PARAM_INT);
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
return $row;
}
public function setMenu($menu,$price,$c_id){//idはオートインクリメントなので必要ない
$sql = "INSERT INTO foods(menu,price,c_id) VALUES (:menu,:price,:c_id)";//メニューと値段とカテゴリidを追加
$stmt = $this->pdo->prepare($sql);//クラス内の変数なのでstmtやsqlのスコープはここだけ
$stmt->bindvalue(":menu",$menu,PDO::PARAM_STR);
$stmt->bindvalue(":price",$price,PDO::PARAM_INT);
$stmt->bindvalue(":c_id",$c_id,PDO::PARAM_INT);
$stmt->execute();
//戻り値は要らない
}
public function updateMenu($menu,$price,$c_id,$id){//どれがupdateされるか分からないので3つとも仮引数を用意
//主キーである$idがないと全部書き換わってしまうのでどこのレコードか指定するため今回4つプロパティが要る
$sql = "UPDATE foods SET menu=:menu, price=:price, c_id=:c_id WHERE id=:id";
$stmt = $this->pdo->prepare($sql);
$stmt->bindvalue(":menu",$menu,PDO::PARAM_STR);
$stmt->bindvalue(":price",$price,PDO::PARAM_INT);
$stmt->bindvalue(":c_id",$c_id,PDO::PARAM_INT);
$stmt->bindvalue(":id",$id,PDO::PARAM_INT);//idも必要ですよ
$stmt->execute();
}
public function deleteMenu($id){//パラメータは主キーであるidだけでいい
$sql = "DELETE FROM foods WHERE id=:id";
$stmt = $this->pdo->prepare($sql);
$stmt->bindvalue(":id",$id,PDO::PARAM_INT);
$stmt->execute();
}
}
?>
lunch.php
$(function(){
console.log("hoge");
});