Pagina 1 di 2 1 2 ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 12

Discussione: ciclo for

  1. #1
    Utente di HTML.it
    Registrato dal
    May 2006
    Messaggi
    442

    ciclo for

    Codice PHP:
     session_start();
    if(!isset(
    $_SESSION['nick']))
    {
    header("Location:logout.php");
    exit();
    }

    if(
    $_GET)
                {
                
    $id=$_GET['id'];
                
    $i=$_GET['i'];    
                echo 
    "a".$id."b".$i;
    include 
    'include/header.inc.php';
    include 
    'include/left.inc.php';
         echo
    " <td bgcolor=#ffcc66><center>";            
                echo 
    "
                
                <form method=post action=\"
    $_SERVER[PHP_SELF]\">
                <table>
                <tr>
                <td>
                <caption>Inserimento nuovi prodotti </caption>
                </td>
                </tr>
                </table>
                <hr>

                "
    ;
                for(
    $j=0;$j<$i;$j++)
                {
                echo 
    "
                        <table>
                        <tr>
                        <td>Marca</td>
                        <td><input type=text name=marca[
    $j]></td>
                        </tr>
                        <tr>
                        <td>Modello</td>
                        <td><input type=text name=mod[
    $j]></td>
                        </tr>
                        <tr>
                        <td>Colore</td>
                        <td><input type=text name=col[
    $j]></td>
                        </tr>
                        <tr>
                        <td>Origine</td>
                        <td><input type=text name=orig[
    $j]></td>
                        </tr>
                        <tr>
                        <td>Disponibilità</td>
                        <td><input type=text name=disp[
    $j]></td>
                        </tr>
                        <tr>
                        <td>Prezzo Privato</td>
                        <td><input type=text name=prezzo[
    $j]></td>
                        </tr>
                        <tr>
                        <td>Prezzo Azienda</td>
                        <td><input type=text name=prezzoaz[
    $j]></td>
                        </tr>
                        <tr>
                        <td>Prezzo Rivenditori</td>
                        <td><input type=text name=prezzoriv[
    $j]></td>
                        </tr>
                    
                        </table>
                        <hr>

                    "
    ;
                }
                
                echo 
    "
                <table>
                <tr>
                <td>
                <input type=hidden name=id value=
    $id>
                <input type=submit name=submit value=Edita>
                </td>
                </tr>
                </table>
                </form>
                </center>
                "
    ;
                echo 
    "</td>";
         include 
    'include/right.inc.php';
            include 
    'include/footer.inc.php';
                
                }
    else if(
    $_POST)
    {
    $marca=addslashes($_POST['marca']);
         
    $mod=addslashes($_POST['mod']);
         
    $col=addslashes($_POST['col']);
         
    $orig=addslashes($_POST['orig']);
         
    $disp=addslashes($_POST['disp']);
         
    $prezzo=addslashes($_POST['prezzo']);
         
    $prezzoaz=addslashes($_POST['prezzoaz']);
         
    $prezzoriv=addslashes($_POST['prezzoriv']);
         
    $id=addslashes($_POST['id']);
         include 
    'include/conf.inc.php';
         
    $sql=mysql_query("update tbl_prodotti set marca='$marca',modello='$mod',tipo='$col',origine='$orig',qt='$disp',prezzo='$prezzo',prezzoaz='$prezzoaz',prezzoriv='$prezzoriv' where id_categorie='$id'").mysql_error();

    allora come si vede creo tramite un ciclo for una tabella per l'inserimento delle caratteristiche di un prodotto. tramiter for ne creo quanti ne vuole inserire l'utente ossia $i

    il problema è la query
    come inserire i vari dati tramite queri essendo che tramite la query che ho scritto mi sovvrascrive i valori

    grazie per la dispon.

  2. #2
    Tralasciando la form, la parte rilevante di quello che chiedi è la gestione di MySQL.

    La prima imprecisione che vedo è l'uso di addslashes(): i dati da inviare a MySQL devono passare per mysql_escape_string() (o meglio mysql_real_escape_string() se hai la risorsa connessione), non da addslashes(). Inoltre, i dati che ricevi dai post possono essere già slashati se la direttiva 'magic_quotes_gpc' è on, per cui scriverei una funzione preventiva:

    Codice PHP:
    function prepareSqlText(&$gpc) {
        
    get_magic_quotes_gpc() && $gpc stripslashes($gpc);
        
    $gpc mysql_escape_string($gpc);

    Il secondo errore è che tratti i dati come entità unica mentre in realtà sono vettori (se la form è corretta), quindi:

    Codice PHP:
    function prepareSqlVar(&$var) {
        if (
    is_array($var)) {
            
    array_walk($var'prepareSqlVar');
        } else {
            
    prepareSqlText($var);
        }

    Terzo, usi il comando SQL errato per "inserire" i dati: UPDATE serve per modificarli (o sovrascriverli, come dici tu):

    Codice PHP:
    // Prepara TUTTI i post per MySQL
    prepareSqlVar($_POST);

    // Questi i campi da inserire nella tabella
    $fields = array('id''marca''mod''col''orig''disp''prezzo''prezzoaz''prezzoriv');

    // Questi i valori
    for ($n 0$n count($_POST['id']); ++ $n) {
        foreach (
    $fields as $field) {
            
    $row[$field] = $_POST[$field][$n];
        }
        
    $values[$n] = '(' implode(','$row) . ')';
    }

    // Questa la query
    $query 'INSERT INTO tbl_prodotti (' implode(','$fields) . ') VALUES ' implode(','$values);

    // Questo per vedere come è fatta la query insert con più righe
    echo "<pre>$query</pre>";

    // Esecuzione della query
    mysql_query($query) || die(mysql_error()); 

  3. #3
    Utente di HTML.it
    Registrato dal
    May 2006
    Messaggi
    442
    ho fatto come mi hai detto
    Codice PHP:
     $marca=addslashes($_POST['marca']);
         
    $mod=addslashes($_POST['mod']);
         
    $col=addslashes($_POST['col']);
         
    $orig=addslashes($_POST['orig']);
         
    $disp=addslashes($_POST['disp']);
         
    $prezzo=addslashes($_POST['prezzo']);
         
    $prezzoaz=addslashes($_POST['prezzoaz']);
         
    $prezzoriv=addslashes($_POST['prezzoriv']);
         
    $id=addslashes($_POST['id']);
         include 
    'include/conf.inc.php';
         
         function 
    prepareSqlText(&$gpc) {
        
    get_magic_quotes_gpc() && $gpc stripslashes($gpc);
        
    $gpc mysql_escape_string($gpc);
    }

    function 
    prepareSqlVar(&$var) {
        if (
    is_array($var)) {
            
    array_walk($var'prepareSqlVar');
        } else {
            
    prepareSqlText($var);
        }
    }

    // Prepara TUTTI i post per MySQL
    prepareSqlVar($_POST);

    // Questi i campi da inserire nella tabella
    $fields = array('''marca''mod''col''orig''disp''prezzo''prezzoaz''prezzoriv','','','id');

    // Questi i valori
    for ($n 0$n count($_POST['id']); ++ $n) {
        foreach (
    $fields as $field) {
            
    $row[$field] = $_POST[$field][$n];
        }
        
    $values[$n] = '(' implode(','$row) . ')';
    }

    // Questa la query
    $query 'INSERT INTO tbl_prodotti (' implode(','$fields) . ') VALUES ' implode(','$values);

    // Questo per vedere come è fatta la query insert con più righe
    echo "<pre>$query</pre>";

    // Esecuzione della query
    mysql_query($query) || die(mysql_error()); 
    ma mi DAQUESTO ERRORE>


    INSERT INTO tbl_prodotti (,marca,mod,col,orig,disp,prezzo,prezzoaz,prezzori v,,,id) VALUES (,rterte,366,54,gdgdf,654,5467567,7567567,757657,1 )
    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 'marca,mod,col,orig,disp,prezzo,prezzoaz,prezzoriv ,,,id) VALUES (

  4. #4
    Vero, mi sono dimenticato di includere i nomi di campi nei backticks (mod è una parola chiave) e i valori in apici:

    Codice PHP:
    include 'include/conf.inc.php';

    function 
    prepareSqlText(&$gpc) {
        
    get_magic_quotes_gpc() && $gpc stripslashes($gpc);
        
    $gpc mysql_escape_string($gpc);
    }

    function 
    prepareSqlVar(&$var) {
        if (
    is_array($var)) {
            
    array_walk($var'prepareSqlVar');
        } else {
            
    prepareSqlText($var);
        }
    }

    // Prepara TUTTI i post per MySQL
    prepareSqlVar($_POST);

    // Questi i campi da inserire nella tabella
    $fields = array('id''marca''mod''col''orig''disp''prezzo''prezzoaz''prezzoriv');

    // Questi i valori
    for ($n 0$n count($_POST['id']); ++ $n) {
        foreach (
    $fields as $field) {
            
    $row[$field] = $_POST[$field][$n];
        }
        
    $values[$n] = "('" implode("','"$row) . "')";
    }

    // Questa la query
    $query 'INSERT INTO tbl_prodotti (`' implode('`,`'$fields) . '`) VALUES ' implode(','$values);

    // Questo per vedere come è fatta la query insert con più righe
    echo "<pre>$query</pre>";

    // Esecuzione della query
    mysql_query($query) || die(mysql_error()); 
    Nella riga $values=... sono stati aggiunti apici per contenere un valore stringa.

    Nella riga $query=... invece sono stati aggiunti dei backticks (apici inversi): non sono apici normali (e non so come farli saltar fuori da Windows). Consiglio un copincolla rapido ed indolore.

    Può darsi ci siano altre imprecisioni, il programma non l'ho provato. Gli assegnamenti da $marca=... fino $id=... non sono usati, ergo puoi toglierli (lasciando ovviamente l'include per la configurazione della connessione).

    PS: il vettore $fields non può avere valori vuoti: perché li hai aggiunti?

  5. #5
    Utente di HTML.it
    Registrato dal
    May 2006
    Messaggi
    442
    ti rimetto il codice:

    Codice PHP:
    $marca=addslashes($_POST['marca']);
         
    $mod=addslashes($_POST['mod']);
         
    $col=addslashes($_POST['col']);
         
    $orig=addslashes($_POST['orig']);
         
    $disp=addslashes($_POST['disp']);
         
    $prezzo=addslashes($_POST['prezzo']);
         
    $prezzoaz=addslashes($_POST['prezzoaz']);
         
    $prezzoriv=addslashes($_POST['prezzoriv']);
         
    $id=addslashes($_POST['id']);
         include 
    'include/conf.inc.php';
         
         function 
    prepareSqlText(&$gpc) {
        
    get_magic_quotes_gpc() && $gpc stripslashes($gpc);
        
    $gpc mysql_escape_string($gpc);
    }

    function 
    prepareSqlVar(&$var) {
        if (
    is_array($var)) {
            
    array_walk($var'prepareSqlVar');
        } else {
            
    prepareSqlText($var);
        }
    }

    // Prepara TUTTI i post per MySQL
    prepareSqlVar($_POST);

    // Questi i campi da inserire nella tabella
    $fields = array('''marca''mod''col''orig''disp''prezzo''prezzoaz''prezzoriv''''''id');

    // Questi i valori
    for ($n 0$n count($_POST['id']); ++ $n) {
        foreach (
    $fields as $field) {
            
    $row[$field] = $_POST[$field][$n];
        }
         
    $values[$n] = "('" implode("','"$row) . "')"
    }

    // Questa la query
    $query 'INSERT INTO tbl_prodotti (`' implode('`,`'$fields) . '`) VALUES ' implode(','$values);

    // Questo per vedere come è fatta la query insert con più righe
    echo "<pre>$query</pre>";

    // Esecuzione della query
    mysql_query($query) || die(mysql_error());
         

    mi da ilseguente errore:

    INSERT INTO tbl_prodotti (``,`marca`,`mod`,`col`,`orig`,`disp`,`prezzo`,`pr ezzoaz`,`prezzoriv`,``,``,`id`) VALUES ('','r','3','2','g','3','2','2','3','1')
    Column count doesn't match value count at row 1

  6. #6
    Originariamente inviato da ntd
    Gli assegnamenti da $marca=... fino $id=... non sono usati, ergo puoi toglierli (lasciando ovviamente l'include per la configurazione della connessione).

    PS: il vettore $fields non può avere valori vuoti: perché li hai aggiunti?
    Ma leggi quello che ti scrivo?

  7. #7
    Utente di HTML.it
    Registrato dal
    May 2006
    Messaggi
    442
    PS: il vettore $fields non può avere valori vuoti: perché li hai aggiunti?
    il primo spazio vuoto è il campo id auto-increment gli altri sono campi che temporaneamente non uso

    quindi come dovrei fare?
    grazie per la disp.

  8. #8
    Porongo, mi sono accorto solo ora di un problema di consistenza.

    Primo: modifica la tua form in modo che gli attributi "name" siano UGUALI ai nomi dei campi della tabella, per esempio:

    Codice PHP:
    // Questo NON VA BENE
    <input type=hidden name=id value=$id>
    // deve diventare id_categorie, perché $id deve registrarsi nel campo 'id_categorie',
    // quindi sostituiscilo con:
    <input type=hidden name=id_categorie value=$id
    Quando sei sicuro che la form è corretta, cambia $fields come segue:

    Codice PHP:
    $fields = array('id_categorie''marca''modello''tipo''origine''qt''prezzo''prezzoaz''prezzoriv'); 

  9. #9
    Utente di HTML.it
    Registrato dal
    May 2006
    Messaggi
    442
    Codice PHP:
     echo "
                
                <form method=post action=\"
    $_SERVER[PHP_SELF]\">
                <table>
                <tr>
                <td>
                <caption>Inserimento nuovi prodotti </caption>
                </td>
                </tr>
                </table>
                <hr>

                "
    ;
                for(
    $j=0;$j<$i;$j++)
                {
                echo 
    "
                        <table>
                        <tr>
                        <td>Marca</td>
                        <td><input type=text name=marca></td>
                        </tr>
                        <tr>
                        <td>Modello</td>
                        <td><input type=text name=modello></td>
                        </tr>
                        <tr>
                        <td>Colore</td>
                        <td><input type=text name=tipo></td>
                        </tr>
                        <tr>
                        <td>Origine</td>
                        <td><input type=text name=origine></td>
                        </tr>
                        <tr>
                        <td>Disponibilit?/td>
                        <td><input type=text name=qt></td>
                        </tr>
                        <tr>
                        <td>Prezzo Privato</td>
                        <td><input type=text name=prezzo></td>
                        </tr>
                        <tr>
                        <td>Prezzo Azienda</td>
                        <td><input type=text name=prezzoaz></td>
                        </tr>
                        <tr>
                        <td>Prezzo Rivenditori</td>
                        <td><input type=text name=prezzoriv></td>
                        </tr>
                    
                        </table>
                        <hr>

                    "
    ;
                }
                
                echo 
    "
                <table>
                <tr>
                <td>
                <input type=hidden name=id_categorie value=
    $id>
                <input type=submit name=submit value=Edita>
                </td>
                </tr>
                </table>
                </form>
                </center>
                "
    ;
                echo 
    "</td>";
         include 
    'include/right.inc.php';
            include 
    'include/footer.inc.php';
                
                }
    else if(
    $_POST)
    {
    $marca=addslashes($_POST['marca']);
         
    $modello=addslashes($_POST['modello']);
         
    $tipo=addslashes($_POST['tipo']);
         
    $origine=addslashes($_POST['origine']);
         
    $qt=addslashes($_POST['qt']);
         
    $prezzo=addslashes($_POST['prezzo']);
         
    $prezzoaz=addslashes($_POST['prezzoaz']);
         
    $prezzoriv=addslashes($_POST['prezzoriv']);
         
    $id_categorie=addslashes($_POST['id_categorie']);
         include 
    'include/conf.inc.php';
         
         function 
    prepareSqlText(&$gpc) {
        
    get_magic_quotes_gpc() && $gpc stripslashes($gpc);
        
    $gpc mysql_escape_string($gpc);
    }

    function 
    prepareSqlVar(&$var) {
        if (
    is_array($var)) {
            
    array_walk($var'prepareSqlVar');
        } else {
            
    prepareSqlText($var);
        }
    }

    // Prepara TUTTI i post per MySQL
    prepareSqlVar($_POST);

    // Questi i campi da inserire nella tabella
    $fields = array('marca''modello''tipo''origine''qt''prezzo''prezzoaz''prezzoriv','id_categorie');

    // Questi i valori
    for ($n 0$n count($_POST['id']); ++ $n) {
        foreach (
    $fields as $field) {
            
    $row[$field] = $_POST[$field][$n];
        }
         
    $values[$n] = "('" implode("','"$row) . "')"
    }

    // Questa la query
    $query 'INSERT INTO tbl_prodotti (`' implode('`,`'$fields) . '`) VALUES ' implode(','$values);

    // Questo per vedere come è fatta la query insert con più righe
    echo "<pre>$query</pre>";

    // Esecuzione della query
    mysql_query($query) || die(mysql_error());
         

    mi da il seguente errore:

    INSERT INTO tbl_prodotti (`marca`,`modello`,`tipo`,`origine`,`qt`,`prezzo`, `prezzoaz`,`prezzoriv`,`id_categorie`) VALUES
    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 '' at line 1


    l'hai provato te?
    grazie per la disp.

  10. #10
    Utente di HTML.it
    Registrato dal
    May 2006
    Messaggi
    442
    Codice PHP:
     <?
    session_start
    ();
    if(!isset(
    $_SESSION['nick']))
    {
    header("Location:logout.php");
    exit();
    }

    if(
    $_GET)
                {
                
    $id=$_GET['id'];
                
    $i=$_GET['i'];    
                echo 
    "a".$id."b".$i;
    include 
    'include/header.inc.php';
    include 
    'include/left.inc.php';
         echo
    " <td bgcolor=#ffcc66><center>";            
                echo 
    "
                
                <form method=post action=\"
    $_SERVER[PHP_SELF]\">
                <table>
                <tr>
                <td>
                <caption>Inserimento nuovi prodotti </caption>
                </td>
                </tr>
                </table>
                <hr>

                "
    ;
                for(
    $j=0;$j<$i;$j++)
                {
                echo 
    "
                        <table>
                        <tr>
                        <td>Marca</td>
                        <td><input type=text name=marca></td>
                        </tr>
                        <tr>
                        <td>Modello</td>
                        <td><input type=text name=modello></td>
                        </tr>
                        <tr>
                        <td>Colore</td>
                        <td><input type=text name=tipo></td>
                        </tr>
                        <tr>
                        <td>Origine</td>
                        <td><input type=text name=origine></td>
                        </tr>
                        <tr>
                        <td>Disponibilit?/td>
                        <td><input type=text name=qt></td>
                        </tr>
                        <tr>
                        <td>Prezzo Privato</td>
                        <td><input type=text name=prezzo></td>
                        </tr>
                        <tr>
                        <td>Prezzo Azienda</td>
                        <td><input type=text name=prezzoaz></td>
                        </tr>
                        <tr>
                        <td>Prezzo Rivenditori</td>
                        <td><input type=text name=prezzoriv></td>
                        </tr>
                    
                        </table>
                        <hr>

                    "
    ;
                }
                
                echo 
    "
                <table>
                <tr>
                <td>
                <input type=hidden name=id_categorie value=
    $id>
                <input type=submit name=submit value=Edita>
                </td>
                </tr>
                </table>
                </form>
                </center>
                "
    ;
                echo 
    "</td>";
         include 
    'include/right.inc.php';
            include 
    'include/footer.inc.php';
                
                }
    else if(
    $_POST)
    {
    $marca=addslashes($_POST['marca']);
         
    $modello=addslashes($_POST['modello']);
         
    $tipo=addslashes($_POST['tipo']);
         
    $origine=addslashes($_POST['origine']);
         
    $qt=addslashes($_POST['qt']);
         
    $prezzo=addslashes($_POST['prezzo']);
         
    $prezzoaz=addslashes($_POST['prezzoaz']);
         
    $prezzoriv=addslashes($_POST['prezzoriv']);
         
    $id_categorie=addslashes($_POST['id_categorie']);
         include 
    'include/conf.inc.php';
         
         function 
    prepareSqlText(&$gpc) {
        
    get_magic_quotes_gpc() && $gpc stripslashes($gpc);
        
    $gpc mysql_escape_string($gpc);
    }

    function 
    prepareSqlVar(&$var) {
        if (
    is_array($var)) {
            
    array_walk($var'prepareSqlVar');
        } else {
            
    prepareSqlText($var);
        }
    }

    // Prepara TUTTI i post per MySQL
    prepareSqlVar($_POST);

    // Questi i campi da inserire nella tabella
    $fields = array('marca''modello''tipo''origine''qt''prezzo''prezzoaz''prezzoriv','id_categorie');

    // Questi i valori
    for ($n 0$n count($_POST['marca']); ++ $n) {
        foreach (
    $fields as $field) {
            
    $row[$field] = $_POST[$field][$n];
        }
         
    $values[$n] = "('" implode("','"$row) . "')"
    }

    // Questa la query
    $query 'INSERT INTO tbl_prodotti (`' implode('`,`'$fields) . '`) VALUES ' implode(','$values);

    // Questo per vedere come è fatta la query insert con più righe
    echo "<pre>$query</pre>";

    // Esecuzione della query
    mysql_query($query) || die(mysql_error());
         
    }

         
    ?>
    adesso mi fa vedere la query

    INSERT INTO tbl_prodotti (`marca`,`modello`,`tipo`,`origine`,`qt`,`prezzo`, `prezzoaz`,`prezzoriv`,`id_categorie`) VALUES ('e','3','3','r','3','2','2','2','1')

    ma se vado a vedere nel db non me li inserisce
    grazie per la disp.

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.