Advertisement
Guest User

STA CAZZ DI MODIFICA..

a guest
Mar 14th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.02 KB | None | 0 0
  1. <?php
  2.     $username = "";
  3.     $pwd = "";
  4.     $nometabella = "";
  5.     $nomecampi = array();
  6.     $tipocampi = array();
  7.     if(isset($_POST["username"]) && isset($_POST["pwd"])) {
  8.         $username = $_POST["username"];
  9.         $pwd = $_POST["pwd"];
  10.         $conn = mysqli_connect("localhost", $username, $pwd, "tarasco");
  11.         if(!$conn)
  12.           die("Non sono riuscito a connettermi: " . mysqli_connect_error());
  13.     $tabelle = mysqli_query($conn, "SHOW TABLES");
  14.       $nometabella = $_POST["nometabella"];
  15.         $nomecampi = $_POST["nomecampi"];
  16.         $tipocampi = $_POST["tipocampi"];
  17.     $n = count($nomecampi);
  18.         if($n != 0) {
  19.       mysqli_query($conn, "DROP TABLE $nometabella");
  20.             $sql = "CREATE TABLE $nometabella (id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,";
  21.             // rimuovo i campi vuoti e aggiorno $n....
  22.             for($i = 0; $i < $n; $i++) {
  23.                 if($nomecampi[$i] != "") {
  24.                     $nom[] = $nomecampi[$i];
  25.                     $tip[] = $tipocampi[$i];
  26.                 }
  27.             }
  28.             $n = count($nom);
  29.             for($i = 0; $i < $n; $i++) {
  30.         if($nom[$i] != "") {
  31.           $sql .= "$nom[$i] $tip[$i] NOT NULL";
  32.                 if($i != $n - 1) {
  33.                     $sql .= ",\n";
  34.                 }
  35.         }
  36.             }
  37.             $sql .= ")";
  38.  
  39.             if(mysqli_query($conn, $sql)) {
  40.                 echo "Tabella $nometabella modificata correttamente.<br>";
  41.                 echo "<table border='1'>";
  42.                 echo "<th>Nome campo</th><th>Tipo campo</th>";
  43.                 for($i = 0; $i < $n; $i++) {
  44.                     if($nom[$i] != "") {
  45.                         echo "<tr><td>$nom[$i]</td><td>$tip[$i]</td></tr>";
  46.                     }
  47.                 }
  48.                 echo "</table><hr>";
  49.             } else if(isset($_POST["nomecampi"])) {
  50.                 echo $sql;
  51.                 echo "Errore nel creare la tabella: " . mysqli_error($conn);
  52.             }
  53.         }
  54.     }
  55. ?>
  56. <html>
  57. <head>
  58.   <link href="https://fonts.googleapis.com/css?family=Space+Mono" rel="stylesheet">
  59.   <style>* {font-family: 'Space Mono', monospace; font-size: 22px; }</style>
  60. </head>
  61. <body>
  62.   <fieldset>
  63.     <legend>Modifica Tabella SQL</legend>
  64.     <form method="post">
  65.             <table border="0" id="tabella">
  66.             <tr><td>username:</td><td><input type="text" name="username" value="<?php echo $_POST['username']; ?>"></td></tr>
  67.             <tr><td>password:</td><td><input type="text" name="pwd" value="<?php echo $_POST['pwd']; ?>"></td></tr>
  68.                 <?php if($tabelle): ?>
  69.             <tr><td>Nome tabella:</td><td><select name="nometabella" value="<?php echo $nometabella; ?>">
  70.             <option value="">Seleziona la tabella da modificare</option>
  71.             <?php
  72.               // Stampo le tabelle presenti nel database
  73.               while($tabella = mysqli_fetch_array($tabelle)) {
  74.                 if($tabella[0] == $nometabella)
  75.                   echo "<option selected value='$tabella[0]'>$tabella[0]</option>";
  76.                 else
  77.                   echo "<option value='$tabella[0]'>$tabella[0]</option>";
  78.               }
  79.             ?>
  80.           </select></td></tr>
  81.                 <?php endif;
  82.           if($nometabella != "") {
  83.                     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>";
  84.             // Stampo i campi presenti nella tabella selezionata
  85.             $tabella = mysqli_query($conn, "DESCRIBE $nometabella");
  86.                     while($row = mysqli_fetch_array($tabella)) {
  87.                         if($row['Field'] != "id") {
  88.                             echo "<tr><td>Nome campo:</td><td><input type='text' value='" . $row['Field'] . "' name='nomecampi[]' placeholder='nome'></td></tr>";
  89.                         echo "<tr><td>Tipo campo:</td><td><input type='text' value='" . $row['Type'] . "' name='tipocampi[]' placeholder='VARCHAR(30)'></td></tr>";
  90.                         }
  91.                     }
  92.           }
  93.             ?>
  94.             </table>
  95.         <input type="submit">
  96.     </form>
  97.     <a href="<?php echo dirname($_SERVER['SCRIPT_NAME']); ?>">Torna al portale MySql</a>
  98.   </fieldset>
  99.     <script src="jquery-3.1.1.js"></script>
  100.     <script>
  101.     function aggiungiCampo() {
  102.         $('#tabella tr:last').after("<tr><td>Nome campo:</td><td><input type='text' name='nomecampi[]' placeholder='nome'></td></tr>");
  103.         $('#tabella tr:last').after("<tr><td>Tipo campo:</td><td><input type='text' name='tipocampi[]' placeholder='VARCHAR(30)'></td></tr>");
  104.     }
  105.     </script>
  106. </body>
  107. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement