Advertisement
rAthus

Insérer simplement des données dans une table

Jun 16th, 2014
528
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.82 KB | None | 0 0
  1. <?php
  2.     function inserer_donnes($donnees,$table)
  3.     {
  4.         $champs_existants = array();
  5.         $requete_colonnes = 'SHOW COLUMNS FROM '.$table;
  6.         $resultat_colonnes = mysql_query($requete_colonnes) or die(mysql_error());
  7.         while ($colonne=mysql_fetch_array($resultat_colonnes))
  8.             $champs_existants[] = $colonne[0];
  9.         $champs = array();
  10.         $valeurs = array();
  11.         foreach($donnees as $cle => $valeur)
  12.         {
  13.             if (!empty($valeur) and in_array($cle,$champs_existants))
  14.             {
  15.                 $champs[] = $cle;
  16.                 $valeurs[] = '"'.htmlspecialchars(is_array($valeur)?'|'.implode('|',$valeur).'|':$valeur,ENT_QUOTES).'"';
  17.             }
  18.         }
  19.         if (count($champs)>0)
  20.         {
  21.             $requete = 'INSERT INTO `'.$table.'` (`'.implode('`,`',$champs).'`) VALUES ('.utf8_decode(implode(',',$valeurs)).')';
  22.             mysql_query($requete) or die(mysql_error());
  23.         }
  24.         return true;
  25.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement