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
– Rovann Linhalis
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.
– Arnaud Lanna
@Rovannlinhalis
– Arnaud Lanna
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
– Rovann Linhalis
@Rovannlinhalis tried, but I can’t understand
– Arnaud Lanna
try to edit your question and post the code of the pages you have
– Rovann Linhalis
@Rovannlinhalis edited
– Arnaud Lanna