problema con "array_key_exist" (era: aiuto!!!)

Fabrizio Villa

Utente Attivo
19 Gen 2013
131
0
16
Salve!
Sono nuovo nel forum e quasi completamente inesperto di php.
Avrei bisogno che qualche anima pia mi aiutasse a risolvere un problema con "array_key_exist"...
ecco il codice che mi dà errore:

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)."',".number_format((float)$_SESSION["commande"]["prix"][$i], 2, '.', ' ').")"; 		  
				  	}
			  	 }
			  }

Grazie infinite!
 
Ultima modifica di un moderatore:

Nefyt

Utente Attivo
17 Apr 2012
1.102
0
0
Non mettere titoli troppo generali inoltre metti il codice php fra i tag appoisti, comunque che tipo di errori ti da?
 

Fabrizio Villa

Utente Attivo
19 Gen 2013
131
0
16
chiedo scusa,
credo di aver fatto un po' di casino per postare il messaggio.
Cmq se puoi aiutarmi, il messaggio di errore dice:

Notice: Undefined index: type in C:\xampp\htdocs\MALERBA\Panier\panier_valider.php on line 168

Warning: array_key_exists() expects parameter 2 to be array, null given in


Erreur SQL > INSERT INTO tstat (idstat, datecommande, champtype, description, prix) 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
 

Nefyt

Utente Attivo
17 Apr 2012
1.102
0
0
Il primo errore dipende da $_SESSION["commande"]["type"] che non è un array e la query forse è sbagliata per via di quest'ultima parentesi che non ci dovrebbe essere .")"
 

Fabrizio Villa

Utente Attivo
19 Gen 2013
131
0
16
Intanto grazie per la risposta e scusa ancora per il titolo.
Ho provato a togliere la parentesi ma non funziona...

Il problema credo sia più complesso di quel che sembra. Tra tutte le pagine del sito infatti c'è qualche "Undefined index" che non riesco a trovare.
Per esempio nella pagina che permette di aggiungere dati alle tabelle (tramite INSERT INTO) mi da errore.

Ecco il codice:

PHP:
if (isset($_POST['Btajout']) && $_POST['Btajout'] == 'Ajouter')
	{
    $table = $_POST['tb'];
		$nom = $_POST['nom'];
		
		//Pour table alcool
		if ($table != 'talcools' || $table != 'tdesert' || $table != 'tboissons')
			$champ = 'nom_'.substr($table, 1);
			
		if ($table == 'tdesert')
		{
			$champ = 'description';
			$champ2 = 'prix';
			$prix = $_POST["prix"];
		}
		
		if ($table == 'talcools')
			$champ = 'nom';
		
		
		if ($table == 'tboissons')
		{
			$champ = 'nom_'.substr($table, 1);
			$champ = substr($champ, 0, -1);
			$champ2 = 'prix';
			$prix = $_POST["prix"];			
		}
		
		if (isset($champ2))
		{
			$sql_prod = "INSERT INTO ".$table." (".$champ.", ".$champ2.") VALUES ('".addslashes($nom)."', ".$prix.")";
			mysql_query($sql_prod) or die ("Erreur SQL > ".$sql_prod."<br />".mysql_error()); 		
		}
		else
		{
			$sql_prod = "INSERT INTO ".$table." (".$champ.") VALUES ('".addslashes($nom)."')";
			mysql_query($sql_prod) or die ("Erreur SQL > ".$sql_prod."<br />".mysql_error()); 
		}
				
		echo '<p class="PCentrer">Valeur ajoutée.<br /><a href="index.php?p=prod&pr='.$table.'">Retour</a></p>';
		exit;
	}

Grazie ancora per l'aiuto!
 

Fabrizio Villa

Utente Attivo
19 Gen 2013
131
0
16
In questo caso dice:

Notice: Undefined index: tb in C:\xampp\htdocs\MALERBA\admin\Produits\prod_ajout.php on line 3
Erreur SQL > INSERT INTO (nom_) VALUES ('ca')
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 '(nom_) VALUES ('ca')' at line 1

Grazie!!
 

Nefyt

Utente Attivo
17 Apr 2012
1.102
0
0
In questo caso dice:

Notice: Undefined index: tb in C:\xampp\htdocs\MALERBA\admin\Produits\prod_ajout.php on line 3
Erreur SQL > INSERT INTO (nom_) VALUES ('ca')
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 '(nom_) VALUES ('ca')' at line 1

Grazie!!

Stampa la query anche con un semplice echo e postala, per l'undefined index significa che quella variabile non è settata quindi in questo caso $_POST['tb'] non lo è
 

Fabrizio Villa

Utente Attivo
19 Gen 2013
131
0
16
E come e/o dove si setta sto caspita di <tb>???
Porca miseria 'sto php mi fa impazzire...
Come faccio a "stampare" la query?

Mi rendo conto della mia nullità...scusa!
 

Fabrizio Villa

Utente Attivo
19 Gen 2013
131
0
16
PHP:
<?php
/*
	Page : admin/Produits/prod_ajout.php
				Page pour ajouter un produit
*/
//Si on est identifier
if (isset($_SESSION['admin']))
{
	//Ajout selon la liste de produits choisi (fromages, pains, etc)
	if (isset($_POST['Btajout']) && $_POST['Btajout'] == 'Ajouter')
	{
    $table = $_POST['tb'];
		$nom = $_POST['nom'];
		
		//Pour table alcool
		if ($table != 'talcools' || $table != 'tdesert' || $table != 'tboissons')
			$champ = 'nom_'.substr($table, 1);
			
		if ($table == 'tdesert')
		{
			$champ = 'description';
			$champ2 = 'prix';
			$prix = $_POST["prix"];
		}
		
		if ($table == 'talcools')
			$champ = 'nom';
		
		
		if ($table == 'tboissons')
		{
			$champ = 'nom_'.substr($table, 1);
			$champ = substr($champ, 0, -1);
			$champ2 = 'prix';
			$prix = $_POST["prix"];			
		}
		
		if (isset($champ2))
		{
			$sql_prod = "INSERT INTO ".$table." (".$champ.", ".$champ2.") VALUES ('".addslashes($nom)."', ".$prix.")";
			mysql_query($sql_prod) or die ("Erreur SQL > ".$sql_prod."<br />".mysql_error()); 		
		}
		else
		{
			$sql_prod = "INSERT INTO ".$table." (".$champ.") VALUES ('".addslashes($nom)."')";
			mysql_query($sql_prod) or die ("Erreur SQL > ".$sql_prod."<br />".mysql_error()); 
		}
				
		echo '<p class="PCentrer">Valeur ajoutée.<br /><a href="index.php?p=prod&pr='.$table.'">Retour</a></p>';
		exit;
	}

	//Affichage du formulaire pour ajouter
	if (isset($_GET['t']) && $_GET['t'] != '')
	{
		$nomprod = substr($_GET['t'], 1);
		echo '<p class="PCentrer"><span class="Titre">Produits</span></p>
			  <form name="FProdajout" method="post" action"index.php?p=prodajout">
			  	Nom '.$nomprod.' : <input type="text" name="nom" />';
		if ($nomprod == 'desert' || $nomprod == 'boissons')	
			echo ' Prix : <input type="text" name="prix" size="2"/> ';
		echo  '<input type="submit" name="Btajout" value="Ajouter" />
				<input type="hidden" name="tb" value="'.$_GET['t'].'"
			  </form>';
	}
}
?>
 
Ultima modifica di un moderatore:

Fabrizio Villa

Utente Attivo
19 Gen 2013
131
0
16
PHP:
<?php
/*
	Page : admin/Produits/prod.php
				Page qui affiche les différents produits
*/
//Si on est identifier
if (isset($_SESSION['admin']))
{
	echo '<p class="PCentrer"><span class="Titre">Produits</span></p>
	     <p class="PCentrer"><a href="index.php?p=prod&pr=tpain">Pain</a> | 
       <a href="index.php?p=prod&pr=tcharcuterie">Charcuteries</a> | 
       <a href="index.php?p=prod&pr=tfromage">Fromages</a> | 
       <a href="index.php?p=prod&pr=tgarniture">Garnitures</a> | 
       <a href="index.php?p=prod&pr=tdivers">Divers</a> | 
       <a href="index.php?p=prod&pr=tboissons">Boissons</a> | 
       <a href="index.php?p=prod&pr=tdesert">Dessert</a> | 
       <a href="index.php?p=prod&pr=talcools">Alcools et vins</a></p>';

	//Modifications des champs
  
	if (isset($_POST['Btmodif']) && $_POST['Btmodif'] == 'Modifier')
	{
  
		$table = $_POST['tb'];
		$pass = false;
		
		//Pour table alcool
		if ($table == 'talcools')
		{
			$champ = 'nom';	
			$nom = $_POST['nom'];
			$pass = true;
		}
		
		if ($table == 'tdesert')
		{
			echo 'sad';
			$champ = 'description';	
			$champs2 = 'prix';
			$nom = $_POST['description'];		
			$prix = $_POST['tprix'];
			$pass = true;		
		}
		
		if ($table == 'tboissons')	
		{
			$champ = 'nom_'.substr($table, 1);
			$champ = substr($champ, 0, -1);
			$champs2 = 'prix';
			$prix = $_POST['tprix'];
			$nom = $_POST[$champ];
			$pass = true;
		}
		
		if ($pass == false)	
		{
			$champ = 'nom_'.substr($table, 1);
			$nom = $_POST['nom_'.substr($table, 1)];
		}
				
		$id = $_POST['id'];	
				
		$idxid = 0;
		if (isset($champs2))
		{		
			for($i = 0; $i < count($nom); $i++)
			{
			
				$sql = "UPDATE ".$table." SET ".$champ." = '".addslashes($nom[$i])."', ".$champs2." = ".$prix[$i]." WHERE id = ".$id[$i]."";
				mysql_query($sql) or die ("Erreur SQL > ".$sql."<br />".mysql_error()); 								
				$idxid++;
			}			
		}
		else
		{
			foreach($nom as $nmodif)
			{
				$sql = "UPDATE ".$table." SET ".$champ." = '".addslashes($nmodif)."' WHERE id = ".$id[$idxid]."";
				mysql_query($sql) or die ("Erreur SQL > ".$sql."<br />".mysql_error()); 
				$idxid++;
			}		
		}
		echo '<p class="PCentrer">Valeur(s) modifiée(s).<br /><a href="index.php?p=prod&pr='.$table.'">Retour</a></p>';
	}

	//Affichage selon la liste de produits choisi (fromages, pains, etc)
	if (isset($_GET['pr']) && $_GET['pr'] != '')
	{
		$pageprod = $_GET['pr'];
		
		$sql_prod = "SELECT * FROM ".$pageprod;
		$req_prod = mysql_query($sql_prod) or die ("Erreur SQL > ".$sql_prod."<br />".mysql_error());
		echo '&nbsp;&nbsp;- <a href="index.php?p=prodajout&t='.$pageprod.'">Ajouter un produit</a>
			  <form name="Fprod" method="post" action="index.php?p=prod">';
		while($ligne = mysql_fetch_assoc($req_prod))
		{
			switch($pageprod)
			{
				case 'tpain' : {$nom = 'nom_pain'; break;}
				case 'tcharcuterie' : {$nom = 'nom_charcuterie'; break;}
				case 'tfromage' : {$nom = 'nom_fromage'; break;}
				case 'tgarniture' : {$nom = 'nom_garniture'; break;}
				case 'tdivers' : {$nom = 'nom_divers'; break;}
				case 'tboissons' : {$nom = 'nom_boisson'; $des = true; break;}	
				case 'talcools' : {$nom = 'nom'; $size = true; break;}	
				case 'tdesert' : {$nom = 'description'; $des = true; break;}	
			}
			
			echo '<a href="index.php?p=prodsup&t='.$pageprod.'&id='.$ligne['id'].'" onclick="javascript: return confirm(\'Voulez-vous vraiment effacer : '.$ligne[$nom].' ?\')">
				  <img src="Images/sup.png" id="Supimg"/></a> - ';
			if (isset($size))
				echo '<textarea name="'.$nom.'[]">'.$ligne[$nom].'</textarea>';  
			elseif (isset($des))
				echo '<input type="text" value="'.$ligne[$nom].'" name="'.$nom.'[]" /> Prix : <input type="text" name="tprix[]" value="'.$ligne['prix'].'" size="2">';
			else
				echo '<input type="text" value="'.$ligne[$nom].'" name="'.$nom.'[]" />';	  
				 
			echo '  <input type="hidden" name="id[]" value="'.$ligne['id'].'" /><br />';	  
		}
		echo '<br /><input type="submit" name="Btmodif" value="Modifier" /><input type="hidden" name="tb" value="'.$pageprod.'" />
			  </form>';
	}
}
?>
 
Ultima modifica di un moderatore:

Nefyt

Utente Attivo
17 Apr 2012
1.102
0
0
E come e/o dove si setta sto caspita di <tb>???
Porca miseria 'sto php mi fa impazzire...
Come faccio a "stampare" la query?

Mi rendo conto della mia nullità...scusa!

Quella variabile è presa da quelle inviate da un form, quindi basta aggiungere un controllo che sia settata o meno con isset() ad esempio

Per stampare la query fai un echo + la variabile della query
PHP:
echo $sql_prod;
 

borgo italia

Super Moderatore
Membro dello Staff
SUPER MOD
MOD
4 Feb 2008
16.046
150
63
PR
www.borgo-italia.it
ciao
evita di mettere tutta quella lunga file di * soprattutto se non scrivi nulla prima del codice, nella pag "cosa c'è di nuovo" si vede solo una sfolza di * e non si capisce a cosa si riferisce il post.
se scrivi solo codice aggiungi almeno il titolo
 

Fabrizio Villa

Utente Attivo
19 Gen 2013
131
0
16
Così per esempio...
PHP:
if (isset($_POST['Btajout']) && $_POST['Btajout'] == 'Ajouter')
	{
		$table = $_POST['tb'];
		$nom = $_POST['nom'];
		$champ = 'nom';
		 echo 
		$sql_prod = "INSERT INTO ".$table." (".$champ.") VALUES ('".addslashes($nom)."')";
		mysql_query($sql_prod) or die ("Erreur SQL > ".$sql_prod."<br />".mysql_error()); 
		echo '<p class="PCentrer">Valeur ajoutée.<br /><a href="index.php?p=prodsalades&pr='.$table.'">Retour</a></p>';
		exit;
	}
Ma non funziona lo stesso...
il valore <tb>, se ho capito bene, dovrebbe essere preso quindi dal form.
Ma perché non lo assegna????
Sempre e comunque grazie!!!
 

Nefyt

Utente Attivo
17 Apr 2012
1.102
0
0
Prova cosi

PHP:
if (isset($_POST['Btajout'],$_POST['tb'],$_POST['nom']) && $_POST['Btajout'] == 'Ajouter') 
    { 
        $table = $_POST['tb']; 
        $nom = $_POST['nom']; 
        $champ = 'nom';   
        $sql_prod = "INSERT INTO ".$table." (".$champ.") VALUES ('".mysql_real_escape_string($nom)."')"; 
        mysql_query($sql_prod) or die ("Erreur SQL > ".$sql_prod."<br />".mysql_error());  
        echo '<p class="PCentrer">Valeur ajoutée.<br /><a href="index.php?p=prodsalades&pr='.$table.'">Retour</a></p>'; 
        exit; 
    }
 

Fabrizio Villa

Utente Attivo
19 Gen 2013
131
0
16
Oh mio dio...e da dove arrivano allora 'ste variabili???
...mi spiace rompere le p...ma non so proprio come fare, grazie ancora per la splendida collaborazione!
 

Fabrizio Villa

Utente Attivo
19 Gen 2013
131
0
16
Qui c'è il form per aggiungere i valori:
PHP:
if (isset($_GET['t']) && $_GET['t'] != '')
	{
		$nomprod = substr($_GET['t'], 1);
		if (substr($nomprod, 0, -7) == 'char')
			$nomprod = "charcuterie (salades)";
		if (substr($nomprod, 0, -7) == 'from')
			$nomprod = "fromage (salades)";	
		if (substr($nomprod, 0, -7) == 'garni')
			$nomprod = "garniture (salades)";	
		if (substr($nomprod, 0, -7) == 'divers')
			$nomprod = "divers (salades)";	
												
		echo '<p class="PCentrer"><span class="Titre">Salades produits</span></p>
			  <form name="FProdajout" method="post" action"index.php?p=prodsajout">
			  	Nom '.$nomprod.' : <input type="text" name="nom" />
				<input type="submit" name="Btajout" value="Ajouter" />
				<input type="hidden" name="tb" value="'.$_GET['t'].'"
			  </form>';
	}

Nell'imput type text scrivo il nome del prodotto che voglio aggiungere, dovrebbe essere qui che il valore tb viene assegnato?
 

Nefyt

Utente Attivo
17 Apr 2012
1.102
0
0
Qui c'è il form per aggiungere i valori:
PHP:
if (isset($_GET['t']) && $_GET['t'] != '')
	{
		$nomprod = substr($_GET['t'], 1);
		if (substr($nomprod, 0, -7) == 'char')
			$nomprod = "charcuterie (salades)";
		if (substr($nomprod, 0, -7) == 'from')
			$nomprod = "fromage (salades)";	
		if (substr($nomprod, 0, -7) == 'garni')
			$nomprod = "garniture (salades)";	
		if (substr($nomprod, 0, -7) == 'divers')
			$nomprod = "divers (salades)";	
												
		echo '<p class="PCentrer"><span class="Titre">Salades produits</span></p>
			  <form name="FProdajout" method="post" action"index.php?p=prodsajout">
			  	Nom '.$nomprod.' : <input type="text" name="nom" />
				<input type="submit" name="Btajout" value="Ajouter" />
				<input type="hidden" name="tb" value="'.$_GET['t'].'"
			  </form>';
	}

Nell'imput type text scrivo il nome del prodotto che voglio aggiungere, dovrebbe essere qui che il valore tb viene assegnato?

Si, se la variabile $_GET è settata e le condizioni sono ok allora ti sei scordato solamente di chiuderlo
Codice:
<input type="hidden" name="tb" value="'.$_GET['t'].'" />
 

Fabrizio Villa

Utente Attivo
19 Gen 2013
131
0
16
Weeeeeeeeeeeeeeeeeeeeeeee!!!!!!!!!!!!!!!!!
Funzionaaaaaaaaaaaaaaaaaaaaaaa!!!!!!!!!

Grazie mille!!!!!!!!!!
Sei un genio!!!!!!!

Grazie davvero!!!!!
 
Discussioni simili
Autore Titolo Forum Risposte Data
T ALTRO PROBLEMA CON ARRAY PHP PHP 1
T PROBLEMA CON ARRAY PHP 8
F [PHP] Problema con array multidimensionale PHP 4
alessandra86 [PHP] Popolamento database con form ricorsivi - problema array (foreach ) PHP 5
R [PHP] Problema stampa array bidimensionali con formula $html.=<<<myHtml... PHP 2
S problema con gli array in javascript Javascript 12
C Problema con creazione array PHP 7
V Problema con array PHP 8
neo996sps PHP/MySQL - Problema con generazione array PHP 14
L problema array con le sessioni PHP 1
F Problema formattazione data e creazione array con nuove date Javascript 2
F problema Json con Array jQuery 1
S Problema con inserire la select in un array PHP 2
E Problema con array multipli Javascript 1
S Problema con 2 funzioni:array,select dinamica Javascript 21
P Problema con array e foreach PHP 15
L Problema con un array e il $$ PHP 2
F Problema urgente con checkbox, array e explode PHP 10
S Problema con fwrite e array PHP 3
F Problema con array multidimensionali Flash 0
N Problema con array multidimensionali PHP 0
minatore problema con gli array PHP 5
O problema con dvr dahua xvr5116 IP Cam e Videosorveglianza 0
G Problema con Xampp Web Server 1
andrea barletta Problema con miniature comandi Photoshop 0
I problema con alice Posta Elettronica 0
N Problema con position absolute e overflow HTML e CSS 4
L Problema con inner join PHP 11
K [php] Problema con inner join PHP 4
K [PHP] Problema con variabili concatenate. PHP 1
O problema con query PHP 4
I problema con 2 account Posta Elettronica 1
L problema collegamento file css con html HTML e CSS 1
E Problema accesso a file con app sviluppata con MIT APP INVENTOR 2 Sviluppo app per Android 0
M Problema con Try Catch PHP 0
Sergio Unia Problema con gli eventi del mouse su una data table: Javascript 2
T PROBLEMA CON SESSIONI PHP 3
R problema con else PHP 0
L problema con query select PHP 2
R Problema query con ricerca id numerico PHP 2
F Problema con risposta PHP 0
S problema con recupero dati tabella mysql PHP 2
Z Problema con il mio tp-l i nk Reti LAN e Wireless 1
L Problema RAM con Tomcat 8 Apache 0
napuleone problema con sort e asort PHP 4
Z Problema con INT MySQL PHP 1
Z Problema database MySQL con XAMPP PHP 0
M Problema con controllo form in real time jQuery 6
Z Problema di sincronizzazione PAYPAL con PHP PHP 1
G Problema con Get page PHP 4

Discussioni simili