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

    [HELP] - Update PHP MYSQL

    Ciao ragazzi, ho scritto questo codice per aggiornare dei campi di una table:
    Codice PHP:
    <?php
    $host
    ="localhost";
    $username="root";
    $password="";
    $database="vetture";

    $conn=mysql_connect($host,$username,$password);
    mysql_select_db($database) or die("Impossibile selezionare il database");
     
    if(
    $_POST && isset($_GET['id']))
    {
    aggiorna_record();}
    elseif(isset(
    $_GET['id']))
    {
    mostra_record();}
    else
        
    mostra_lista();
     
    function 
    mostra_lista()
    {
        if(isset(
    $_GET['msg']))
            echo 
    '[b]'.htmlentities($_GET['msg']).'[/b]

    '
    ;

        
    $query "SELECT id,uid,upwd FROM members";
        
    $result mysql_query($query);
        if (!
    $result) {
            die(
    "Errore nella query $query: " mysql_error());
        }
        echo 
    '
        <table border="1">
            <tr>
                <th>id</th>
                <th>utente</th>
                <th>password</th>
                <th></th>
            </tr>'
    ;
         while (
    $row mysql_fetch_assoc($result))
        {
            
    $id=htmlspecialchars($row['id']);
            
    $uid=htmlspecialchars($row['uid']);
            
    $upwd=htmlspecialchars($row['upwd']);
            
    $link $_SERVER['PHP_SELF'] . '?id=' $row['id'];
     
            echo 
    "<tr>
                    <td>
    $id</td>
                    <td>
    $uid</td>
                    <td>
    $upwd</td>
                    <td><a href=\"
    $link\">modifica</a></td>
                </tr>"
    ;
        }
        echo 
    '</table>';
        
    mysql_free_result($result);
        
    mysql_close();
    }

    function 
    aggiorna_record()
    {
        
    // recupero i campi di tipo "stringa"
        
    $id=intval($_GET['id']);
        
    $uid=trim($_POST['uid']);
        
    $upwd=trim($_POST['upwd']);
     
        
    // preparo la query
        
    $query "UPDATE members SET uid='$uid', upwd='$upwd', WHERE id=$id";
        
    $result mysql_query($query);
        if (!
    $result) {
            die(
    "Errore nella query $query: " mysql_error());
        }
        
    mysql_close($conn);
     
        
    $messaggio urlencode('Aggiornamento effettuato con successo');
        
    header("location: $_SERVER[PHP_SELF]?msg=$messaggio");
    }


    function 
    mostra_record()

        
    $id intval($_GET['id']);
        
    $query "SELECT id,uid,upwd, FROM members WHERE id = $id";
        
    $result mysql_query($query);
        if (!
    $result
        { die(
    "Errore nella query $query: " mysql_error()); }
        
        if(
    mysql_num_rows($result) != 1
        { die(
    "l'ID passato via GET è errato"); }
     
        list(
    $uid,$upwd,)=mysql_fetch_row($result);
     
        
    $uid=htmlspecialchars($uid);
        
    $upwd=htmlspecialchars($upwd);

     echo 
    "
          utente
          <input name='utente' type='text' value=<?php echo 
    $uid?>/>
          password
          <input name='password' type='text' value=<?php echo 
    $upwd?>/>
          <input name='invia' type='submit' value='Invia'/> "
    };
    ?>
    Ora quando lo vado ad eseguire mi da il seguente errore:
    Parse error: syntax error, unexpected '}', expecting ',' or ';' in C:\xampp\htdocs\upd.php on line 96
    Sembra che manchi una parentesi, una virgola o un punto e virgola ma vedendolo non vedo dove...
    Mi date una manina a trovare l'intoppo perchè magari a me sfugge qualcosa?
    By GeddY_LeE

  2. #2
    Utente di HTML.it L'avatar di bstefano79
    Registrato dal
    Feb 2004
    Messaggi
    2,520
    in fondo ha invertito } e il ;

    da
    Codice PHP:
    echo " 
          utente 
          <input name='utente' type='text' value=<?php echo $uid?>/> 
          password 
          <input name='password' type='text' value=<?php echo $upwd?>/> 
          <input name='invia' type='submit' value='Invia'/> " 
    }; 
    ?>
    a
    Codice PHP:
     echo " 
          utente 
          <input name='utente' type='text' value=<?php echo $uid?>/> 
          password 
          <input name='password' type='text' value=<?php echo $upwd?>/> 
          <input name='invia' type='submit' value='Invia'/> "; 

    ?>

  3. #3

    Re: [HELP] - Update PHP MYSQL

    Originariamente inviato da GeddY_LeE
    ...
    Parse error: syntax error, unexpected '}', expecting ',' or ';' in C:\xampp\htdocs\upd.php on line 96
    Sembra che manchi una parentesi, una virgola o un punto e virgola ma vedendolo non vedo dove...
    Scusa, non voglio polemizzare, ma il compilatore ti dice perfino la riga nella quale si trova l'errore e guardando alla riga indicata l'errore è ovvio!!!
    "Mai discutere con un idiota. Ti trascina al suo livello e ti batte con l'esperienza." (Oscar Wilde)

  4. #4
    Per ora ho risolto! Grazie mille bstefano79!!! nel caso sorgano altri errori posso postare in questo stesso thread senza che ne apro degli altri inutilmente? ^^
    By GeddY_LeE

  5. #5

    Re: Re: [HELP] - Update PHP MYSQL

    Originariamente inviato da satifal
    il compilatore ti dice perfino la riga nella quale si trova l'errore e guardando alla riga indicata l'errore è ovvio!!!
    Io ci guardavo all'ultima e infatti il problema come ha fatto notare bstefano79 era la riga sopra
    By GeddY_LeE

  6. #6
    Ora quando clicco sul link modifica mi risponde col seguente errore:
    Errore nella query SELECT id,uid,upwd, FROM members WHERE id = 6: 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 'FROM members WHERE id = 6' at line 1
    che sintassi dovrei usare? :S
    By GeddY_LeE

  7. #7
    la virgola rossa è di troppo
    SELECT id,uid,upwd, FROM members WHERE id = 6

  8. #8
    Originariamente inviato da GeddY_LeE
    Ora quando clicco sul link modifica mi risponde col seguente errore:
    Errore nella query SELECT id,uid,upwd, FROM members WHERE id = 6: 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 'FROM members WHERE id = 6' at line 1
    che sintassi dovrei usare? :S
    Non ci vuole la virgola dopo upwd e prima di FROM.

    P.S.
    Anche in questo caso il messaggio di errore era chiarissimo, c'era un problema prima del FROM!!!
    "Mai discutere con un idiota. Ti trascina al suo livello e ti batte con l'esperienza." (Oscar Wilde)

  9. #9
    ahahah vero! mannaggia!! ragà oggi sto un pò "addormentato" cavolo! scusate se vi faccio perdere tempo! :/
    By GeddY_LeE

  10. #10
    Ora in tutti i vari passaggi non ci sono più errori, ma c'è un problema inizio con l'incollarvi il codice aggiornato:

    Codice PHP:
    <?php
    $host
    ="localhost";
    $username="root";
    $password="";
    $database="vetture";

    $conn=mysql_connect($host,$username,$password);
    mysql_select_db($database) or die("Impossibile selezionare il database");
     
    if(
    $_POST && isset($_GET['id']))
    {
    aggiorna_record();}
    elseif(isset(
    $_GET['id']))
    {
    mostra_record();}
    else
        
    mostra_lista();
     
    function 
    mostra_lista()
    {
        if(isset(
    $_GET['msg']))
            echo 
    '[b]'.htmlentities($_GET['msg']).'[/b]

    '
    ;

        
    $query "SELECT id, uid, upwd FROM members";
        
    $result mysql_query($query);
        if (!
    $result) {
            die(
    "Errore nella query $query: " mysql_error());
        }
        echo 
    '
        <table border="1">
            <tr>
                <th>id</th>
                <th>utente</th>
                <th>password</th>
                <th></th>
            </tr>'
    ;
         while (
    $row mysql_fetch_assoc($result))
        {
            
    $id=htmlspecialchars($row['id']);
            
    $uid=htmlspecialchars($row['uid']);
            
    $upwd=htmlspecialchars($row['upwd']);
            
    $link $_SERVER['PHP_SELF'] . '?id=' $row['id'];
     
            echo 
    "<tr>
                    <td>
    $id</td>
                    <td>
    $uid</td>
                    <td>
    $upwd</td>
                    <td><a href=\"
    $link\">modifica</a></td>
                </tr>"
    ;
        }
        echo 
    '</table>';
        
    mysql_free_result($result);
        
    mysql_close();
    }


    function 
    mostra_record()

        
    $id intval($_GET['id']);
        
    $query "SELECT id,uid,upwd FROM members WHERE id = $id";
        
    $result mysql_query($query);
        if (!
    $result
        { die(
    "Errore nella query $query: " mysql_error()); }
        
        if(
    mysql_num_rows($result) != 1
        { die(
    "l'ID passato via GET è errato"); }
     
        list(
    $id,$uid,$upwd)=mysql_fetch_row($result);
     
        
    $id=htmlspecialchars($id);
        
    $uid=htmlspecialchars($uid);
        
    $upwd=htmlspecialchars($upwd);

     echo 
    "
          <form action=\"upr.php\" method=\"post\">
          utente:  
          <input name='utente' type='text' value=\"
    $uid\"/>
      
          password:  
          <input name='password' type='text' value=\"
    $upwd\"/>  
          <input name='invia' type='submit' value='Invia'/>
          </form>
          "
    ;

    }
    ?>
    Questa è la pagina che visaulizza i record, per ognuno mi da un link per la modifica dove clicco e mi porta alla sezione function mostra_record() dove inserisco i nuovi dati da modificare e alla pressione del pulsante Invia che richiama la pagina upr.php quì sotto riportata:

    Codice PHP:
    <?php
    function aggiorna_record()
    {  
        
    // recupero i campi di tipo "stringa"
        
    $id=intval($_GET['id']);
        
    $uid=trim($_POST['uid']);
        
    $upwd=trim($_POST['upwd']);

        if(
    get_magic_quotes_gpc())
        {
            
    $id stripslashes($id);
            
    $uid stripslashes($uid);
            
    $upwd stripslashes($upwd);
        }
        
    $id mysql_real_escape_string($id);
        
    $uid mysql_real_escape_string($uid);
        
    $upwd mysql_real_escape_string($upwd);

        
    $query "UPDATE members SET uid='$uid', upwd='$upwd' WHERE id=$id";
        
    $result mysql_query($query);
        if (!
    $result) {
            die(
    "Errore nella query $query: " mysql_error());
        }
        
    mysql_close($conn);
     
        
    $id urlencode('Aggiornamento effettuato con successo');
        
    header("location: $_SERVER[PHP_SELF]?msg=$id");
    }
    ?>
    Ora mi riporta a questa pagina però non mi modifica i dati.
    Dove sbaglio?

    Se può esservi utile ecco a voi anche la struttura della table:

    id int(10) unsigned PRI NULL auto_increment
    uid char(30)
    upwd char(30)
    By GeddY_LeE

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.