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
| <body>
<?php
$rows = $bdd->query("describe adherents");
?>
<fieldset><legend>Insérer une colonne dans la BDD après la colonne choisie</legend>
<form name="ajout_champ_bdd" method="post" action="#">
<select name="test[]" style="width:130px" >
<option>Selectionner ...</option>
<?php
while ($row = $rows->fetch()){
print $row['Field'] . '<br>';
echo '<option value="$row[]"</option>';
}
?>
</select>
Saisir nom de la nouvelle colonne ><input type="text" name="nom_new_col_bdd" style="width:130px; margin-left:5px" />
<input type="submit" name="ajout_col_bdd" value="Ajout colonne à la bdd" style="width:130px; margin-left:0px" /><br>
</form>
<div style="margin-left:25px">
<?php
if(isset($_POST['test'])) {
foreach( $_POST['test'] as $ii => $test )
{
$varselect = $test;
echo 'Le nom de colonne '.$varselect.' a été sélectionné <br>';
}
}else{
echo "Aucune option n'a été sélectionnée <br>";
}
?>
</div>
<?php
// récupération des variables POST:
$varnewcol=isset($_POST['nom_new_col_bdd'])?$_POST['nom_new_col_bdd']:'';
if (isset($_POST['ajout_col_bdd'])) {
$sql= "SHOW COLUMNS FROM adherents LIKE '$varnewcol'";
$result = $bdd->query($sql);
if($result->rowCount() > 0) {
echo '<script>alert("La colonne \"'.$varnewcol.'\" existe, saisir un autre nom !");</script>';
} else {
$req=$bdd->query('ALTER TABLE adherents ADD '.$varnewcol.' TEXT NOT NULL AFTER '.$varselect);//fonctionne pas
echo '<script>alert("La colonne \"'.$varnewcol.'\" a été ajouté à la bdd");</script>';
}
}
?>
</fieldset>
</body> |
Partager