コード例 #1
2
 /**
  * Callback for sorting inputs by date then value
  *
  * @param  Input $a
  * @param  Input $b
  * @return int
  */
 protected function sortInputs($a, $b)
 {
     if ($a->getDate() == $b->getDate()) {
         if ($a->getValue() == $b->getValue()) {
             return 0;
         }
         // Positive values come first, so same-day start/ends are
         // incremented before they are decremented
         return $a->getValue() > $b->getValue() ? -1 : 1;
     }
     return $a->getDate() < $b->getDate() ? -1 : 1;
 }
コード例 #2
0
ファイル: ads.create.php プロジェクト: LZJCodeup/AdLister
function processForm($postImage)
{
    $errors = [];
    $errors['count'] = 0;
    $today = date("Y-m-d");
    //pass this to be inserted into the database
    //form was submitted when $_POST is not empty
    if (!empty($_POST)) {
        try {
            $category = Input::getString('category');
        } catch (Exception $e) {
            $errors['category'] = 'Category: ' . $e->getMessage();
            $errors['count']++;
        }
        try {
            $postingTitle = Input::getString('title');
        } catch (Exception $e) {
            $errors['title'] = 'Posting Title: ' . $e->getMessage();
            $errors['count']++;
        }
        try {
            $price = Input::getNumber('price');
        } catch (Exception $e) {
            $errors['price'] = 'Price: ' . $e->getMessage();
            $errors['count']++;
        }
        try {
            $description = Input::getString('description');
        } catch (Exception $e) {
            $errors['description'] = 'Description: ' . $e->getMessage();
            $errors['count']++;
        }
        try {
            $date_posted = Input::getDate('date_posted');
        } catch (Exception $e) {
            $errors['date_posted'] = 'Date Posted: ' . $e->getMessage();
            $errors['count']++;
        }
        if ($errors['count'] == 0) {
            $adObject = new AdModel();
            $adObject->category = $category;
            $adObject->title = $postingTitle;
            $adObject->price = $price;
            $adObject->description = $description;
            var_dump($postImage);
            $adObject->image = $postImage;
            $adObject->date_posted = $today;
            // hardcoded:  $adObject->user_id = $_SESSION['user_id'];
            $adObject->users_id = 1;
            $adObject->save();
            //unset the $_SESSION['image'] - will be using the one in the database
            unset($_SESSION['image']);
            header("Location: /ads.show.php?id=" . $adObject->id);
            //this will be the $_GET for the ads.show.php
            die;
        }
    }
    return $errors;
}
コード例 #3
0
function insertPark($dbc, $park)
{
    $errorsArray = [];
    try {
        $name = Input::getString('name');
    } catch (Exception $e) {
        $error = $e->getMessage();
        array_push($errorsArray, $error);
    }
    try {
        $location = Input::getString('location');
    } catch (Exception $e) {
        $error = $e->getMessage();
        array_push($errorsArray, $error);
    }
    try {
        $date_established = Input::getDate('date_established');
    } catch (Exception $e) {
        $error = $e->getMessage();
        array_push($errorsArray, $error);
    }
    try {
        $area = Input::getNumber('area');
    } catch (Exception $e) {
        $error = $e->getMessage();
        array_push($errorsArray, $error);
    }
    try {
        $visitors = Input::getString('visitors');
    } catch (Exception $e) {
        $error = $e->getMessage();
        array_push($errorsArray, $error);
    }
    try {
        $description = Input::getString('description');
    } catch (Exception $e) {
        $error = $e->getMessage();
        array_push($errorsArray, $error);
    }
    if (!empty($errorsArray)) {
        return $errorsArray;
    }
    $query = "INSERT INTO national_parks (name, location, date_established, area, visitors, description)\n\t\t\t\tVALUES (:name, :location, :date_established, :area, :visitors, :description)";
    $query = $dbc->prepare($query);
    $query->bindValue(':name', $name, PDO::PARAM_STR);
    $query->bindValue(':location', $location, PDO::PARAM_STR);
    $query->bindValue(':date_established', $date_established, PDO::PARAM_STR);
    $query->bindValue(':area', $area, PDO::PARAM_STR);
    $query->bindValue(':visitors', $visitors, PDO::PARAM_STR);
    $query->bindValue(':description', $description, PDO::PARAM_STR);
    // $query->fetchAll(PDO::FETCH_ASSOC);
    $query->execute();
}
コード例 #4
0
function insertPark($dbc)
{
    $errors = [];
    try {
        $date = Input::getDate('date_established');
        $d = $date->format('Y-m-d');
    } catch (Exception $e) {
        $error = "An error occured: " . $e->getMessage() . PHP_EOL;
        array_push($errors['date_established'], $e->getMessage());
    }
    try {
        $name = Input::getString('name');
    } catch (Exception $e) {
        $error = "An error occured: " . $e->getMessage() . PHP_EOL;
        array_push($errors['name'], $e->getMessage());
    }
    try {
        $location = Input::getString('location');
    } catch (Exception $e) {
        $error = "An error occured: " . $e->getMessage() . PHP_EOL;
        array_push($errors['location'], $e->getMessage());
    }
    try {
        $area_in_acres = Input::getNumber('area_in_acres');
    } catch (Exception $e) {
        $error = "An error occured: " . $e->getMessage() . PHP_EOL;
        array_push($errors['area_in_acres'], $e->getMessage());
    }
    try {
        $description = Input::getString('description');
    } catch (Exception $e) {
        $error = "An error occured: " . $e->getMessage() . PHP_EOL;
        array_push($errors['description'], $e->getMessage());
    }
    if ($error) {
        echo $error;
        print_r($errors);
    } else {
        $insert = "INSERT INTO national_parks (name, location, date_established, area_in_acres, description)\n\tVALUES (:name, :location, :date_established, :area_in_acres, :description)";
        $stmt = $dbc->prepare($insert);
        $stmt->bindValue(':name', $name, PDO::PARAM_STR);
        $stmt->bindValue(':location', $location, PDO::PARAM_STR);
        $stmt->bindValue(':date_established', $d, PDO::PARAM_STR);
        $stmt->bindValue(':area_in_acres', $area_in_acres, PDO::PARAM_STR);
        $stmt->bindValue(':description', $description, PDO::PARAM_STR);
        $stmt->execute();
    }
}
コード例 #5
0
 function insertParks($dbc)
 {
     // Now calls on the Input class's getString and getDate methods with try catches.
     // Try catch create an array of errors for passing to the user in the HTML.
     $errorArray = [];
     try {
         $name = Input::getString('name', 0, 50);
     } catch (Exception $e) {
         $error = $e->getMessage();
         $errorArray['errName'] = $error;
     }
     try {
         $location = Input::getString('location', 0, 50);
     } catch (Exception $e) {
         $error = $e->getMessage();
         $errorArray['errLoc'] = $error;
     }
     try {
         $date_established = Input::getDate('date_established', '1776-07-04', 'next month');
         $date_established = $date_established->format('Y-m-d');
     } catch (Exception $e) {
         $error = $e->getMessage();
         $errorArray['errDate'] = $error;
     }
     try {
         $area_in_acres = Input::getNumber('area_in_acres', 0, 375000000);
     } catch (Exception $e) {
         $error = $e->getMessage();
         $errorArray['errArea'] = $error;
     }
     try {
         $description = Input::getString('description', 0, 500);
     } catch (Exception $e) {
         $error = $e->getMessage();
         $errorArray['errDes'] = $error;
     }
     // If the $errorArray is not empty, this will return out of the method before binding values and executing below. The $errorArray returns with an array of strings.
     if (!empty($errorArray)) {
         return $errorArray;
     }
     $stmt = $dbc->prepare('INSERT INTO national_parks (name, location, date_established, area_in_acres, description) VALUES (:name, :location, :date_established, :area_in_acres, :description)');
     $stmt->bindValue(':name', $name, PDO::PARAM_STR);
     $stmt->bindValue(':location', $location, PDO::PARAM_STR);
     $stmt->bindValue(':date_established', $date_established, PDO::PARAM_STR);
     $stmt->bindValue(':area_in_acres', $area_in_acres, PDO::PARAM_STR);
     $stmt->bindValue(':description', $description, PDO::PARAM_STR);
     $stmt->execute();
 }
コード例 #6
0
function insertData($dbc)
{
    $errors = [];
    if (!empty($_POST)) {
        try {
            $name = Input::getString('name');
        } catch (Exception $e) {
            $errors[] = $e->getMessage();
        }
        try {
            $location = Input::getString('location');
        } catch (Exception $e) {
            $errors[] = $e->getMessage();
        }
        try {
            $date = Input::getDate('date_established');
        } catch (Exception $e) {
            $errors[] = $e->getMessage();
        }
        try {
            $area = Input::getNumber('area_in_acres');
        } catch (Exception $e) {
            $errors[] = $e->getMessage();
        }
        try {
            $description = Input::getString('description');
        } catch (Exception $e) {
            $errors[] = $e->getMessage();
        }
        if (Input::notEmpty('name') && Input::notEmpty('location')) {
            $userData = 'INSERT INTO national_parks (name, location, date_established, area_in_acres, description)
							VALUES (:name, :location, :date_established, :area_in_acres, :description)';
            $userStmt = $dbc->prepare($userData);
            $userStmt->bindValue(':name', $name, PDO::PARAM_STR);
            $userStmt->bindValue(':location', $location, PDO::PARAM_STR);
            $userStmt->bindValue(':date_established', $date, PDO::PARAM_STR);
            $userStmt->bindValue(':area_in_acres', $area, PDO::PARAM_STR);
            $userStmt->bindValue(':description', $description, PDO::PARAM_STR);
            try {
                $userStmt->execute();
            } catch (Exception $e) {
                $errors[] = $e->getMessage();
                throw new Exception('Error: {$e->getMessage()}');
            }
        }
    }
    return $errors;
}
コード例 #7
0
function insertListing($dbc, $item_name, $price, $image, $description, $status = "active")
{
    $userId = Auth::id();
    $d = Input::getDate('now');
    $listing_date = $d->format('Y-m-d');
    $insert = "INSERT INTO ads(listing_date, item_name, price, image, description, status, user_id) \n\t\tVALUES (:listing_date, :item_name, :price, :image, :description, :status, :user_id)";
    $stmt = $dbc->prepare($insert);
    $stmt->bindValue(':listing_date', $listing_date, PDO::PARAM_STR);
    $stmt->bindValue(':item_name', $item_name, PDO::PARAM_STR);
    $stmt->bindValue(':price', $price, PDO::PARAM_INT);
    $stmt->bindValue(':image', $image, PDO::PARAM_STR);
    $stmt->bindValue(':description', $description, PDO::PARAM_STR);
    $stmt->bindValue(':status', $status, PDO::PARAM_STR);
    $stmt->bindValue(':user_id', $userId, PDO::PARAM_INT);
    $stmt->execute();
}
コード例 #8
0
function insertpark($dbc)
{
    $name = Input::getString('parkname', 2, 100);
    $location = Input::getString('parklocation', 2, 100);
    $date = Input::getDate('date', '1776-07-04', '9999-01-01');
    $area = Input::getNumber('area', 0, 1000000000000);
    $description = Input::getString('parkdescription', 2, 10000);
    $inner = 'INSERT INTO national_parks (name, location, date_established, area_in_acres, description) VALUES (:name, :location, :date_established, :area_in_acres, :description)';
    $query = $dbc->prepare($inner);
    $query->bindValue(':name', $name, PDO::PARAM_STR);
    $query->bindValue(':location', $location, PDO::PARAM_STR);
    $query->bindValue(':date_established', $date, PDO::PARAM_INT);
    $query->bindValue(':area_in_acres', $area, PDO::PARAM_INT);
    $query->bindValue(':description', $description, PDO::PARAM_STR);
    $query->fetchAll(PDO::FETCH_ASSOC);
    $query->execute();
}
コード例 #9
0
function insertPark($dbc)
{
    $errors = [];
    try {
        $park = Input::has('park') ? Input::getString('park') : null;
    } catch (Exception $e) {
        array_push($errors, $e->getMessage());
    }
    try {
        $location = Input::has('location') ? Input::getString('location') : null;
    } catch (Exception $e) {
        array_push($errors, $e->getMessage());
    }
    try {
        $date_established = Input::has('date_established') ? Input::getDate('date_established') : null;
        $date_established = $date_established->format('Y-m-d');
    } catch (Exception $e) {
        array_push($errors, $e->getMessage());
    }
    try {
        $area_in_acres = Input::has('area_in_acres') ? Input::getNumber('area_in_acres') : null;
    } catch (Exception $e) {
        array_push($errors, $e->getMessage());
    }
    try {
        $description = Input::has('description') ? Input::getString('description') : null;
    } catch (Exception $e) {
        array_push($errors, $e->getMessage());
    }
    if (!empty($errors)) {
        return $errors;
    }
    $query = "INSERT INTO national_parks (park, location, date_established, area_in_acres, description) VALUES (:park, :location, :date_established, :area_in_acres, :description)";
    $stmt = $dbc->prepare($query);
    $stmt->bindValue(':park', $park, PDO::PARAM_STR);
    $stmt->bindValue(':location', $location, PDO::PARAM_STR);
    $stmt->bindValue(':date_established', $date_established, PDO::PARAM_STR);
    $stmt->bindValue(':area_in_acres', $area_in_acres, PDO::PARAM_STR);
    $stmt->bindValue(':description', $description, PDO::PARAM_STR);
    $stmt->execute();
    return $errors;
}
コード例 #10
0
    Input::has('location')) {
        $park = new Park();
    try {
        $park->name = Input::getString('name');
    } catch (Exception $e) {
        $errors[] = $e->getMessage();
    }

    try {
        $park->area_in_acres = Input::getNumber('area_in_acres');
    } catch (Exception $e) {
        $errors[] = $e->getMessage();
    }

    try {
        $park->date_established = Input::getDate('date_established');
    } catch (Exception $e) {
        $errors[] = $e->getMessage();
    }

    try {
        $park->description = Input::getString('description');
    } catch (Exception $e) {
        $errors[] = $e->getMessage();
    }

    try {
        $park->location = Input::getString('location');
    } catch (Exception $e) {
        $errors[] = $e->getMessage();
    }
コード例 #11
0
require_once 'db_connect.php';
require_once 'Input.php';
if (isset($_GET['page'])) {
    $page = $_GET['page'];
    if ($page < 1) {
        $page = 1;
    }
} else {
    $page = 1;
}
$limit = 4;
$offset = ($page - 1) * 4;
//prepare statements for user submissions
//A NOTE FROM THURSDAY: right now, the specific getDate/String/Number methods from Input class are only being applied at the user submission level. to update this, (remove these?? maybe not) add these to your PDO, or whatever it is called)
//note FROM THURSDAY CATCH LESSON: Push your errors onto an $errors[] array.
if (Input::has('nameSubmit') && Input::getString('nameSubmit') != "" && Input::has('acreageSubmit') && Input::getNumber('acreageSubmit') != "" && Input::getString('stateSubmit') && Input::has('stateSubmit') != "" && Input::getDate('date_estSubmit') && Input::has('date_estSubmit') != "" && Input::getString('descriptionSubmit') && Input::has('descriptionSubmit') != "") {
    echo "THANK YOU FOR YOUR INPUT." . PHP_EOL;
    $nameSubmit = $_POST['nameSubmit'];
    $stateSubmit = $_POST['stateSubmit'];
    $date_estSubmit = $_POST['date_estSubmit'];
    $acreageSubmit = $_POST['acreageSubmit'];
    $descriptionSubmit = $_POST['descriptionSubmit'];
    $stmt = $connection->prepare("INSERT INTO national_parks (name, location, date_est, acreage, description) VALUES (:nameSubmit, :stateSubmit, :date_estSubmit, :acreageSubmit, :descriptionSubmit)");
    $stmt->bindValue(':nameSubmit', $nameSubmit, PDO::PARAM_STR);
    $stmt->bindValue(':stateSubmit', $stateSubmit, PDO::PARAM_STR);
    $stmt->bindValue(':date_estSubmit', $date_estSubmit, PDO::PARAM_STR);
    $stmt->bindValue(':acreageSubmit', $acreageSubmit, PDO::PARAM_STR);
    $stmt->bindValue(':descriptionSubmit', $descriptionSubmit, PDO::PARAM_STR);
    $stmt->execute();
}
//to convert a query statement to a prepare statement, change query to prepare and change $variable to :variable
コード例 #12
0
$limit = 4;
$pageID = Input::has('page') ? Input::get('page') : 1;
$offset = $limit * $pageID - $limit;
$stmt = $dbc->prepare("SELECT * FROM national_parks LIMIT :limit OFFSET :offset");
$stmt->bindValue(':offset', $offset, PDO::PARAM_INT);
$stmt->bindValue(':limit', $limit, PDO::PARAM_INT);
$stmt->execute();
$count = $dbc->query('SELECT COUNT(*) FROM national_parks;')->fetchColumn();
$parks = $stmt->fetchAll(PDO::FETCH_ASSOC);
$numPages = ceil($count / $limit);
$next = $pageID + 1;
$previous = $pageID - 1;
$newPark = [];
$errorMessage = "Add a Park!";
if (!empty($_POST)) {
    if (Input::getString('name') && Input::getString('url') && Input::getString('location') && Input::getDate('date_established') && Input::getNumber('area_in_acres') && Input::getString('description')) {
        $newPark = $dbc->prepare("INSERT INTO national_parks(name, url, location, date_established, area_in_acres, description) \n\t\tVALUES(:name, :url, :location, :date_established, :area_in_acres, :description)");
        $newPark->bindValue(':name', Input::get('name'), PDO::PARAM_STR);
        $newPark->bindValue(':url', Input::get('url'), PDO::PARAM_STR);
        $newPark->bindValue(':location', Input::get('location'), PDO::PARAM_STR);
        $newPark->bindValue(':date_established', Input::get('date_established'), PDO::PARAM_STR);
        $newPark->bindValue(':area_in_acres', Input::get('area_in_acres'), PDO::PARAM_STR);
        $newPark->bindValue(':description', Input::get('description'), PDO::PARAM_STR);
        $newPark->execute();
    } else {
        $errorMessage = 'To add a park please input all fields!';
    }
}
$del = "DELETE FROM `national_parks` WHERE `id` = :id_to_delete";
?>
コード例 #13
0
$limit = 4;
$offset = $page * $limit - $limit;
if (Input::has('name') && Input::get('name') != '' && Input::has('location') && Input::get('location') != '' && Input::has('date_established') && Input::get('date_established') != '' && Input::has('area_in_acres') && Input::get('area_in_acres') != '' && Input::has('description') && Input::get('description') != '') {
    try {
        $parkname = Input::getString('name');
    } catch (Exception $e) {
        array_push($errorMessages, $e->getMessage());
        // echo 'An error occurred: ' . $e->getMessage() . PHP_EOL;
    }
    try {
        $parklocation = Input::getString('location');
    } catch (Exception $e) {
        array_push($errorMessages, $e->getMessage());
    }
    try {
        $parkdate = Input::getDate('date_established');
    } catch (Exception $e) {
        array_push($errorMessages, $e->getMessage());
    }
    try {
        $parksize = Input::getNumber('area_in_acres');
    } catch (Exception $e) {
        array_push($errorMessages, $e->getMessage());
    }
    try {
        $parkdescr = Input::getString('description');
    } catch (Exception $e) {
        array_push($errorMessages, $e->getMessage());
    }
    if (empty($errorMessages)) {
        $checkrow = 'SELECT id FROM national_parks WHERE name = :name';
コード例 #14
0
 } catch (OutOfRangeException $e) {
     $errors[] = $e->getMessage();
 } catch (DomainException $e) {
     $errors[] = $e->getMessage();
 } catch (RangeException $e) {
     $errors[] = $e->getMessage();
 }
 if ($_FILES) {
     $uploadsDirectory = 'img/uploads/';
     $filename = $uploadsDirectory . basename($_FILES['image_url']['name']);
     if (!move_uploaded_file($_FILES['image_url']['tmp_name'], $filename)) {
         $errors[] = "Sorry, there was an error uploading your file.";
     }
 }
 try {
     $sale_end_date = Input::getDate('sale_end_date');
 } catch (Exception $e) {
     $errors[] = $e->getMessage();
 }
 try {
     $categories = Input::getString('categories');
 } catch (InvalidArguementException $e) {
     $errors[] = $e->getMessage();
 } catch (OutOfRangeException $e) {
     $errors[] = $e->getMessage();
 } catch (DomainException $e) {
     $errors[] = $e->getMessage();
 } catch (LengthException $e) {
     $errors[] = $e->getMessage();
 }
 try {
コード例 #15
0
     $password = Input::getString('password');
 } catch (Exception $e) {
     $errors['password'] = $e->getMessage();
 }
 try {
     $confirmPassword = Input::getString('confirm_password');
 } catch (Exception $e) {
     $errors['confirm_password'] = $e->getMessage();
 }
 try {
     $email = Input::getString('email');
 } catch (Exception $e) {
     $errors['email'] = $e->getMessage();
 }
 try {
     $dateTimeObject = Input::getDate('birth_date', new DateTime('1900-01-01'), new DateTime());
 } catch (Exception $e) {
     $errors['birth_date'] = $e->getMessage();
 }
 try {
     $gender = Input::get('gender');
 } catch (Exception $e) {
     $errors['gender'] = $e->getMessage();
 }
 var_dump($errors);
 $user = new User();
 try {
     if ($user->checkUsername($userName)) {
         throw new Exception("Username has been taken");
     }
 } catch (Exception $e) {
コード例 #16
0
function insertPark($dbc)
{
    $errors = [];
    try {
        $name = Input::getString('name');
    } catch (Exception $e) {
        array_push($errors, $e->getMessage());
    }
    try {
        $location = Input::getString('location');
    } catch (Exception $e) {
        array_push($errors, $e->getMessage());
    }
    try {
        $date_established = Input::getDate('date_established');
        $date_established = $date_established->format("Y-m-d");
    } catch (Exception $e) {
        array_push($errors, $e->getMessage());
    }
    try {
        $area_in_acres = Input::getNumber('area_in_acres');
    } catch (Exception $e) {
        array_push($errors, $e->getMessage());
    }
    try {
        $url = Input::getString('url');
    } catch (Exception $e) {
        array_push($errors, $e->getMessage());
    }
    try {
        $description = Input::getString('description');
    } catch (Exception $e) {
        array_push($errors, $e->getMessage());
    }
    if (!empty($errors)) {
        return $errors;
    }
    $userInput = $dbc->prepare('INSERT INTO national_parks (name, location, date_established, area_in_acres, url, description) VALUES (:name, :location, :date_established, :area_in_acres, :url, :description)');
    $userInput->bindValue(':name', ucfirst($name), PDO::PARAM_STR);
    $userInput->bindValue(':location', ucfirst($location), PDO::PARAM_STR);
    $userInput->bindValue(':date_established', $date_established, PDO::PARAM_STR);
    $userInput->bindValue(':area_in_acres', $area_in_acres, PDO::PARAM_STR);
    $userInput->bindValue(':url', $url, PDO::PARAM_STR);
    $userInput->bindValue(':description', ucfirst($description), PDO::PARAM_STR);
    $userInput->execute();
    return $errors;
}
コード例 #17
0
define('DB_USER', 'parks_user');
define('DB_PASS', 'parks');
require "../db_connect.php";
//put handling of insert here
$errorMessage = "";
$errors = [];
$inputName = "";
$inputLocation = "";
$dateResult = "";
$inputArea = "";
$inputDescription = "";
if (isset($_REQUEST['submit'])) {
    try {
        $inputName = Input::getString('name', 1, 50);
        $inputLocation = Input::getString('location', 1, 50);
        $inputDate = Input::getDate('date_established', '1776-07-04');
        $inputArea = Input::getNumber('area_in_acres', 1, 5000000);
        $inputDescription = Input::getString('description', 1, 255);
        $dateResult = $inputDate->format('Y-m-d H:i:s');
        $dataArray = array($inputName, $inputLocation, $dateResult, $inputArea, $inputDescription);
        $insert = 'INSERT INTO national_parks (name,location,date_established, area_in_acres, description) VALUES (?,?,?,?,?)';
        $stmt = $dbc->prepare($insert);
        $stmt->execute($dataArray);
        if ($stmt->rowCount() != 1) {
            $errorMessage = "There was an error inserting the new row.";
        } else {
            $errorMessage = "Your entry has been submitted successfully.";
            $inputName = "";
            $inputLocation = "";
            $dateResult = "";
            $inputArea = "";
コード例 #18
0
$page = Input::has('page') ? Input::get('page') : 1;
$limit = Input::has('limit') ? Input::get('limit') : 4;
$offset = $limit * ($page - 1);
// Prepare statement for national parks
$stmt = $dbc->prepare("SELECT * FROM national_parks LIMIT :limit OFFSET :offset");
// Bind values for security
$stmt->bindValue(':limit', $limit, PDO::PARAM_INT);
$stmt->bindValue(':offset', $offset, PDO::PARAM_INT);
$stmt->execute();
$parks = $stmt->fetchAll(PDO::FETCH_ASSOC);
// Process POST request
if ($_POST) {
    // Assign POST variables
    $newParkName = Input::has('park_name') && Input::get('park_name') != '' ? Input::getString('park_name') : '';
    $newParkLoc = Input::has('park_loc') && Input::get('park_loc') != '' ? Input::getString('park_loc') : '';
    $newParkDate = Input::has('date_est') && Input::get('date_est') != '' ? Input::getDate('date_est') : '';
    $newParkArea = Input::has('park_area') && Input::get('park_area') != '' ? Input::getNumber('park_area') : '';
    $newParkDesc = Input::has('park_desc') && Input::get('park_desc') != '' ? Input::getString('park_desc') : '';
    // Insert new park into DB
    $insert = $dbc->prepare('INSERT INTO national_parks (park_name, park_loc, date_est, area_in_acres, about_park)
        VALUES (:name, :location, :date, :area, :description)');
    $insert->bindValue(':name', $newParkName, PDO::PARAM_STR);
    $insert->bindValue(':location', $newParkLoc, PDO::PARAM_STR);
    $insert->bindValue(':date', date_format($newParkDate, 'Y-m-d'), PDO::PARAM_STR);
    $insert->bindValue(':area', $newParkArea, PDO::PARAM_STR);
    $insert->bindValue(':description', $newParkDesc, PDO::PARAM_STR);
    $insert->execute();
}
?>

<!DOCTYPE html>
コード例 #19
0
        $name = Input::getString('name');
    } catch (Exception $e) {
        $errors['name'] = $e->getMessage();
    }
    try {
        $location = Input::getString('location');
    } catch (Exception $e) {
        $errors['location'] = $e->getMessage();
    }
    try {
        $area = Input::getNumber('area');
    } catch (Exception $e) {
        $errors['area'] = $e->getMessage();
    }
    try {
        $dateTimeObject = Input::getDate('date_established', new DateTime('1700-01-01'), new DateTime());
    } catch (Exception $e) {
        $errors['date_established'] = $e->getMessage();
    }
    if (empty($errors)) {
        $formattedDate = $dateTimeObject->format('Y-m-d');
        $query = 'INSERT INTO national_parks (name,location, date_established, area_in_acres)
		VALUES (:name,:location,:date_established,:area )';
        $stmt = $dbc->prepare($query);
        $stmt->bindValue(':name', $name, PDO::PARAM_STR);
        $stmt->bindValue(':location', $location, PDO::PARAM_STR);
        $stmt->bindValue(':date_established', $formattedDate, PDO::PARAM_STR);
        $stmt->bindValue(':area', $area, PDO::PARAM_STR);
        $stmt->execute();
    }
}