Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $username = "";
- $pwd = "";
- $nometabella = "";
- $nomecampi = array();
- $tipocampi = array();
- if(isset($_POST["username"]) && isset($_POST["pwd"])) {
- $username = $_POST["username"];
- $pwd = $_POST["pwd"];
- $conn = mysqli_connect("localhost", $username, $pwd, "tarasco");
- if(!$conn)
- die("Non sono riuscito a connettermi: " . mysqli_connect_error());
- $tabelle = mysqli_query($conn, "SHOW TABLES");
- $nometabella = $_POST["nometabella"];
- $nomecampi = $_POST["nomecampi"];
- $tipocampi = $_POST["tipocampi"];
- $n = count($nomecampi);
- if($n != 0) {
- mysqli_query($conn, "DROP TABLE $nometabella");
- $sql = "CREATE TABLE $nometabella (id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,";
- // rimuovo i campi vuoti e aggiorno $n....
- for($i = 0; $i < $n; $i++) {
- if($nomecampi[$i] != "") {
- $nom[] = $nomecampi[$i];
- $tip[] = $tipocampi[$i];
- }
- }
- $n = count($nom);
- for($i = 0; $i < $n; $i++) {
- if($nom[$i] != "") {
- $sql .= "$nom[$i] $tip[$i] NOT NULL";
- if($i != $n - 1) {
- $sql .= ",\n";
- }
- }
- }
- $sql .= ")";
- if(mysqli_query($conn, $sql)) {
- echo "Tabella $nometabella modificata correttamente.<br>";
- echo "<table border='1'>";
- echo "<th>Nome campo</th><th>Tipo campo</th>";
- for($i = 0; $i < $n; $i++) {
- if($nom[$i] != "") {
- echo "<tr><td>$nom[$i]</td><td>$tip[$i]</td></tr>";
- }
- }
- echo "</table><hr>";
- } else if(isset($_POST["nomecampi"])) {
- echo $sql;
- echo "Errore nel creare la tabella: " . mysqli_error($conn);
- }
- }
- }
- ?>
- <html>
- <head>
- <link href="https://fonts.googleapis.com/css?family=Space+Mono" rel="stylesheet">
- <style>* {font-family: 'Space Mono', monospace; font-size: 22px; }</style>
- </head>
- <body>
- <fieldset>
- <legend>Modifica Tabella SQL</legend>
- <form method="post">
- <table border="0" id="tabella">
- <tr><td>username:</td><td><input type="text" name="username" value="<?php echo $_POST['username']; ?>"></td></tr>
- <tr><td>password:</td><td><input type="text" name="pwd" value="<?php echo $_POST['pwd']; ?>"></td></tr>
- <?php if($tabelle): ?>
- <tr><td>Nome tabella:</td><td><select name="nometabella" value="<?php echo $nometabella; ?>">
- <option value="">Seleziona la tabella da modificare</option>
- <?php
- // Stampo le tabelle presenti nel database
- while($tabella = mysqli_fetch_array($tabelle)) {
- if($tabella[0] == $nometabella)
- echo "<option selected value='$tabella[0]'>$tabella[0]</option>";
- else
- echo "<option value='$tabella[0]'>$tabella[0]</option>";
- }
- ?>
- </select></td></tr>
- <?php endif;
- if($nometabella != "") {
- echo "<tr><td><button type='button' onclick='aggiungiCampo()'>Aggiungi Campo</button></td><td><p style='font-size:11px'>(Per rimuovere un campo cancellare il suo nome)</p></td></tr>";
- // Stampo i campi presenti nella tabella selezionata
- $tabella = mysqli_query($conn, "DESCRIBE $nometabella");
- while($row = mysqli_fetch_array($tabella)) {
- if($row['Field'] != "id") {
- echo "<tr><td>Nome campo:</td><td><input type='text' value='" . $row['Field'] . "' name='nomecampi[]' placeholder='nome'></td></tr>";
- echo "<tr><td>Tipo campo:</td><td><input type='text' value='" . $row['Type'] . "' name='tipocampi[]' placeholder='VARCHAR(30)'></td></tr>";
- }
- }
- }
- ?>
- </table>
- <input type="submit">
- </form>
- <a href="<?php echo dirname($_SERVER['SCRIPT_NAME']); ?>">Torna al portale MySql</a>
- </fieldset>
- <script src="jquery-3.1.1.js"></script>
- <script>
- function aggiungiCampo() {
- $('#tabella tr:last').after("<tr><td>Nome campo:</td><td><input type='text' name='nomecampi[]' placeholder='nome'></td></tr>");
- $('#tabella tr:last').after("<tr><td>Tipo campo:</td><td><input type='text' name='tipocampi[]' placeholder='VARCHAR(30)'></td></tr>");
- }
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement