Edit MySQL database with php

0

I'm doing a registration system in php, and using mysql as db, I've already been able to do the registration page and it's working normally. The problem is that now I need a link to be created when the database is created and clicking on it to edit the data in the database.

The code that performs the registration:

 <?php
 /**
  * Created by PhpStorm.
  * User: Arnaud
  * Date: 22/05/2017
  * Time: 16:38
  */

 $nome = $_POST["txtnome"];
 $email = $_POST["txtemail"];
 $telefone = $_POST["txttelefone"];
 $senha = $_POST["txtpass"];
 $data = $_POST["txtdata"];
 $id = $_POST["id"];

 $con = mysqli_connect("localhost", "root", "");
 mysqli_select_db($con,"testeconstrusite");
 if (mysqli_connect_errno())
 {
     echo "Falha ao conectar ao servidor MySQL: " . mysqli_connect_error();
 }


 if(!empty($nome) && !empty($email) && !empty($telefone) && !empty($senha) && !empty($data) && !empty($id) && empty($id)) {
     mysqli_query($con, "insert into clientes (nome_cliente, email_cliente, telefone_cliente, senha_cliente, data_nasc_cliente) VALUE('$nome', '$email', '$telefone', '$senha', STR_TO_DATE('$data','%d/%m/%Y'))");
     $id_inserido = mysqli_insert_id($con);
     echo "Cliente inserido com id de número: $id_inserido <br>";
     echo "Para editar o usuário cadastrado clique <a href='index.php?id=$id_inserido'>aqui</a>";
 }

 elseif(!empty($nome) && !empty($email) && !empty($telefone) && !empty($senha) && !empty($data) && !empty($id) && !empty($id)){
     mysqli_query($con, "update clientes set nome_cliente ='$nome', email_cliente='$email', telefone_cliente='$telefone', senha_cliente='$senha', data_nasc_cliente=STR_TO_DATE('$data','%d/%m/%Y') where id_cliente = '$id'");
 }

 else{
     echo "Preencha todos os campos!";
 }

The form page:

      <!DOCTYPE html>
 <html lang="pt-br">
 <head>

     <meta charset="UTF-8">
     <link type="text/css" rel="stylesheet" media="all"  href="estilos/estilo.css" />
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script><scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/jquery.mask/1.14.10/jquery.mask.min.js"></script>

     <title>Cadastro</title>
 </head>
 <body>

 <div id="formulario" class = formulario>
 <form action="cadastrar.php" method="post">

     <input type='text' name="id" class='campo' id="id" value="<?php if(!empty($_GET["id"])){echo $_GET["id"];}; ?>">

     <label>Nome *</label><br>
     <input type='text' name="txtnome" class='campo' id="txtnome" required>
     <br>

     <label>E-mail *</label><br>
     <input type="email" class='campo' required="required" name="txtemail" id="txtemail" pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$">
     <br>

     <label>Telefone *<span class="esmaecido"></span> </label><br>
     <script type="text/javascript">$("#txttelefone").mask("(00) 0000-00009");</script>
     <input type="tel" class="campo" name="txttelefone" id="txttelefone" required="required" pattern="\([0-9]{2}\)[\s][0-9]{4}-[0-9]{4,5}" />
     <br>

     <label>Senha *</label><br>
     <input type="password" name="txtpass" class='campo' required="required" id="txtpass">
     <br>

     <label>Data de Nascimento *</label><br>
     <script type="text/javascript">$("#txtdata").mask('00/00/0000');</script>
     <input type='data' name="txtdata" id="txtdata" class='campo' required="required">
     <br>


     <input type='submit' name='BTEnvia' value='Cadastrar' class = botao><br>

 </form>
 </div>
 </body>

    
asked by anonymous 23.05.2017 / 00:43

2 answers

0

Use the same page to insert and edit records.

On this page, place a field to save the primary key of the selected record.

On the save button, if the field is empty, it performs INSERT , if it is filled, UPDATE .

At the time of opening the page from a button INSERIR CLIENTE open it normally, At the time of opening from a button EDITAR CLIENTE open it by entering a parameter, either by GET or POST , which has the value of the primary key of the record you wanted to edit.

When loading the page, check that this parameter is empty, if yes, open the screen clean for the user to register a new client, if not, execute the select in the database and fill in the fields with the data of the selected client .

Here's an example:

PHP

<?php

$nome = $_POST["txtnome"];
 $email = $_POST["txtemail"];
 $telefone = $_POST["txttelefone"];
 $senha = $_POST["txtpass"];
 $data = $_POST["txtdata"];


 $con = mysqli_connect("localhost", "root", "");
 mysqli_select_db($con,"testeconstrusite");
 if (mysqli_connect_errno())
 {
     echo "Falha ao conectar ao servidor MySQL: " . mysqli_connect_error();
 }

 if (isset($_POST["nid"]))
{
    $nid = $_POST["nid"];
    mysqli_query($con, "update clientes set nome_cliente ='$nome', email_cliente='$email', telefone_cliente='$telefone', senha_cliente='$senha', data_nasc_cliente=STR_TO_DATE('$data','%d/%m/%Y') where nid = '$nid'");
    //executa a query
}
else
{
    mysqli_query($con, "insert into clientes (nome_cliente, email_cliente, telefone_cliente, senha_cliente, data_nasc_cliente) VALUE('$nome', '$email', '$telefone', '$senha', STR_TO_DATE('$data','%d/%m/%Y'))");
    $id_inserido = mysqli_insert_id($con);
}

 ?>

HTML - > Must rename to PHP

<?php


 $con = mysqli_connect("localhost", "root", "");
 mysqli_select_db($con,"testeconstrusite");
 if (mysqli_connect_errno())
 {
     echo "Falha ao conectar ao servidor MySQL: " . mysqli_connect_error();
 }

if (isset($_GET['nid']))
{
    $nid = $_GET['nid'];
    $clienteAtual = mysql_fetch_assoc(mysql_query("
    SELECT *,
    FROM clientes
    where nid = $nid")) or die(mysql_error()); 
}

?>





<!DOCTYPE html>
 <html lang="pt-br">
 <head>

     <meta charset="UTF-8">
     <link type="text/css" rel="stylesheet" media="all"  href="estilos/estilo.css" />
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script><scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/jquery.mask/1.14.10/jquery.mask.min.js"></script>

     <title>Cadastro</title>
 </head>
 <body>

 <div id="formulario" class = formulario>
 <form action="cadastrar.php" method="post">

     <input type='number' name="txtid" class='campo' id="nid" hidden="hidden" value="<?php echo($nid); ?>">

     <label>Nome *</label><br>
     <input type='text' name="txtnome" class='campo' id="txtnome"  <?php  echo('value="'.$clienteAtual['nome_cliente'].'"');  ?> required>
     <br>

     <label>E-mail *</label><br>
     <input type="email" class='campo' required="required" name="txtemail" id="txtemail" pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$">
     <br>

     <label>Telefone *<span class="esmaecido"></span> </label><br>
     <script type="text/javascript">$("#txttelefone").mask("(00) 0000-00009");</script>
     <input type="tel" class="campo" name="txttelefone" id="txttelefone" required="required" pattern="\([0-9]{2}\)[\s][0-9]{4}-[0-9]{4,5}" />
     <br>

     <label>Senha *</label><br>
     <input type="password" name="txtpass" class='campo' required="required" id="txtpass">
     <br>

     <label>Data de Nascimento *</label><br>
     <script type="text/javascript">$("#txtdata").mask('00/00/0000');</script>
     <input type='data' name="txtdata" id="txtdata" class='campo' required="required">
     <br>

     <input type='submit' name='BTEnvia' value='Cadastrar' class = botao><br>

 </form>
 </div>
 </body>
    
23.05.2017 / 01:09
0

You can create another change page for this. page_alteracao.php

Only you pass as parameter the id that was saved in the table.

So the link to change would look like this.

<a href="pagina_alteracao.php?id=id_cliente" Editar </a>

With this id you can get through the query:

SELECT * FROM client where id = client_id

You only need to query to change the data.

    
23.05.2017 / 01:58