Question: PHP/HTML This program should pull book information (isbn, title, description) from a file, and populate a two-dimensional array from the file. Then it should accept
PHP/HTML
This program should pull book information (isbn, title, description) from a file, and populate a two-dimensional array from the file. Then it should accept information about another book from an HTML form, add the information to the array, and restore the information in a file. Use the code from Example 2-4 or Example 2-5 to help you.
Before you can read from a file there needs to be recorded in the file. Add the code to save the array into the file first. Then run the program. Verify that the information was saved properly in the file. Then add code to read from the file.
Example 2-4 Code
$customer_file = file_get_contents("customer_data.json");
$customer_record = json_decode($customer_file, TRUE);
$customer = filter_input_array(INPUT_POST);
$customer_info = validate_array($customer["customer_record"]);
array_push($customer_record, $customer_info);
ptrint_r($customer_record);
file_put_contents("customer_data.json", json_encode($customer_record));
?>
Example 2-5 Code
<?php
try
{
$customer_file = file_get_contents("customer_data.json");
$customer_record = json_decode($customer_file, TRUE);
$customer = filter_input_array(INPUT_POST);
$customer_info = validate_array($customer["customer_record"]);
array_push($customer_record, $customer_info);
ptrint_r($customer_record);
file_put_contents("customer_data.json", json_encode($customer_record));
}
catch(Exception $e)
{
print "An Exception occurred. Message: ".$e->getMessage();
}
catch(Error $e)
{
print"An Error occurred. Message: ".$e->getMessage();
}
?>
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
