반응형
웹 서버 설정 시 사용한 코드를 업로드한다. 2대의 웹 서버 가상머신에 대하여 각각 일치하는 테이블 명으로 변경한 뒤에 사용하면 된다.
[index.php]
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" href="cssexample.css">
<title>Sample Form</title>
</head>
<body>
<h3>Name and Email Form</h3>
<p>This form will send the following data to the database server.</p>
<?php if(isset($_GET['success']) && $_GET['success']==false): ?>
<p class="error">ERROR! Please try again.</p>
<?php elseif(isset($_GET['success']) && $_GET['success']==true): ?>
<p class="success">Your data has been successfully submitted to the database server.</p>
<?php endif; ?>
<form method="POST" action="/process.php">
<label>First Name:</label>
<input type="text" name="first">
<br />
<label>Last Name:</label>
<input type="text" name="last">
<br />
<label>Email:</label>
<input type="text" name="email">
<br />
<input type="submit" value="Submit">
</form>
<a href="show.php">view the data</a>
</body>
</html>
[process.php]
<?php
$username = 'root';
$password = 'rootpass';
$dsn = 'mysql:host=10.1.3.4;dbname=sample';
try{
$db = new PDO($dsn, $username, $password);
$result=FALSE;
if($_POST['first']!=null && $_POST['last']!=null && $_POST['email']!=null)
{
$first=filter_var(trim($_POST['first']),FILTER_SANITIZE_SPECIAL_CHARS);
$last=filter_var(trim($_POST['last']),FILTER_SANITIZE_SPECIAL_CHARS);
$email=filter_var(trim($_POST['email']),FILTER_SANITIZE_EMAIL);
if(validSubmission($first,$last,$email))
$result=insertInfo($db,$first,$last,$email);
}
} catch(PDOException $ex) {
echo $ex->getMessage();
}
header('Location: index.php?success='.$result);
//Return true if user info is valid (not empty, email syntax okay)
function validSubmission($first,$last,$email)
{
$result=FALSE;
if(nonempty($first) && nonempty($last) && nonempty($email) && validEmail($email))
$result=TRUE;
return $result;
}
//Return true if email contains '@' and at least one '.' after '@'
function validEmail($email)
{
$valid=FALSE;
$position=strpos($email,'@');
if($position!=FALSE && strpos(substr($email,$position),'.')!=FALSE) //false if @ is first char too
$valid=TRUE;
return $valid;
}
//Return true if input is not null and not an empty string
function nonempty($input)
{
$nonempty=FALSE;
if($input!=null && $input!='')
$nonempty=TRUE;
return $nonempty;
}
//Insert entered form info into the database
function insertInfo($db,$first,$last,$email)
{
$stmt = $db->prepare("INSERT INTO response1 (firstname, lastname, email) VALUES (:first, :last, :email)");
$stmt->bindParam(':first', $first);
$stmt->bindParam(':last', $last);
$stmt->bindParam(':email', $email);
return $stmt->execute();
}
?>
[show.php]
<?php
$username = 'root';
$password = 'rootpass';
$dsn = 'mysql:host=10.1.3.4;dbname=sample';
try{
$db = new PDO($dsn, $username, $password);
$result=FALSE;
$query = "SELECT * FROM response1";
$stmt = $db->query($query);
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
echo "<table border='1'>
<p> vm1-kor-web1 </p>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>email</th>
</tr>";
foreach($rows as $row) {
echo "<tr>";
echo "<td>".$row['firstname']."</td>";
echo "<td>".$row['lastname']."</td>";
echo "<td>".$row['email']."</td>";
echo "</tr>";
}
} catch(PDOException $ex) {
echo $ex->getMessage();
}
echo "</table>";
?>
[cssexample.css]
h1, h2 {color: red;}
h3, h4 {color: darkgreen; font-size: x-large}
.success {color:green;}
.error{color:red;}
label {
display: inline-block;
width: 100px;
margin-bottom: 10px;}
반응형
'클라우드 > Azure' 카테고리의 다른 글
Azure를 이용한 웹 서비스 환경 구축 (3) (0) | 2022.07.08 |
---|---|
Azure를 이용한 웹 서비스 환경 구축 (2) (0) | 2022.07.07 |
Azure를 이용한 웹 서비스 환경 구축 (1) (0) | 2022.07.07 |