1
I’m doing a simple registration using Mysql(bd), PHP(checks and connection to bd) and HTML(page). When I click on my button to register (save data in the bd) it returns my own PHP code. I’m new to PHP and this is my first project linking a database by code, so I don’t know if it’s right.
All I want is for the data to be saved in the BD when the Register button is clicked.
Screenshot of my HTML site
HTML
<!DOCTYPE html>
<head>
<title>Cadastro de clientes</title>
<meta charset="UTF-8">
<link rel="stylesheet" href="css/style.css">
<link REL="SHORTCUT ICON" HREF="imgs/icone.png" type="image/x-icon">
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.css">
</head>
<input type="button" class="voltar" value="←" onclick=" return BackToComMenu()">
<body class="teste">
<center><div class="caixa">
<center><img id="logo" src="imgs/logo.png" alt="Logo da FullTime"></center>
<main>
<form action="php/cadastrar.php" method="POST" id="FormCadastro">
<p>Cadastro de Clientes</p>
<div class="inputWithIcon">
<input name="nome" type="text" placeholder="Nome">
<i class="fa fa-user fa-lg fa-fw" aria-hidden="true" name="user"></i>
</div>
<div class="inputWithIcon">
<input name="email" type="text" placeholder="Email" onclick="return checarEmail()" >
<i class="fa fa-envelope fa-lg fa-fw" aria-hidden="true" name="user"></i>
</div>
<div class="inputWithIcon">
<input name="celular" type="text" placeholder="Telefone" maxlength="11">
<i class="fa fa-phone fa-lg fa-fw" aria-hidden="true" name="user"></i>
</div>
<div class="inputWithIcon">
<input name="cpf" type="text" placeholder="CPF" maxlength="14" OnKeyPress="formatar('###.###.###-##', this)">
<i class="fa fa-address-card fa-lg fa-fw" aria-hidden="true" name="user"></i>
</div>
<input type="submit" value="Cadastrar" name="enviar" onclick="return cadastrar()">
</div></center>
</form>
</main>
</body>
PHP
Connection with the BD:
<?php
$conect = mysql_connect("localhost", "root", "");
if (!$conect) die ("<h1>Falha na conexão com o Banco de Dados!</h1>");
$db = mysql_select_db("bd.sql");
?>
PHP code to register:
<?php
session_start();
include_once("php/servidor.php");
if (isset($_GET['enviar'])) {
if (!empty($_GET['nome']) || !empty($_GET['email']) || !empty($_GET['telefone']) || !empty($_GET['cpf'])){
$nome=$_GET['nome'];
$email=$_GET['email'];
$telefone=$_GET['telefone'];
$cpf=$_GET['cpf'];
$comando="INSERT INTO clientes(nomecli, emailcli, telefone, cpfcli) VALUES ('$nome', '$email', '$telefone', '$cpf')";
$enviar=mysqli_query($conn, $comando);
if ($enviar) {
$_SESSION['mensagem']="Cadastrado com Sucesso";
header("location:index.html");
exit;
}
else{
$_SESSION['mensagem']="Erro ao cadastrar";
header("location:cadastro.html");
exit;
}
}
else{
$_SESSION['mensagem']="Algum dos campos ficou em branco";
header("location:cadastro.html");
exit;
}
}
else{
header("location:index.html");
exit;
}
I found the solution to my problem via the following link: https://answall.com/questions/230804/erro-uncaught-error-class-mysqli-not-found/230862#230862
– Jonatan