Créer une table mySQL automatiquement via un form
Bonjour,
comment générer automatiquement une table mySQL via le formulaire ci-dessous :
Code:
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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
| <form name="mySQL_table" id="mySQL_table">
<fieldset>
<legend>Create MySQL table</legend>
<div style="display:block; height:100px;">
<label>Nom de la table</label> <input type="text" name="name_table" id="name_table" value="" class="width300" required /> Ajouter <input type="number" name="numColumn" id="numColumn" value="1" class="width80" /> colonne(s) <button type="button" id="btn_addColumn">Exécuter</button>
</div>
<div style="display:block">
<table id="table_create" width="95%">
<tr>
<th width="20%">Nom</th>
<th width="20%">Type</th>
<th width="10%">Taille/valeurs</th>
<th width="10%">Defaut</th>
<th width="10%">Interclassement</th>
<th width="10%">Attributs</th>
<th width="5%">Null</th>
<th width="5%px">Index</th>
<th width="5%">A_I</th>
<th width="5%"></th>
</tr>
<tr>
<td>
<input type="text" name="name_column[]" class="name_column" value="" class="width400" required />
</td>
<td>
<select name="type[]" class="type width200">
<option value="INT">INT</option>
<option value="TINYINT">TINYINT</option>
<option value="VARCHAR">VARCHAR</option>
<option value="CHAR">CHAR</option>
<option value="TEXT">TEXT</option>
<option value="DATE">DATE</option>
<option value="DECIMAL">DECIMAL</option>
<option value="FLOAT">FLOAT</option>
</select>
</td>
<td>
<input type="number" name="size[]" value="" class="size width80" required / >
</td>
<td>
<select name="defaut[]" class="defaut width150">
<option value="Aucune">Aucune</option>
</select>
</td>
<td>
<select name="interclassement[]" class="interclassement width200">
<option value="utf8_general_ci">utf8_general_ci</option>
</select>
</td>
<td>
<select name="attributs[]" class="attributs width200">
<option value="BINARY">BINARY</option>
<option value="UNSIGNED" selected>UNSIGNED</option>
<option value="UNSIGNED_ZEROFIL">UNSIGNED ZEROFIL</option>
<option value="CURRENT_TIMESTAMP">on update CURRENT_TIMESTAMP</option>
</select>
</td>
<td>
<input type="checkbox" name="null[]" class="null" value="NOT NULL" />
</td>
<td>
<select name="index[]" class="index width150">
<option value="0">...</option>
<option value="PRIMARY KEY">PRIMAIRE</option>
<option value="UNIQUE">UNIQUE</option>
<option value="INDEX">INDEX</option>
<option value="FULLTEXT">FULLTEXT</option>
</select>
</td>
<td>
<input type="checkbox" name="A_I[]" class="A_I" value="AUTO_INCREMENT" />
</td>
<td></td>
</tr>
</table>
</form> |
Dans la page process.php, je récupère les variables postées :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| if(!empty( $_POST["name_column"] )) {
foreach($_POST['name_column'] as $name_column) {
//echo "<p>name_column : ".$name_column."</p>";
}
//---------------------
//format
//---------------------
$name_column = implode(",", $_POST["name_column"]);
echo "name_column => : ".$name_column."</br>";
}
else{
$name_column = "";
} |
mais comment compiler les colonnes afin d'obtenir :
Code:
1 2 3
| $sql = "CREATE TABLE IF NOT EXISTS ".$table_name."(
".$_POST['name_column']." "..$_POST['type']."(".$_POST['size.") ...,
PRIMARY KEY (ID) )";" |
Merci d'avance; :D