サーラリマンは働き過ぎ、社畜化に注意しよう!!

mysql php pdo insert

mysql
スポンサーリンク

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”>

<h2>Enter information regarding book</h2>
<ul>
<form name=”insert” action=”insert.php” method=”POST” >
<li>Book Name:</li><li><input type=”text” name=”title” /></li>
<li>Author:</li><li><input type=”text” name=”author” /></li>
<li>Price (USD):</li><li><input type=”text” name=”price” /></li>
<li><input type=”submit” /></li>
</form>
</ul>
</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;
?>

mysqlphp
スポンサーリンク
シェアする
ふじやんをフォローする
なんでもDIY

コメント