Problema array_key_exists!

Negli innumerevoli test (ovviamente ormai quasi solo a caso...) ho notato che c'è un problema di parentesi:
Ecco il codice funzionante (per quantità=1):
PHP:
$sqlcommande = "INSERT INTO tstat (idstat, datecommande, champtype, description, prix) VALUES( ";			 
			  foreach($cles as $i)
			  
			  	if (array_key_exists($i, $_SESSION["commande"]["type"]) && $_SESSION["commande"]["type"] != NULL)
			  	{
			  		for ($j = 0; $j < $_SESSION["commande"]["quantite"][$i]; $j++)
			  		{		  			
				  		$desc = str_replace("<br />", " - ", $_SESSION["commande"]["prod"][$i]);
				  		$desc = strip_tags($desc);			  		
				  		$sqlcommande .= "'', '".date("Y-m-d")."', '".$_SESSION["commande"]["type"][$i]."', '".addslashes($desc)."',".$_SESSION["commande"]["prix"][$i]."'";             		  
				  	} 
			  	 }
			  }
			  $sqlcommande = substr($sqlcommande, 0, strlen($sqlcommande)-1);
        $sqlcommande .= ");";  			  		  
			  mysql_query($sqlcommande) or die ("Erreur SQL > ".$sqlcommande."<br />".mysql_error()); 	
 
			  unset($_SESSION["commande"]);
Se tolgo la parentesi nella stringa: $sqlcommande .= ");";
non funziona più...
può essere d'aiuto?
 
il problema è che ripeti due volte i VALUES
puoi postare un var_dump di tutta la SESSION ?
PHP:
var_dump($_SESSION)
eccetto ovviamente i dati sensibili tipo password
 
Eh, grazie per la risposta.
Il problema è che la struttura parte da un'altra pagina.
Puoi vedere il sito : www.aupetitcomestible.net
Nella prima pagina (index.php) si caricano tutte le pagine.
Poi si fa un ordine, si aggiunge al carrello e poi si manda una mail di conferma.
Tutto funziona alla perfezione, tranne il passaggio finale, nel quale insieme alla mail si deve aggiungere alla tabella delle statistiche quello che il cliente ha ordinato.
Se l'ordine è singolo e di una sola quantità tutto è ok, se l'ordine è > 1 o in quantità >1 va tutto a farsi benedire...
 
prova cosi: togli questa riga dal ciclo interno for
PHP:
$sqlcommande .= "'', '" . date("Y-m-d") . "', '" . $_SESSION["commande"]["type"][$i] . "', '" . addslashes($desc) . "'," . $_SESSION["commande"]["prix"][$i] . "'";
e mettila appena furi dal ciclo
PHP:
$sqlcommande = "INSERT INTO tstat (idstat, datecommande, champtype, description, prix) VALUES( ";
foreach ($cles as $i) {    
    if (array_key_exists($i, $_SESSION["commande"]["type"]) && $_SESSION["commande"]["type"] != NULL) {
        for ($j = 0; $j < $_SESSION["commande"]["quantite"][$i]; $j++) {
            $desc = str_replace("<br />", " - ", $_SESSION["commande"]["prod"][$i]);
            $desc = strip_tags($desc);            
        }
        $sqlcommande .= "'', '" . date("Y-m-d") . "', '" . $_SESSION["commande"]["type"][$i] . "', '" . addslashes($desc) . "'," . $_SESSION["commande"]["prix"][$i] . "'";
    }
}
$sqlcommande = substr($sqlcommande, 0, strlen($sqlcommande) - 1);
$sqlcommande .= ");";
 
Cmq ecco l'errore con il var_dump:

int(0) array(1) { [0]=> string(4) "shop" } Erreur SQL > INSERT INTO tstat (idstat, datecommande, champtype, description, prix) VALUES();
Column count doesn't match value count at row 1
 
ecco appunto, sembrava troppo bello per essere vero:
non dà più l'errore però purtroppo inserisce nella tabella una sola quantità del prodotto.
Se metto una quantità 2 o + nella 'tstat' appare solo quantità=1
 
vuoi dire che dovresti avere piu righe?
rimetti anche la prima modifica che ti avevo fatto fare ovvero la prima parte della query dentro il ciclo foreach
PHP:
foreach ($cles as $i) {
            $sqlcommande = "INSERT INTO tstat (idstat, datecommande, champtype, description, prix) VALUES( ";
            if (array_key_exists($i, $_SESSION["commande"]["type"]) && $_SESSION["commande"]["type"] != NULL) {
                for ($j = 0; $j < $_SESSION["commande"]["quantite"][$i]; $j++) {
                    $desc = str_replace("<br />", " - ", $_SESSION["commande"]["prod"][$i]);
                    $desc = strip_tags($desc);
                }
                $sqlcommande .= "'', '" . date("Y-m-d") . "', '" . $_SESSION["commande"]["type"][$i] . "', '" . addslashes($desc) . "'," . $_SESSION["commande"]["prix"][$i] . "'";
            }
        }
        $sqlcommande = substr($sqlcommande, 0, strlen($sqlcommande) - 1);
        $sqlcommande .= ");";

anzi no
non credo di aver capito
 
(grazie)

In pratica succede qualcosa di strano quando il parametro "quantità" è > 1, oppure quando l'id del prodotto è > 1 (cioè più prodotti).

Erreur SQL > INSERT INTO tstat (idstat, datecommande, champtype, description, prix) VALUES( '', '2013-03-07', 'sand', 'Giorgio - Sandwiche - ',10.00''', '2013-03-07', 'sand', 'Giorgio - Sandwiche - ',10.00);
You have an error in your SQL syntax; check (...) near '''', '2013-03-07', 'sand', 'Giorgio - Sandwiche - ',10.00)' at line 1

Oppure con il var_dump:

nt(0) array(1) { [0]=> string(4) "sand" } Erreur SQL > INSERT INTO tstat (idstat, datecommande, champtype, description, prix) VALUES();
Column count doesn't match value count at row 1

Non trova cosa????
 
per il primo erroreSQL sei tornato indietro
rimetti quella riga fuori dal ciclo interno for
non ho capito questa frase
Se metto una quantità 2 o + nella 'tstat' appare solo quantità=1
non vedo una colonna quantità nella 'tstat'
 
ciao
io proverei in questa maniera
PHP:
<?php
//......
foreach ($cles as $i) {
	$sqlcommande = "INSERT INTO tstat (datecommande, champtype, description, prix) VALUES( ";
	if (array_key_exists($i, $_SESSION["commande"]["type"]) && $_SESSION["commande"]["type"] != NULL) {
		for ($j = 0; $j < $_SESSION["commande"]["quantite"][$i]; $j++) {
			$desc = str_replace("<br />", " - ", $_SESSION["commande"]["prod"][$i]);
			$desc = strip_tags($desc);
		}//fine for
		$sqlcommande .= "'" . date("Y-m-d") . "', '" . $_SESSION["commande"]["type"][$i] . "', '" . addslashes($desc) . "'," . $_SESSION["commande"]["prix"][$i];
	}//fine if
	//$sqlcommande = substr($sqlcommande, 0, strlen($sqlcommande) - 1);//a che serve togliere l'ultimo apice basta non metterlo dopo il prix[$i]
    $sqlcommande .= ");";
	mysql_query($sqlcommande) or die ("Erreur SQL > ".$sqlcommande."<br />".mysql_error());  
}//fine foreach
unset($_SESSION["commande"]);
//.......
?>
mal che vada "funzia" come prima
 
Ciao!
Intanto ne approfitto ancora per ringraziarvi infinitamente per il vostro più che indispensabile aiuto!
Ho provato ad inserire il comando che mi hai dato, ma mi dà il seguente errore:

Parse error: syntax error, unexpected end of file in..

Che faccio?
 
Per rispondere alla domanda di cricic, in effetti non c'è nella tstat un parametro quantità.
Quando il prodotto è > di 1 infatti si dovrebbe inserire una stringa con (idstat, datecommande, champtype, description, prix) 2 volte, o n volte a seconda di quanti ordini sono fatti.
 
provala cosi: ho fatto due piccole modifiche commentate
PHP:
$sqlcommande = "INSERT INTO tstat (idstat, datecommande, champtype, description, prix) VALUES( ";
foreach ($cles as $i) {    
    if (array_key_exists($i, $_SESSION["commande"]["type"]) && $_SESSION["commande"]["type"] != NULL) {
        for ($j = 0; $j < $_SESSION["commande"]["quantite"][$i]; $j++) {
            $desc = str_replace("<br />", " - ", $_SESSION["commande"]["prod"][$i]);
            $desc = strip_tags($desc); 
            $sqlcommande .= "'', '" . date("Y-m-d") . "', '" . $_SESSION["commande"]["type"][$i] . "', '" . addslashes($desc) . "'," . $_SESSION["commande"]["prix"][$i] . "'),";
        // aggiunte parentesi e virgola per chiudere la prima insert
            
        }
        
    }
}
$sqlcommande = substr($sqlcommande, 0, strlen($sqlcommande) - 1);
// $sqlcommande .= ");"; tolta la chiusura
 
scusa tanto, ho visto che avevo dimenticato una } prima della fine della pagina.
Quindi ho sistemato e non dà più errore, ma la quantità nella tstat resta sempre = 1...

Con il var_dump esce fuori:

int(0) array(1) { [0]=> string(4) "shop" } Erreur SQL > INSERT INTO tstat (datecommande, champtype, description, prix) VALUES( );
Column count doesn't match value count at row 1

...
 
Ciao Cricic, grazie!
Purtroppo con l'ultima modifica non dà errore, ma non inserisce proprio nulla nella tabella tstat...
 
cosi dovrebbe funzionarti
stai attento alla sintassi che ho solo spostato una parentesi ( e aggiunto un apice '
PHP:
$sqlcommande = "INSERT INTO tstat (idstat, datecommande, champtype, description, prix) VALUES ";
foreach ($cles as $i) {    
    if (array_key_exists($i, $_SESSION["commande"]["type"]) && $_SESSION["commande"]["type"] != NULL) {
        for ($j = 0; $j < $_SESSION["commande"]["quantite"][$i]; $j++) {
            $desc = str_replace("<br />", " - ", $_SESSION["commande"]["prod"][$i]);
            $desc = strip_tags($desc); 
            $sqlcommande .= "('', '" . date("Y-m-d") . "', '" . $_SESSION["commande"]["type"][$i] . "', '" . addslashes($desc) . "','" . $_SESSION["commande"]["prix"][$i] . "'),";
        // aggiunte parentesi e virgola per chiudere la prima insert
            
        }
        
    }
}
$sqlcommande = substr($sqlcommande, 0, strlen($sqlcommande) - 1);
// $sqlcommande .= ");"; tolta la chiusura

ps
se non va metti alla fine
PHP:
echo $sqlcommande
e posta
 
Ho dovuto aggiungere echo $sqlcommande; (con il ;), ma mi dice

INSERT INTO tstat (idstat, datecommande, champtype, description, prix) VALUES ('', '2013-03-07', 'sand', 'DAN - Sandwiche - ','13.00')

Cioè, non dà errore, ma non inserisce nella tabella...
 
Questo messaggio appare invece se la quantità di "DAN" è = 2
(nella tstat sempre nulla...)

INSERT INTO tstat (idstat, datecommande, champtype, description, prix) VALUES ('', '2013-03-07', 'sand', 'DAN - Sandwiche - ','13.00'),('', '2013-03-07', 'sand', 'DAN - Sandwiche - ','13.00')

Perché non dice più ERROR?
 
hai messo piu quantita?
mettine 2 o 3 e riprova
 

Discussioni simili