function controllaRegistra($usr, $idkey, $tel, $imei) { $con = mysqli_connect("localhost", "root", "", "testing"); if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: "; } if ($idkey == "" || $idkey == "%20" || strlen($idkey) < 10) { echo 'chiave non valida'; return; } if ($usr == "" || $usr == "%20" || strlen($usr) < 1) { echo 'nome utente non valido'; return; } if ($tel == "" || $tel == "%20" || strlen($tel) < 2) { echo 'numero di telefono non valido'; return; } if ($imei == "" || $imei == "%20" || strlen($imei) < 10) { echo 'imei utente non valido'; return; } $exist = "SELECT * FROM devices "; $result = mysqli_query($con, $exist); while ($row = mysqli_fetch_array($result)) { $value = $row['idkey']; if ($value == $idkey) { echo 'chiave già memorizzata utente registrato come: ' . $row['name']; mysqli_close($con); return; } } registra($usr, $idkey, $tel, $imei); echo "Sei stato registrato correttamente!"; return; }
<?php if ($_SERVER["REQUEST_METHOD"] == "GET") { ?> <h1>Invia un post</h1> <form action="<?php $_SERVER['PHP_SELF']; ?> " method="post"> Username: <input type="text" name="utente"> Password: <input type="password" name="password"> <br><br> Titolo: <input type="text" name="titolo"> <br> Contenuto: <textarea name="contenuto" rows="8" cols="40"></textarea> <input type="submit" value="Pubblica"> </form> <?php } else { if (!utenteValido($_POST['utente'], $_POST['password'])) { ?> <h2>ERRORE</h2> <p>Non sei autorizzato ad effettuare l'inserimento.</p> <?php } else { registra($_POST['titolo'], $_POST['contenuto']); echo "<p> POST PUBBLICATO CON SUCCESSO </p>"; } } ?> </body> </html>