Visualizzazione dei risultati da 1 a 5 su 5
  1. #1

    select con php/mysql: dov'è l'errore?

    Con l'aiuto di untente di questo forum (Emulman) ho creato questo script: contiene due select e richiama dei dati da un database mysql che in teoria dovrebbero venir stampati su una pagina...ma purtroppo c'è qualcosa che non funziona, non trova nulla nel database.

    Il risultato è che viene fuori adesso purtroppo è questo.

    Qualcuno sa trovare dov'è l'errore per favore?

    Codice PHP:
    <?php
      
    if ($_REQUEST['ok']) {
        
    $cittaID $_POST['citta'];
        
    $mestiereID $_POST['mestieri'];
      }
    ?>
    <!DOCTYPE HTML>

    <html>

    <head>
      <title>PROVA SELECT CON PHP</title>
      <meta charset="utf-8" />
    </head>

    <body>
      <form id="sel" method="post" action="emulman.php?ok=1">
        <div><label for="citta">Citt&agrave;</label>
        <select name="citta" id="citta" size="1" tabindex="1" onchange="location.href='emulman.php?ok=1'">
        <?php
          $link 
    mysql_connect('localhost''nozzeprova''');//utente e passw
          
    if (!$link)
            echo 
    '<script>alert("Could not connect: ' mysql_error() . ')</script>';
          else {
            
    $db_selected mysql_select_db('my_nozzeprova'$link);//nome del mio database my_nozzeprova
            
    if (!$db_selected)
              echo 
    '<script>alert("Cannot use database: ' mysql_error() . ')</script>';
            else {
              
    $result mysql_query("SELECT * FROM citta ORDER BY nome ASC");
              if (
    mysql_num_rows($result) < 1)
                echo 
    "No rows found, nothing to print so am exiting";
               else {
                 while (
    $row mysql_fetch_assoc($result)) {
                   if (
    $_REQUEST['ok'])
                     echo 
    '<option value="' $row['ID_CITTA'] . '"' selected($row['ID_CITTA'], $cittaID) . '>' $row['nome'] . '</option>';
                   else
                    echo 
    '<option value="' $row['ID_CITTA'] . '">' $row['nome'] . '</option>';
                 }
               }
             }
           }
          
    mysql_close($link);
        
    ?>
       </select></div>
                            e
       <div><label for="mestieri">Mestiere</label>
        <select name="mestier" id="mestiere" size="1" tabindex="1" onchange="location.href='emulman.php?ok=1'">
        <?php
          $link 
    mysql_connect('localhost''nozzeprova''');//utente e passw
          
    if (!$link)
            echo 
    '<script>alert("Could not connect: ' mysql_error() . ')</script>';
          else {
            
    $db_selected mysql_select_db('my_nozzeprova'$link);//nome del mio database my_nozzeprova
            
    if (!$db_selected)
              echo 
    '<script>alert("Cannot use database: ' mysql_error() . ')</script>';
            else {
              
    $result mysql_query("SELECT * FROM mestieri ORDER BY nome ASC");
              if (
    mysql_num_rows($result) < 1)
                echo 
    "No rows found, nothing to print so am exiting";
               else {
                 while (
    $row mysql_fetch_assoc($result))  {
                   if (
    $_REQUEST['ok'])
                     echo 
    '<option value="' $row['ID_MESTIERE'] . '"' selected($row['ID_MESTIERE'], $cittaID) . '>' $row['nome'] . '</option>';
                   else
                     echo 
    '<option value="' $row['ID_MESTIERE'] . '">' $row['nome'] . '</option>';
                 }
               }
             }
           }
          
    mysql_close($link);
        
    ?>
       </select></div>
       <input type="submit" name="invia" value="Cerca">
       <?php
         
    if ($_REQUEST['ok']) {
           
    $link mysql_connect('localhost''nozzeprova''');//utente e passw
           
    if (!$link) {
             die(
    'Could not connect: ' mysql_error());
           }
           
    $db_selected mysql_select_db('my_nozzeprova'$link);//nome del mio database my_nozzeprova
           
    if (!$db_selected) {
             die (
    'Can\'t use foo : ' mysql_error());
           }
           
    $result mysql_query("SELECT * FROM aziende WHERE id_citta = " $cittaID " AND id_mestiere = " $mestiereID);
           if (
    mysql_num_rows($result) < 1) {
             echo 
    "No rows found, nothing to print so am exiting";
             exit;
           }
           echo 
    '<table id="lavori"><caption>AZIENDE IN ' $citta($cittaID) . ' LAVORI ' $lavori($mestiereID) . '</caption>';
           echo 
    '<tr><th>Citta</th></td><td>Descrizione</td></tr>';
           while (
    $row mysql_fetch_assoc($result)) {
             echo 
    '<tr><td>' $row['nome'] . '</td><td>' $row['descrizione'] . '</td></tr>';
           }
           echo 
    '</table>';
         }
         
    ?>
       </form>
      </body>
    </html>
    <?php
      
    function selected($val1$val2) {
        
    $val1 trim($val1);
        
    $val2 trim($val2);
        if (
    $val1 == $val2)
          return 
    " selected";
        else
          return 
    "";
      }

      function 
    cittaID($id) {
         
    $link mysql_connect('localhost''nozzeprova''');//utente e passw
         
    if (!$link) {
           die(
    'Could not connect: ' mysql_error());
         }
         
    $db_selected mysql_select_db('my_nozzeprova'$link);//nome del mio database my_nozzeprova
         
    if (!$db_selected) {
           die (
    'Can\'t use foo : ' mysql_error());
         }
         
    $result mysql_query("SELECT * FROM citta WHERE ID_CITTA = " $citta);
         if (
    mysql_num_rows($result)) {
           
    $row mysql_fetch_assoc($result);
           
    $citta $row['nome'];
         }
         
    mysql_free_result($result);
         
    mysql_close($link);
         return 
    $citta;
      }

      function 
    lavori($id) {
         
    $link mysql_connect('localhost''nozzeprova''');//utente e passw
         
    if (!$link) {
           die(
    'Could not connect: ' mysql_error());
         }
         
    $db_selected mysql_select_db('my_nozzeprova'$link);//nome del mio database my_nozzeprova
         
    if (!$db_selected) {
           die (
    'Can\'t use foo : ' mysql_error());
         }
         
    $result mysql_query("SELECT * FROM mestieri WHERE ID_MESTIERE = " $mestiere);
         if (
    mysql_num_rows($result)) {
           
    $row mysql_fetch_assoc($result);
           
    $lavoro $row['nome'];
         }
         
    mysql_free_result($result);
         
    mysql_close($link);
         return 
    $lavoro;
      }
    ?>

  2. #2
    Utente di HTML.it L'avatar di boots
    Registrato dal
    Oct 2012
    Messaggi
    1,626
    Come in molti ripetono nel forum, fatti stampare la query così hai un'idea di cosa non va.

    Poi, non so se è solo un errore di battitura nel post (ci sono un po' di doppi apici mancanti ), ma la seconda select ha il name errato

    name="mestier"

  3. #3
    Utente di HTML.it L'avatar di .Kurt
    Registrato dal
    Jul 2007
    Messaggi
    654
    ci sono così tanti livelli di codice sbagliato...

  4. #4
    Originariamente inviato da boots

    la seconda select ha il name errato

    name="mestier"
    è vero, grazie. Ho fatto la correzione e ora quando premo sul tasto "cerca" mi da questo errore:

    Fatal error: Function name must be a string in /membri/nozzeprova/emulman.php on line 89

    Ma dov'è l'errore alla linea 89?

  5. #5
    E cosa dovrebbero essere:

    Codice PHP:
    $citta(...) 
    e

    Codice PHP:
    $lavori(...) 


    Il copia/incolla non porta da nessuna parte
    "Mai discutere con un idiota. Ti trascina al suo livello e ti batte con l'esperienza." (Oscar Wilde)

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 © 2025 vBulletin Solutions, Inc. All rights reserved.