Pagina 1 di 2 1 2 ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 20
  1. #1

    Notice e Strict Standards su script per connettersi a un database

    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
    Immagini allegate Immagini allegate
    Ultima modifica di Maures; 08-09-2014 a 16:07 Motivo: problema di indentazione

  2. #2
    Il messaggio di errore ti dice esattamente il file e la riga in cui esso si verifica!
    "Mai discutere con un idiota. Ti trascina al suo livello e ti batte con l'esperienza." (Oscar Wilde)

  3. #3
    grazie, so leggere anche io, però non so come risolvere questi "errori"

  4. #4
    Quote Originariamente inviata da Maures Visualizza il messaggio
    grazie, so leggere anche io, ...
    Sinceramente il dubbio mi viene dato che hai postato del codice che non c'entra assolutamente nulla col problema evidenziato.
    Nel codice che hai postato dov'è che invochi i metodi connect(), parseDSN() e isError() che ti causano l'errore???
    "Mai discutere con un idiota. Ti trascina al suo livello e ti batte con l'esperienza." (Oscar Wilde)

  5. #5
    i metidi che dici tu li invoco quando richiamo il file DB.php all'inizio del listato con require.

    questo file è copiato da quello presente nella cartella pear di xamp

  6. #6
    Utente di HTML.it
    Registrato dal
    Feb 2014
    residenza
    Voltati
    Messaggi
    913
    Devi prima instanziare la classe
    Codice PHP:
    $db = new DB
    e poi richiamare i metodi
    Codice PHP:
    $db->connect(/*...*/); 
    No

  7. #7
    Quote Originariamente inviata da tampertools Visualizza il messaggio
    Devi prima instanziare la classe
    Codice PHP:
    $db = new DB
    e poi richiamare i metodi
    Codice PHP:
    $db->connect(/*...*/); 
    modificando il codice come dici tu mi dà questo errore:

    codice:
    Parse error: syntax error, unexpected '->' (T_OBJECT_OPERATOR) in C:\xampp\htdocs\esempi\730.php on line 9

  8. #8
    Utente di HTML.it
    Registrato dal
    Feb 2014
    residenza
    Voltati
    Messaggi
    913


    Prova ad aggiungere prima di quella riga
    Codice PHP:
    print_r($db); 
    e dimmi che ti esce fuori...
    No

  9. #9
    Posta il file DB.php
    Come ti dice l'errore il metodo connect() non è statico per cui non puoi invocarlo con DB::connect()
    "Mai discutere con un idiota. Ti trascina al suo livello e ti batte con l'esperienza." (Oscar Wilde)

  10. #10
    Quote Originariamente inviata da tampertools Visualizza il messaggio


    Prova ad aggiungere prima di quella riga
    Codice PHP:
    print_r($db); 
    e dimmi che ti esce fuori...
    ho messo il codice prima della riga che dà l'errore, ma non mi stampa nulla!

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.