PHP + Mysql --- Save data to BD

Asked

Viewed 130 times

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

inserir a descrição da imagem aqui


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>


MysqlBD


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

2 answers

1

Look, the mistake I see in this code is that you send the information by the POST method on line 19

<form action="php/cadastrar.php" method="POST" id="FormCadastro">

And receive them by the GET (url) method in the.php register ! I recommend (even for security), send and receive the information via POST. The code would look like this

<?php
session_start();
include_once("php/servidor.php");

if (isset($_POST['enviar'])) {
    if (!empty($_POST['nome']) || !empty($_POST['email']) || !empty($_POST['telefone']) || !empty($_POST['cpf'])){

        $nome=$_POST['nome'];
        $email=$_POST['email'];
        $telefone=$_POST['telefone'];
        $cpf=$_POST['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;
}
?>
  • Hello, thanks for the help. I’ve really been inattentive, but the browser keeps returning me its own code as an answer.

  • @Jonatanrocha could send a print?

  • Hello @Danieldeaguida, I did another post, because this was classified as "out of scope", and there I managed to solve my problem !! The problem was in the php folder, the php.ini file and its extensions to be more specific :D

-2

  • Thanks for the help, I made the appropriate modifications and yet the browser continues to return me the PHP code as a response.

Browser other questions tagged

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