Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP File Handling with PHP Parsing Specific Formats Reading XML

Samandar Mirzayev
Samandar Mirzayev
11,834 Points

Guys i need help, there is i did for add_book.php cannot add books i did everything and chmod from console but notworked

<?php

$new_book = [
    'title' => filter_input(INPUT_POST, 'title', FILTER_SANITIZE_STRING),
    'link' => filter_input(INPUT_POST, 'link', FILTER_SANITIZE_URL),
    'book_image_url' => filter_input(INPUT_POST, 'book_image_url', FILTER_SANITIZE_URL),
    'book_description' => filter_input(INPUT_POST, 'book_description', FILTER_SANITIZE_STRING),
    'num_pages' => filter_input(INPUT_POST, 'num_pages', FILTER_SANITIZE_NUMBER_INT),
    'author_name' => filter_input(INPUT_POST, 'author_name', FILTER_SANITIZE_STRING),
    'isbn' => filter_input(INPUT_POST, 'isbn', FILTER_SANITIZE_NUMBER_INT),
    'average_rating' => filter_input(INPUT_POST, 'average_rating', FILTER_SANITIZE_NUMBER_FLOAT),
    'book_published' => filter_input(INPUT_POST, 'book_published', FILTER_SANITIZE_NUMBER_INT),
];

$file = '../data/json/top_programming_books.json';
$books = json_decode(file_get_contents($file));

if (is_object($books->collection->books[0])) {
    $books->collection->books[] = $new_book;
}

$json = json_encode($books, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
//echo '<pre>' . $json . '</pre>';
file_put_contents($file, $json);
header('location: /books.php');

Moderator edited: Markdown added so that code renders properly in the forums.