/* don't forget to wrap this whole file in tags (-: Week 6 Assignment: Fill in the bodies of db_connect(), save_comment() and display_comment() Save this file as guestbook.inc and turn it in (along with a copy of guestbook.php) At the end of the day it should work just like http://davidmintz.org/php_course/guestbook.php */ error_reporting(E_ALL); $subject_options = array(''=>'[select one]','rant'=>'rant','praise'=>'praise','question'=>'question','other'=>'other'); $defaults = array('comment_type'=>'','text'=>'','posted_by'=>''); function db_connect() { // fill in a method that connects to the database server // and selects the php_university database // return true on success or false on failure. // suppress error output with @ (except during development (-:) } function display_comments() { /* select the 10 most recent comments in descending order of date/time of posting and display them (using a while loop). Use the date_format() mysql function as part of the SELECT to format the timestamp: date_format(posted_on,'%a %d-%b-%Y %l:%i %p') 'posted_on' use html_entities() on all the database data return false if the SELECT query fails return true at the end of the function */ } function save_comment($data) { /* here's a tip: foreach($data as $key=>$value) { $data[$key]=mysql_escape_string($value) ; } now you can use these values in your insert -- you still will need quotes around them, though. execute the insert query and return false if it fails. otherwise, return mysql_affected_rows(); */ } function validate_form(&$data) { $errors = array(); if (empty($data['text'])) { $errors[] = 'The comments field is required'; } if (strlen($data['text']) > 255 ) { $errors[] = 'The comments field exceeds the maximum length'; } $data['text'] = strip_tags($data['text']); $data['posted_by'] = strip_tags(trim($data['posted_by'])); if (! strlen($data['posted_by'])) { $data['posted_by'] = 'anonymous'; } if (strlen($data['posted_by']) > 36) { // truncate it $data['posted_by'] = substr($data['posted_by'],0,36); $errors[] = 'Your name exceeds the max field length' ; } if (empty($data['comment_type'])) { $errors[] = 'Subject field is required'; } if (! array_key_exists($data['comment_type'],$GLOBALS['subject_options'])) { $errors[] = 'Invalid subject field'; } return $errors; } function input_select($element_name, $selected, $options, $multiple = false) { // print out the '; // set up the list of things to be selected $selected_options = array(); if ($multiple) { foreach ($selected[$element_name] as $val) { $selected_options[$val] = true; } } else { $selected_options[ $selected[$element_name] ] = true; } // print out the '; } print ''; } function display_form($defaults) { global $subject_options; ?>
Subject
Your name
Comments