1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| if (isset($_POST['nv_jeux']['nom'],$_POST['nv_jeux']['console']))
{
// supprime les espace vides en début et fin de chaine
$nv_jeux = array_map('trim',$_POST['nv_jeux']['nom']);
$nv_console = array_map('trim',$_POST['nv_jeux']['console']);
// supprime les entrées vides des tableaux
$nv_jeux = array_filter($nv_jeux);
$nv_console = array_filter($nv_console);
// applique les protection sur les variables pour insertion en bdd
$nv_jeux = array_map('mysql_real_escape_string',$nv_jeux);
$nv_console = array_map('mysql_real_escape_string',$nv_console);
$tab_values = array();
// si tu veux que les deux champs soient remplis on met une condition et on construit les values de la requête
foreach ($nv_jeux as $key => $value)
{
if (isset($nv_console[$key])) $tab_values[] = "('".$value."','".$nv_console[$key]."')";
}
// construit la syntaxe pour insertions multiples
$tab_values = implode(',',$tab_values);
if ($tab_values != '')
{
$sql = "INSERT INTO jeux (console, nom) VALUES ".$tab_values."";
// juste pour vérifier si la requête est bien construite
echo $sql;
// suite du code, exécution de la requête etc.
}
else
{
echo 'aucune donnée rentrée ou données incomplètes';
}
}
?>
<form action="#" method="POST">
<label>Nouveau(x) jeu(x)</label><br /><br />
<input type="text" name="nv_jeux[nom][]" /> sur <input type="text" name="nv_jeux[console][]" /><br />
<input type="text" name="nv_jeux[nom][]" /> sur <input type="text" name="nv_jeux[console][]" /><br />
<input type="text" name="nv_jeux[nom][]" /> sur <input type="text" name="nv_jeux[console][]" /><br />
<input type="text" name="nv_jeux[nom][]" /> sur <input type="text" name="nv_jeux[console][]" /><br />
<input type="text" name="nv_jeux[nom][]" /> sur <input type="text" name="nv_jeux[console][]" /><br />
<input type="text" name="nv_jeux[nom][]" /> sur <input type="text" name="nv_jeux[console][]" /><br />
<input type="text" name="nv_jeux[nom][]" /> sur <input type="text" name="nv_jeux[console][]" /><br />
<input type="submit" value="insérer">
</form> |
Partager