Edit Mysql database with php

Asked

Viewed 1,147 times

0

I’m making a registration system in php, and using mysql as db, I’ve been able to make the registration page and it’s working normally. The problem is that now I need that when the registration is created a link is generated to edit and that by clicking on it it is possible to edit the data in the database.

The code that executes the register:

 <?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 page of the form:

      <!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>
     <script src="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>

  • use the same page, and use an Hidden field to store the primary key. If the key is empty, you enter, otherwise update

  • But how do I create a link for people to click and the primary key is already filled? I have an html page and a php page.

  • @Rovannlinhalis

  • you must have a correct customer insert page? Ex. you have an insert client button, which opens this page, make an edit client that will also open this page, but passing a value in the POST with the key that you need to edit. From there, just test if this value is not empty and give the select in the database, and fill in the fields

  • @Rovannlinhalis tried, but I can’t understand

  • try to edit your question and post the code of the pages you have

  • @Rovannlinhalis edited

Show 2 more comments

2 answers

0


Use the same page to insert and edit records.

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

On the save button, if the field is empty, perform the INSERT, if filled in, UPDATE.

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

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

Follow 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>
     <script src="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>
  • I already managed to add the hidden id field on the first page by pulling the id when the person clicks the link to edit @Rovannlinhalis now missing to make the change in the registration on the condition.

  • @Arnaudlanna edited my answer with his code, see if it helps

  • when I open the index the following error is returned: ( ! ) Notice: Undefined variable: nid in C:\wamp64\www\testeb\index.php on line 42 Call Stack #TimeMemoryFunctionLocation 10.0003241144{main}( )...\index.php:0 "> Nome *

  • I managed to modify my code, it’s working perfectly, just doesn’t fill in the fields, how do I do it?

  • in the code I changed, the id and name fields should be filled in

0

You can create another page to change this. pagina_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=id_client

Ai only need to do the query to change the data.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.