Code behaviour
Posted: Wed Feb 14, 2018 11:13 pm
Hi guys i am learning php by following the book "Learning PHP". I building a simple form for an excercise in the book and need some clarification on this line of code $input['dish_name'] = $dishes[$_POST['dish_name']], why do i need $dishes in there?? Why cant i simply go $input['dish_name'] = $_POST['dish_name'] When i tried this line i wouldnt get any output just a blank page. Any help would be appreciated. Thanks
p.s I am using php 7.3 and here is the code of the form
p.s I am using php 7.3 and here is the code of the form
Code: Select all
<?php
require 'formHelper.php';
try{
$db = new PDO('sqlite:c:\xampp\htdocs\tmp\restaurant2.db');
}
catch(PDOException $e){
print "Couldnt connect to the database, " . $e->getMessage();
}
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
$dishes = array();
$query = $db->query("SELECT dish_name FROM dishes");
foreach($query as $dish){
$dishes[] = $dish['dish_name'];
}
function validate_form(){
global $dishes;
$errors = array();
$input = array();
if(isset($_POST['dish_name'])){
$input['dish_name'] = $dishes[$_POST['dish_name']];
}
else{
$errors[] = "Please select a dish from the menu.";
}
return array($errors,$input);
}