connet.lolipop.jp
データベースヘ接続
1. Sqliteを読み込みます。この時DBファイルの存在確認をします,存在しない場合は、 作成してくれますので大変便利で楽ですが、ただし、”IF NOT EXISTS”文の挿入は必須になります。
2. DBファイルの読み込みに成功すると、DBのオブジェクトを返します。
3. DBファイルのパスは任意に設定して下さい。ここでは、プロジェクトと同一パスにしてます。

personDB.php
<?php
// 接続
function executeQuery(){
  try {
  	// DBが存在しない場合は作成
  	$db = new PDO('sqlite:person.db');	
  	// TABLEが存在しない場合は作成(”IF NOT EXISTS”を挿入)
  	$db->exec("CREATE TABLE IF NOT EXISTS persontbl (id INTEGER PRIMARY KEY,name text, address text, email text, phone_number text, memo text, created_at timestamp NOT NULL default (datetime(CURRENT_TIMESTAMP,'localtime')), updated_at timestamp NOT NULL default (datetime(CURRENT_TIMESTAMP,'localtime')))");
  	return $db;	
  } catch (Exception $e) {
      echo 'Error!';
  }
}
?>
全件データ読み込み
DBオブジェクトをもとに、全てのデータを取得・表示します。

select.php
<?php
// ファイルを読込む(読込みは一回のみ)
require_once("personDB.php");

// データベース(person.db)のオブジェクトを取得
$db = executeQuery();

$sql = "SELECT * FROM persontbl";
$result = $db->query($sql);
$rowVar = '';
if(!$result){
  echo 'Error!';
} else {
	foreach ($result as $row) {
	  $rowVar .= "<tr>";
	  $rowVar .= "<td>".$row["id"]."</td><td>".$row["name"]."</td><td>".$row["address"]."</td><td>".$row["email"]."</td><td>".$row["phone_number"]."</td><td>".$row["memo"]."</td><td>".$row["created_at"]."</td><td>".$row["updated_at"]."</td>";
	  $rowVar .= "<td class=\"updel\"><a href=\"update.php?id=".$row["id"]."\">Edit</a></td>";
	  $rowVar .= "<td class=\"updel\"><a href=\"delete.php?id=".$row["id"]."\" onclick=\"return recdelChk(".$row["id"].");\">Destroy</a></td>";
	  $rowVar .= "</tr>";
	}
}
// データベースから接続を解放して、データへの操作を再実行可能にする
$result->closeCursor();
?>

<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>全件データ読み込み</title>
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/v/bs4-4.1.1/jq-3.3.1/dt-1.10.18/sl-1.2.6/datatables.min.css"/>
<script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/v/bs4-4.1.1/jq-3.3.1/dt-1.10.18/sl-1.2.6/datatables.min.js"></script>						
<script type="text/javascript">
  function recdelChk (id) {
   // 確認ダイアログ表示
   var id;
   var flag = confirm ("[ID="+id+"]番のデータを削除してもよろしいですか?\n\n削除したくない場合は[キャンセル]ボタンを押して下さい");
   // falg が TRUEなら送信、FALSEなら送信しない
   return flag;
  }
</script>						
<style>
body {
  font-family: Arial, Helvetica, sans-serif;
  padding: 10px 20px;
  color: #333;
  background-color:#FCF7E7;
}
.entlink {
  margin:20px;
}
.entlink a {
  font-size:1.1rem;
  color:#BC0000;
  text-decoration:underline;
}
.updel a {
  font-size:1.0rem;
  color:#BC0000;
  text-decoration:underline;
}
.updel a:hover {
  color:#F80000;
  text-decoration:none;
}
</style>
</head>
<body>
<h1>Address table -YetiTable Page-</h1><br />
<div>
<table id="example" class="table table-striped table-bordered nowrap" width="100%">
  <thead>
    <tr>
      <th>ID</th>
      <th>NAME</th>
      <th>ADDRESS</th>
      <th>EMAIL</th>
      <th>TEL</th>
      <th>MEMO</th>
      <th>CREATED</th>
      <th>UPDATED</th>
      <th width="10" style="background:#828282;"> </th>
      <th width="20" style="background:#828282;"> </th>
    </tr>
  </thead>
  <tfoot></tfoot>	
  <tbody>
  	<?= $rowVar ?>
  </tbody>
</table>
</div>
<div class="entlink">
<a href="insert.html">Create</a><br />
<a href="index.html">Index</a>
</div>
<script type="text/javascript">
	$(document).ready( function () {
	var table = $('#example').DataTable();
	});
</script>
</body>
</html>
登録フォーム
ここでは、IDとCREATED(timestamp)の登録はしません。

insert.html
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>登録フォーム</title>
<style>
body {
  font-family: Arial, Helvetica, sans-serif;
  padding: 10px 20px;
  color: #333;
  background:#FCF7E7;
}
a {
  font-size:1.1rem;
  color:#BC0000;
  text-decoration:underline;
}
.inpsubmit {
  margin-top:10px;
  margin-left:-5px;
  border:none;
  font-size:1.0rem;
  color:#BC0000;
  background:transparent;
  text-decoration:underline;
  cursor:pointer;
}
</style>
</head>
<body>
<h1>Registration</h1>
<form id="contact-form" name="inpform" action="insert2.php" method="POST">
  <table>
    <tr><td>NAME:</td><td><input type="text" name="name" value="" required></td></tr>
    <tr><td>ADDRESS:</td><td><input type="text" name="address" value=""></td></tr>
    <tr><td>EMAIL:</td><td><input type="text" name="email" value=""></td></tr>
    <tr><td>TEL:</td><td><input type="text" name="phone_number" value=""></td></tr>
    <tr><td>MEMO:</td><td><input type="text" name="memo" value=""></td></tr>
  </table>
  <input type="submit" value="Create" class="inpsubmit">
</form>
<br />
<div>
<a href="select.php">Address table</a><br /> 
<a href="index.html">Index</a>
</div>
</body>
</html>
DBへ登録
登録フォームの入力データを取得して、DBへコミットします。

insert2.php
<?php
require_once("personDB.php");

$name = htmlspecialchars($_POST['name'], ENT_QUOTES, 'UTF-8');
$address = htmlspecialchars($_POST['address'], ENT_QUOTES, 'UTF-8');
$email = htmlspecialchars($_POST['email'], ENT_QUOTES, 'UTF-8');
$phone_number = htmlspecialchars($_POST['phone_number'], ENT_QUOTES, 'UTF-8');
$memo = htmlspecialchars($_POST['memo'], ENT_QUOTES, 'UTF-8');

$db = executeQuery();
//トランザクションの開始
$db->exec('BEGIN');
try {
  $stmt = $db->prepare('insert into persontbl (name, address, email, phone_number, memo) values(?, ?, ?, ?, ?)');
  $stmt->bindParam(1,$name);
  $stmt->bindParam(2,$address);
  $stmt->bindParam(3,$email);
  $stmt->bindParam(4,$phone_number);
  $stmt->bindParam(5,$memo);
	
  $result = $stmt->execute();
  $db->exec('COMMIT');
} catch (Exception $e) {
    $db->exec('ROLLBACK');
    echo 'Error!';
}
?>

<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>登録</title>
<style>
body {
  font-family: Arial, Helvetica, sans-serif;
  padding: 10px 20px;
  color: #333;
  background:#FCF7E7;
}
div a {
  font-size:1.1rem;
  color:#BC0000;
  text-decoration:underline;
}
</style>
</head>
<body>
  <h1>finished registration</h1>
  <table>
  <tr><td>NAME:</td><td><input type="text" name="name" value="<?php echo $name ?>"></td></tr>
  <tr><td>ADDRESS:</td><td><input type="text" name="address" value="<?php echo $address ?>"></td></tr>
  <tr><td>EMAIL:</td><td><input type="text" name="email" value="<?php echo $email ?>"></td></tr>
  <tr><td>TEL:</td><td><input type="text" name="phone" value="<?php echo $phone_number ?>"></td></tr>
  <tr><td>MEMO:</td><td><input type="text" name="memo" value="<?php echo $memo ?>"></td></tr>
  </table>
  <br /><br />
  <div>
  <a href="select.php">Address table</a><br />
  <a href="index.html">Index</a>
  </div>
</body>
</html>
Search
Google


↟ このページの先頭へ