Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <head>
- <title>Exemplo</title>
- <style type="text/css">
- #geral{
- margin: auto;
- background-color: lightblue;
- color: white;
- width: 400px;
- height: auto;
- border:1px solid black;
- border-radius: 15px;
- }
- .ger{
- margin: auto;
- background-color: lightblue;
- color: white;
- width: 400px;
- height: auto;
- border:1px solid black;
- border-radius: 15px;
- }
- p{
- text-align: center;
- }
- #bt1{
- width: 260px;
- height: 30px;
- background-color: lightgrey;
- color: black;
- }
- .alert{
- text-align: center;
- margin: auto;
- background-color: red;
- margin-right: 15px;
- margin-left: 15px;
- }
- .ok{
- text-align: center;
- margin: auto;
- background-color: green;
- margin-right: 15px;
- margin-left: 15px;
- }
- .tbcriada{
- margin: auto;
- }
- </style>
- </head>
- <body>
- <?php
- $bd_host="localhost";
- $bd_user="root";
- $bd_password="";
- $bd_database="da";
- $msg= "";
- $existe="";
- $num=$nome=$email=$tele="";
- $mq = new mysqli($bd_host,$bd_user,$bd_password,$bd_database);
- if(isset($_POST["submit"])){
- $qr = "INSERT INTO tbl_ex(num,nome,email,tele) VALUES(?,?,?,?)";
- $st = $mq->prepare($qr);
- $st->bind_param('isss', $_POST["num"],
- $_POST["nome"],
- $_POST["email"],
- $_POST["tele"]);
- if ($st->execute() && $st->affected_rows>0){
- $msg= "<p class='ok'>Foi inserido/atualizado um registo!</p>";
- }
- else{
- $msg='<p class="alert"> Erro: ('. $mq->errno .') '. $mq->error .'</p>';
- $erro=1;
- }
- $st->close();
- }
- if(isset($_POST["Consulta"])){
- $qr="SELECT * FROM tbl_ex ORDER BY num";
- $results = $mq->query($qr);
- echo "<table class=tbcriada border='1'>";
- echo "<td>Numero</td>";
- echo "<td>Nome</td>";
- echo "<td>Email</td>";
- echo "<td>Telemovel</td>";
- while ($row = $results->fetch_assoc()) {
- echo "<tr>";
- echo "<td>".$row["num"]."</td>";
- echo "<td>".$row["nome"]."</td>";
- echo "<td>".$row["email"]."</td>";
- echo "<td>".$row["tele"]."</td>";
- echo "</tr>";
- }
- echo "</table>";
- }
- else{
- if(isset($_POST["pesq"])){
- $qr="SELECT * FROM tbl_ex where num=?";
- $st=$mq->prepare($qr);
- $st->bind_param('i', $_POST["num"]);
- $st->execute();
- $st->bind_result($num,$nome,$email,$tele);
- if($st->fetch()){
- $existe=true;
- $msg='<p class="ok" > Registo Encontrado</p>';
- }
- else{
- $existe=false;
- $msg='<p class="alert">Registo nao Encontrado</p>';
- }
- $st->close();
- }
- else{
- if(isset($_POST["Altera"])){
- $qr="UPDATE tbl_ex SET nome=?, email=?, tele=? WHERE num=?";
- $st=$mq->prepare($qr);
- $st->bind_param('sssi',$_POST["nome"],$_POST["email"],$_POST["tele"],$_POST["num"]);
- $st->execute();
- $st->close();
- }
- else{
- if(isset($_POST["apaga"])){
- $qr="DELETE FROM tbl_ex WHERE num= ?";
- $st=$mq->prepare($qr);
- $st->bind_param('i',$_POST["num"]);
- $st->execute();
- $st->close();
- }
- }
- }
- }
- ?>
- <br>
- <form method="post" action="" id="tb1" class=ger name="tb1">
- <input type="hidden" name="pesq" value="1"/>
- <p>Seleciona: <select name="num" id="num" onChange="document.tb1.submit();">
- <option value="-1">
- Selecione um registo
- </option>
- <?php
- $qrs="SELECT * FROM tbl_ex";
- $st = $mq->query($qrs);
- $option = '';
- while($row = $st->fetch_assoc())
- {
- echo '<option value = "'.$row['num'].'">'.$row['nome'].'</option>';
- }
- $st->close();
- ?>
- </select>
- </form>
- <form method="post" action="" id="geral" class="ger">
- <p>Numero:<input type="number" name="num" value="<?php echo $num ?>"><input type="submit" name="pesq" value="Pesquisa"></p>
- <p>Nome:<input type="text" name="nome" value="<?php echo $nome ?>"></p>
- <p>Email:<input type="text" name="email" value="<?php echo $email ?>"></p>
- <p>Telemovel:<input type="text" name="tele" value="<?php echo $tele ?>"></p>
- <p><input type="submit" name="submit" value="insira" id="bt1"></p>
- <p><input type="submit" name="Consulta" value="consulta" id="bt2"></p>
- <?php if ($existe==true) {?>
- <p><input type="submit" name="Altera" value="Alterar" /></p>
- <p><input type="submit" name="apaga" value="Eliminar" /></p>
- <?php }?>
- <p>
- <?php
- echo $msg;
- $mq->close();
- ?>
- </p>
- </form>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement