Form html with PHP code to insert into mysql

-1

I'm starting now with HTML, PHP and MySQL with Sublime using XAMPP.

I'm doing a test form in HTML to include direct in MySQL.

But you are not including the data.

I've done several tests, including the last one I copied from the internet, and it does not include it in the form.

Form Code:

<!DOCTYPE html>
<html lang="pt-br">
<head>
  <meta charset="utf-8">
  <title> Testando conexão ao banco de dados </title>
</head>
<body>
  <h3>Formulário de Cadastro de Clientes</h3><br>
  <form name="Cadastro" action="cadastrar.php" method="POST">
    <label>Nome do Cliente: </label>
    <input type="text" name="NomeCliente" size="30"><br>
    <label>Sobrenome do Cliente: </label>
    <input type="text" name="SobrenomeCliente" size="45"><br>
    <label>Sexo do Cliente: </label>
    <select name="Sexo">
      <option value="M">Masculino</option>
      <option value="F">Feminino</option>
      <option value="N">Não Declarado</option>
    </select><br>
    <input type="submit" name="enviar" value="Enviar">
  </form>
</body>
</html>

PHP code that connects to the database:

<?php
$nome = $_POST['NomeCliente'];
$sobrenome = $_POST['SobrenomeCliente'];
$sexo = $_POST['Sexo'];
$strcon = mysqli_connect('localhost','root','','cadastro') or die('Erro ao conectar ao banco de dados');
$sql = "INSERT INTO banco_teste VALUES ";
$sql .= "('$nome', '$sobrenome', '$sexo')"; 
mysqli_query($strcon,$sql) or die("Erro ao tentar cadastrar registro");
mysqli_close($strcon);
echo "Cliente cadastrado com sucesso!";
echo "<a href='formulario.html'>Clique aqui para realizar um novo cadastro</a><br>";
echo "<a href='consulta.php'>Clique aqui para realizar uma consulta</a><br>";
?>

When I include data in the form it appears on the screen and does not enter the data in phpMyAdmin:

<?php
$nome = $_POST['NomeCliente'];
$sobrenome = $_POST['SobrenomeCliente'];
$sexo = $_POST['Sexo'];
$strcon = mysqli_connect('localhost','root','','cadastro') or die('Erro ao conectar ao banco de dados');
$sql = "INSERT INTO 'banco_teste' VALUES ";
$sql .= "('$nome', '$sobrenome', '$sexo')"; 
mysqli_query($strcon,$sql) or die("Erro ao tentar cadastrar registro");
mysqli_close($strcon);
echo "Cliente cadastrado com sucesso!";
echo "<a href='formulario.html'>Clique aqui para realizar um novo cadastro</a><br>";
echo "<a href='consulta.php'>Clique aqui para realizar uma consulta</a><br>";
?>
    
asked by anonymous 13.06.2018 / 17:45

3 answers

2

The problem is that in your query you are not declaring where the data will be entered.

How are you?

$sql = "INSERT INTO 'banco_teste' VALUES ";
$sql .= "('$nome', '$sobrenome', '$sexo')";

How should it be:

$sql = "INSERT INTO 'banco_teste'(nome, sobrenome, sexo) VALUES ";
$sql .= "('$nome', '$sobrenome', '$sexo')"; 
    
13.06.2018 / 18:42
1

In the HTML form you must include an action, for example:

<form action="inserir.php">
<div class="form-group>
   <label for="nomeCliente">Nome do Cliente</label>
   <input type="text" id="nome" name="nome">
</div>
<button type="submit">Inserir</button>

In a page called insert.php you have to put the following code:

<?php
// LIGAR A BASE DE DADOS
$link = mysqli_connect("localhost", "user", "password", "nome_da_base_de_dados");

// VERIFICAR CONEXÃO
if($link === false){
    die("ERRO AO LIGAR À BASE DE DADOS. " . mysqli_connect_error());
}

// ESCAPE INPUTS
$nome = mysqli_real_escape_string($link, $_REQUEST['nome']);

// INSERIR DADOS NA TABELA
$sql = "INSERT INTO nome_da_tabela (name) VALUES ('$name')";

// VOLTAR A PAGINA DO FORMULARIO
if(mysqli_query($link, $sql)){
    mysqli_close($link);
    header('Location: ../pagina_do_formulario.php');
    exit;
} else {
    echo "Não foi possível adicionar o cliente.";
}
?>

I hope I have helped.

    
13.06.2018 / 18:09
0

When you submit the form, what appears to you is the PHP code that should be writing to the database, the way you should be accessing the page is wrong.

Are you using an address type http://localhost/omeuprojecto/ or file:///caminho/para/o/projecto/index.html ?

If it's the 2nd, you have to make sure you have XAMPP started, and try to access it using http://localhost/ .

If it does not work, XAMPP may be on a different port, to see which port, you can open the XAMPP Control Panel and see which port Apache is using, in principle it will be 80 and 443 , if it is different from 80 , for example 81 , you must use http://localhost:81

    
13.06.2018 / 18:57