vi /var/www/html/book/insert.html
<!doctype html>
<html>
<head>
<meta charset=”UTF-8″>
<title>How To Create A Database Search With MySQL & PHP Script</title>
<link href=”css/bootstrap.min.css” rel=”stylesheet”>
<script src=”js/bootstrap.min.js”></script>
</head>
<body>
<div class=”container”>
<h3>書籍のデーターベース</h3>
<form class=”form-horizontal” name=”insert” action=”insert.php” method=”POST”>
<div class=”form-group”>
<label class=”col-sm-1 control-label”>本の題名:</label>
<div class=”col-sm-5″>
<input type=”text” class=”form-control” name=”title” placeholder=”本の題名”>
</div>
</div>
<div class=”form-group”>
<label class=”col-sm-1 control-label”>著者:</label>
<div class=”col-sm-2″>
<input type=”text” class=”form-control” name=”author” placeholder=”著者”>
</div>
</div>
<div class=”form-group”>
<label class=”col-sm-1 control-label”>価格:</label>
<div class=”col-sm-1″>
<input type=”text” class=”form-control” name=”price” placeholder=”価格”>
</div>
</div>
<div class=”form-group”>
<div class=”col-sm-offset-1 col-sm-5″>
<button type=”submit” class=”btn btn-default”>送信</button>
</div>
</div>
</form>
</div>
</body>
</html>
vi /var/www/html/book/insert.php
<?php
$servername = “localhost”;
$username = “user”;
$password = “パスワード”;
$dbname = “book”;
$title = $_POST[‘title’];
$author = $_POST[‘author’];
$price = $_POST[‘price’];
try {
$conn = new PDO(“mysql:host=$servername;dbname=$dbname”, $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = “INSERT INTO book (title, author, price)
VALUES (‘$title’, ‘$author’, ‘$price’)”;
// use exec() because no results are returned
$conn->exec($sql);
echo “New record created successfully”;
}
catch(PDOException $e)
{
echo $sql . “<br>” . $e->getMessage();
}
$conn = null;
?>
コメント