Buongiorno a tutti,
da qualche settimana sono entrato nel mondo della programmazione web e sto affrontando le basi di php.
Fino a ora tutto bene, anche grazie a un manuale che si intitola PHP5 Elementi di programmazione della O'reilly (non intendo fare pubblicità, in caso avvisatemi e cancello il titolo)
Sono arrivato al punto in cui è presente un listato per inserire i dati all'interno di un database che è questo:
Codice PHP:
<?php// Load PEAR DB
require 'DB.php';
// Load the form helper functions
require 'formhelpers.php';
// Connect to the database
$db = DB::connect('mysql://root:root@localhost/restaurant');
if (DB::isError($db)) { die ("Can't connect: " . $db->getMessage()); }
// Set up automatic error handling
$db->setErrorHandling(PEAR_ERROR_DIE);
// The main page logic:
// - If the form is submitted, validate and then process or redisplay
// - If it's not submitted, display
if ($_POST['_submit_check']) { // If validate_form() returns errors, pass them to show_form()
if ($form_errors = validate_form()) {
show_form($form_errors);
} else {
// The submitted data is valid, so process it
process_form();
}
} else {
// The form wasn't submitted, so display
show_form();}
function show_form($errors = '') {
// If the form is submitted, get defaults from submitted parameters
if ($_POST['_submit_check']) {
$defaults = $_POST;
} else {
// Otherwise, set our own defaults: price is $5
$defaults = array('price' => '5.00'); }
// If errors were passed in, put them in $error_text (with HTML markup)
if (is_array($errors)) {
$error_text = '<tr><td>You need to correct the following errors:';
$error_text .= '</td><td><ul><li>';
$error_text .= implode('</li><li>',$errors);
$error_text .= '</li></ul></td></tr>';
} else {
// No errors? Then $error_text is blank
$error_text = ''; }
// Jump out of PHP mode to make displaying all the HTML tags easier
?>
<form method="POST" action="<?php print $_SERVER['PHP_SELF']; ?>"><table><?php print $error_text ?>
<tr><td>Dish Name:</td><td><?php input_text('dish_name', $defaults) ?></td></tr>
<tr><td>Price:</td><td><?php input_text('price', $defaults) ?></td></tr>
<tr><td>Spicy:</td><td><?php input_radiocheck('checkbox','is_spicy', $defaults, 'yes'); ?> Yes</td></tr>
<tr><td colspan="2" align="center"><?php input_submit('save','Order'); ?></td></tr>
</table><input type="hidden" name="_submit_check" value="1"/></form>
<?php } // The end of show_form()
function validate_form() {
$errors = array();
// dish_name is required
if (! strlen(trim($_POST['dish_name']))) {
$errors[] = 'Please enter the name of the dish.'; }
// price must be a valid floating point number and
// more than 0
if (floatval($_POST['price'] <= 0)) {
$errors[] = 'Please enter a valid price.'; }
return $errors;}
function process_form() {
// Access the global variable $db inside this function
global $db;
// Get a unique ID for this dish
$dish_id = $db->nextID('dishes');
// Set the value of $is_spicy based on the checkbox
if ($_POST['is_spicy'] == 'yes') {
$is_spicy = 1;
} else {
$is_spicy = 0;
}
// Insert the new dish into the table
$db->query('INSERT INTO dishes (dish_id, dish_name, price, is_spicy)
VALUES (?,?,?,?)',
array($dish_id, $_POST['dish_name'], $_POST['price'], $is_spicy));
// Tell the user that we added a dish.
print 'Added ' . htmlentities($_POST['dish_name']) . ' to the database.';}
?>
i file richiamati all'inizio sono già stati spostati rispettivamente dalla cartella C:\xampp\php\pear per quanto riguarda il file DB.php, mentre il file formhelpers.php è questo:
Codice PHP:
<?php//print a text box
function input_text($element_name, $values) {
print '<input type="text" name="' . $element_name .'" value="';
print htmlentities($values[$element_name]) . '">';}
//print a submit button
function input_submit($element_name, $label) {
print '<input type="submit" name="' . $element_name .'" value="';
print htmlentities($label) .'"/>';}
//print a textarea
function input_textarea($element_name, $values) {
print '<textarea name="' . $element_name .'">';
print htmlentities($values[$element_name]) . '</textarea>';}
//print a radio button or checkbox
function input_radiocheck($type, $element_name, $values, $element_value) {
print '<input type="' . $type . '" name="' . $element_name .'" value="' . $element_value . '" ';
if ($element_value == $values[$element_name]) {
print ' checked="checked"';
}
print '/>';}
//print a <select> menu
function input_select($element_name, $selected, $options, $multiple = false) {
// print out the <select> tag
print '<select name="' . $element_name;
// if multiple choices are permitted, add the multiple attribute
// and add a [] to the end of the tag name
if ($multiple) { print '[]" multiple="multiple'; }
print '">';
// 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 <option> tags
foreach ($options as $option => $label) {
print '<option value="' . htmlentities($option) . '"';
if ($selected_options[$option]) {
print ' selected="selected"';
}
print '>' . htmlentities($label) . '</option>';
}
print '</select>';}?>
pensando di aver fatto tutto correttamente, carico il file ma lo script mi dà questi errori:
codice:
Strict Standards: Non-static method DB::connect() should not be called statically in C:\xampp\htdocs\esempi\730.php on line 8
Strict Standards: Non-static method DB::parseDSN() should not be called statically in C:\xampp\htdocs\esempi\DB.php on line 520
Strict Standards: Non-static method DB::isError() should not be called statically in C:\xampp\htdocs\esempi\DB.php on line 557
Strict Standards: Non-static method DB::isError() should not be called statically in C:\xampp\htdocs\esempi\730.php on line 9
Notice: Undefined index: _submit_check in C:\xampp\htdocs\esempi\730.php on line 16
Notice: Undefined index: _submit_check in C:\xampp\htdocs\esempi\730.php on line 31
ho provato a cercare un po' su internet, ma credo che rivolgendomi a voi possa risolvere prima il mio problema.
Metto in allegato anche uno screen di come viene visualizzata la pagina.
Grazie e a presto
Edit: mi ha sfasato tutti gli a capo, spero di essere riuscito a sistemarli tutti