Visualizzazione dei risultati da 1 a 6 su 6

Discussione: PHP Order By

  1. #1
    Utente di HTML.it
    Registrato dal
    Nov 2007
    Messaggi
    60

    PHP Order By

    Salve a tutti ho un problema con un order by (lo so che in giro c'è tanta roba ma non riesco perché a me dia errore).

    Ho creato un sistema di gestione dei contatti, funziona tutto perfettamente (inserisco i contatti da csv, li esporto, aggiungo, modifico).

    Ora volevo far in modo di poter ordinare le colonne a seconda della scelta ma se provo a far funzionare il seguente script mi da il seguente errore:

    codice:
    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ORDER BY Nome_Cognome asc' at line 8
    Come posso risolvere?

    Codice PHP:
    <?php
    include 'auth.inc.php';
    ?>
    <link rel="stylesheet" type="text/css" href="css/style.css">
    <style type="text/css">
    #error-login {
        clear:left;
        text-align:center;
        position:relative;
        top:50px;
        font-size:18px;
    }

    #mainform_error {
        height:350px;
        width:800px;
    }
    /*#intestazione {
        position:fixed;
    }*/
    </style>
    <?php

    if ($_SESSION['admin_level'] < 1) {
        
    header('Refresh: 5; URL=index.php');
       echo 
    '
                <div class="mainform_admin" id="mainform_error" >
                <div>
                [img]images/logo.png[/img]
                  <h1 class="h1">CRM Verde Profilo</h1>
                </div>



                <div id="error-login">
                    

    [b][/b]Non sei autorizzato a visualizzare la pagina.[/b]</p>
                    

    Verrai ora reindirizzato verso la pagina di login. 

    Se il tuo browser non ti reindirizza automaticamente, [url="index.php"]clicca qui[/url].</p>
                </div>
                </div>
        '
    ;
        die();
    }

    include 
    'db.inc.php';

    $db mysql_connect(MYSQL_HOSTMYSQL_USERMYSQL_PASSWORD) or 
        die (
    'Unable to connect. Check your connection parameters.');
    mysql_select_db(MYSQL_DB$db) or die(mysql_error($db));
    ?>
    <html>
     <head>
      <title>CRM Verde Profilo</title>
    <link rel="stylesheet" type="text/css" href="css/style.css">
    <link rel="Shotcut Icon" href="VPfavicon.ico" />
      <style type="text/css">
       th { background-color: #999;color:#fff;}
       .odd_row { background-color: #EEE; }
       .even_row { background-color: #FFF; }
    .mainform_admin {
        width: auto !important;
    }
      </style>
     </head>
     <body>
         <div class="mainform_admin" >
         
      
    <?php

        
    if(empty($_GET['ordercampo'])) {
                 
    $ciao "Nome_Cognome";
        }
        else {
            
    $ciao $_GET['ordercampo'];
        }
                 if(empty(
    $_GET['order'])) {
                 
    $_GET['order'] = "asc";
                 }
                 if(
    $_GET['order']=="asc") {$ordinaVerso "desc";} 
                 if(
    $_GET['order']=="desc") {$ordinaVerso "asc";} 
                 
    $query "SELECT
                                *
                            FROM
                                contatti, Lista
                            WHERE
                                contatti.Lista_id = Lista.Lista_id 
                            AND   
                                contatti.Lista_id = " 
    $_GET['Lista_id'] ." ORDER BY ".  $ciao ." "$ordinaVerso;
                                
                                
                 
    $result mysql_query($query) or die(mysql_error()); 
                 
                 

    $odd true

    while (
    $row mysql_fetch_assoc($result)) { 

        
    $tel substr($row['Telefono'], 01);
        
        

        if (
    $tel == || $tel == || $tel == || $tel == || $tel == || $tel == || $tel == || $tel == || $tel == 9) {
            
            
    $tel_new "0".$row['Telefono'];    
            
        }
        
        else {
            
    $tel_new $row['Telefono'];
        }
        
        
    $tel2 substr($row['Telefono'], 01);
        
        

        if (
    $tel2 == || $tel2 == || $tel2 == || $tel2 == || $tel2 == || $tel2 == || $tel2 == || $tel2 == || $tel2 == 9) {
            
            
    $tel2_new "0".$row['Telefono_2'];    
            
        }
        
        else {
            
    $tel2_new $row['Telefono_2'];
        }
        
        
    $fax substr($row['Fax'], 01);
        
        if (
    $fax == || $fax == || $fax == || $fax == || $fax == || $fax == || $fax == || $fax == || $fax == 9) {
            
            
    $fax_new "0".$row['Fax'];    
            
        }
        
        else {
            
    $fax_new $row['Fax'];
        }
        
        
    $url substr($row['Sito'], 03);
        
        

        if (
    $url == 'www') {
            
            
    $url_new "http://".$row['Sito'];    
            
        }
        
        else {
            
    $url_new $row['Sito'];
        }
        
        

        if (
    $lista !== $row['Lista']) {
           
    $titolo_lista $row['Lista'];
           
    $lista $titolo_lista;
           echo 
    '<div>
                [img]images/logo.png[/img]
                  <h1 class="h1">CRM Verde Profilo</h1>
                  <p class="p_main_contact">Gestione DataBase Contatti | [url="logout.php"]Disconnetti[/url]</p>
                  <a href="select_db.php">
                    <div class="btn_indietro">
                      [img]images/back.png[/img]Torna Indietro</div>
                  </a>
                  <a href="contatti.php?action=add"> 
                    <div class="btn_add_user">
                      [img]images/add_user.png[/img]Aggiungi Nuovo</div>
                  </a>
                  <h2 class="h2">Contatti</h2>
              </div>'
    ;  
           echo 
    '<table class="table_contact" > 
          <tr> 
           <th colspan="14">'
    $titolo_lista .'</th> 
          </tr>'

        echo 
    '<tr id="intestazione">';  
        echo 
    '[b]<td class="tb_subtitle">';  
        echo 
    'Titolo'
        echo 
    '</td>';
        echo 
    '<td class="tb_subtitle">';  
        echo 
    'Ragione Sociale'
        echo 
    '</td>';
        echo 
    '<td class="tb_subtitle">';  
        echo 
    'Nome Cognome'
        echo 
    '</td>';
        echo 
    '<td class="tb_subtitle">';  
        echo 
    'Indirizzo'
        echo 
    '</td>';
        echo 
    '<td class="tb_subtitle">';  
        echo 
    'Indirizzo alternativo'
        echo 
    '</td>';
        echo 
    '<td class="tb_subtitle">';  
        echo 
    'CAP'
        echo 
    '</td>';
        echo 
    '<td class="tb_subtitle">';  
        echo 
    'Localita'
        echo 
    '</td>'
        echo 
    '<td class="tb_subtitle">';  
        echo 
    'Provincia'
        echo 
    '</td>';
        echo 
    '<td class="tb_subtitle">';  
        echo 
    'Nazione'
        echo 
    '</td>';
        echo 
    '<td class="tb_subtitle">';  
        echo 
    'Telefono'
        echo 
    '</td>';
        echo 
    '<td class="tb_subtitle">';  
        echo 
    'Telefono alternativo'
        echo 
    '</td>';
        echo 
    '<td class="tb_subtitle">';  
        echo 
    'Fax'
        echo 
    '</td>';
        echo 
    '<td class="tb_subtitle">';  
        echo 
    'Cellulare'
        echo 
    '</td>';
        echo 
    '<td class="tb_subtitle">';  
        echo 
    'Email'
        echo 
    '</td>';
        echo 
    '<td class="tb_subtitle">';  
        echo 
    'Sito Web'
        echo 
    '</td>';
        echo 
    '<td class="tb_subtitle">';  
        echo 
    'Partita IVA'
        echo 
    '</td>';
        echo 
    '<td class="tb_subtitle">';  
        echo 
    'Note'
        echo 
    '</td>';
        echo 
    '<td class="tb_subtitle">';  
        echo 
    'Lista'
        echo 
    '</td>[/b]</tr>';   
        }    
        
        else {
           
    $titolo_lista "";
        }
        
        
        
    $tit html_entity_decode($row['Titolo']);
        
    $rag html_entity_decode($row['Ragione_Sociale']);
        
    $Nome html_entity_decode($row['Nome_Cognome']);
        
    $ind html_entity_decode($row['Indirizzo']);
        
    $ind2 html_entity_decode($row['Indirizzo_2']);
        
    $loc html_entity_decode($row['Localita']);
        
    $mail html_entity_decode($row['Email']);
        
    $url html_entity_decode($row['Sito']);
        
    $note html_entity_decode($row['Note']);
        
          
        echo (
    $odd == true) ? '<tr class="odd_row">' '<tr class="even_row">'
        
    $odd = !$odd;  
        echo 
    '<td style="width:8%;">';  
        echo 
    $tit;
        echo 
    '</td>';
        echo 
    '<td style="width:8%;">';  
        echo 
    $rag;
        echo 
    '</td>';
        echo 
    '<td style="width:8%;">';  
        echo 
    $Nome;
        echo 
    '</td>';
        echo 
    '<td style="width:8%;">';  
        echo 
    $ind;
        echo 
    '</td>';
        echo 
    '<td style="width:8%;">';  
        echo 
    $ind2;
        echo 
    '</td>';
        echo 
    '<td style="width:8%;">';  
        echo 
    $row['CAP']; 
        echo 
    '</td>';
        echo 
    '<td style="width:8%;">';  
        echo 
    $loc
        echo 
    '</td>'
        echo 
    '<td style="width:8%;">';  
        echo 
    $row['Provincia']; 
        echo 
    '</td>';
        echo 
    '<td style="width:8%;">';  
        echo 
    $row['Nazione']; 
        echo 
    '</td>';
        echo 
    '<td style="width:8%;">';  
        echo 
    $tel_new
        echo 
    '</td>';
        echo 
    '<td style="width:8%;">';  
        echo 
    $tel2_new
        echo 
    '</td>';
        echo 
    '<td style="width:8%;">';  
        echo 
    $fax_new
        echo 
    '</td>';
        echo 
    '<td style="width:8%;">';  
        echo 
    $row['Cellulare']; 
        echo 
    '</td>';
        echo 
    '<td style="width:8%;">';  
        echo 
    '[url='.']'.$mail.'[/url]'
        echo 
    '</td>';
        echo 
    '<td style="width:8%;">';  
        echo 
    '[url='.$url_new.']'.$url.'[/url]'
        echo 
    '</td>';
        echo 
    '<td style="width:8%;">';  
        echo 
    $row['Partita_iva']; 
        echo 
    '</td>';
        echo 
    '<td style="width:8%;">';  
        echo 
    $note
        echo 
    '</td>';
        echo 
    '<td style="width:8%;">';  
        echo 
    $row['Lista']; 
        echo 
    '</td><td>'
        echo 
    ' [url="contatti.php?action=edit&id=' $row['movie_id'] . '"] [EDIT][/url]';  
        echo 
    ' [url="delete.php?type=contatti&id=' $row['movie_id'] . '"] [DELETE][/url]'
        echo 
    ' [url="duplicate.php?id=' $row['movie_id'] . '"] [DUPLICATE][/url]'
        echo 
    '</td></tr>'
    }


    ?>
    [url="admin.php?ordercampo=<?php echo 'Nome_Cognome' ?>&order=<?php echo $ordinaVerso ?>"]link[/url]
    </div>
    </body>
    </html>

  2. #2
    Utente di HTML.it L'avatar di homerbit
    Registrato dal
    Dec 2005
    residenza
    Roma
    Messaggi
    1,380
    ma non bastava postare solo la query
    hai dimenticato dei doppi apici, così dovrebbe andare:
    Codice PHP:
               $query "SELECT *   FROM
                                contatti, Lista
                            WHERE
                                contatti.Lista_id = Lista.Lista_id 
                            AND   
                                contatti.Lista_id =  "
    $_GET['Lista_id'] ." ORDER BY ".  $ciao ." "$ordinaVerso ." "
    If you think your users are idiots, only idiots will use it. DropBox

  3. #3
    Utente di HTML.it
    Registrato dal
    Apr 2004
    Messaggi
    3,709
    non serve postare tutto il codice così... difficilmente sarà letto al primo contatto... prova invece prima della riga $result = mysql_query($query) or die(mysql_error()); a mettere var_export($query);exit; e a postare qui la query risultante che sarà stampata

  4. #4
    Utente di HTML.it
    Registrato dal
    Nov 2007
    Messaggi
    60
    Grazie a entrambi, credevo più facile postando tutto (la prossima volta non lo farò).

    Il problema dei doppi apici non ha risolto, da sempre lo stesso problema.

    Mettendo var export la risposta è stata:

    codice:
    'SELECT * FROM contatti, Lista WHERE contatti.Lista_id = Lista.Lista_id AND contatti.Lista_id = ORDER BY Nome_Cognome asc '

  5. #5
    Utente di HTML.it L'avatar di homerbit
    Registrato dal
    Dec 2005
    residenza
    Roma
    Messaggi
    1,380
    sicuro di aver validato $_GET['Lista_id'] ?
    If you think your users are idiots, only idiots will use it. DropBox

  6. #6
    Utente di HTML.it
    Registrato dal
    Nov 2007
    Messaggi
    60
    Scusate sono un deficiente!

    Mi potete anche bannare dal forum ora!

    Mi dispiace!

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.