コード例 #1
0
} catch (PDOException $e) {
    print 'Connection failed: ' . $e->getMessage();
}
?>
<body>
<main>
    <h1>Diet Edit</h1>
    <div>
        <h2>Add new creature diet</h2>
        <form method="post" action="CreatureDietAdd.php">
            <input type="text" name="Name">
            <input type="submit" value="Create">
            <input type="reset" value="Restart">
        </form>
    </div>
    <div>
        <h2>List of creature diets</h2>
        <ul>
            <?php 
$creatureDietGateway = new CreatureDietGateway($database);
$attributes = $creatureDietGateway->selectAll();
foreach ($attributes as $attribute) {
    print "<li>" . $attribute['name'] . "</li>";
}
?>
        </ul>
    </div>
</main>
</body>
</html>
コード例 #2
0
<?php

use BattleChores\domain\creature\CreatureDietGateway;
include '../config.php';
try {
    $database = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
    print 'Connection failed: ' . $e->getMessage();
}
$errorCount = 0;
if (!isset($_POST['Name']) || strlen($_POST['Name']) < 1) {
    print "<p>Please specify a name for the creature diet</p>";
    $errorCount++;
}
if (strlen($_POST['Name']) > 50) {
    print "<p>The creature diet's name must be shorter than 50 characters</p>";
    $errorCount++;
}
if ($errorCount == 0) {
    $creatureDietGateway = new CreatureDietGateway($database);
    $insertSuccess = $creatureDietGateway->insertNew($_POST['Name']);
    if ($insertSuccess) {
        print "Diet " . $_POST['Name'] . " successfully added to the Database";
    } else {
        print "Error Adding diet " . $_POST['Name'];
    }
}