problem in sending php form data to Mysql database

Asked

Viewed 74 times

-2

Hello, I am beginner in PDO and in my development of a form I can not send anything to the database and I can not identify the problem in my php form, someone could help me please?

FILE index.php(ex):

    <?php session_start();?>
<input type="text" name="nome" data-required="true" placeholder="Nome*">

<input type="email" name="email" data-required="true" placeholder="Endereço de e-mail*">

<input type="text" name="assunto" data-required="true" placeholder="Assunto*">

<textarea name="message" data-required="true" placeholder="Message*"></textarea>

<button type="submit" name="SendCadCont">Enviar</button>

FILE send_mail.php(ex):

<?php 
session_start();
$conn = new PDO("mysql:host=localhost;dbname=teste", "root", "root"); 

// Verificação do botão quando apertado.
$SendCadCont = filter_input(INPUT_POST, 'SendCadCont', FILTER_SANITIZE_STRING);
if ($SendCadCont) {

    //Recebe os dados do formulário.
    $nome = filter_input(INPUT_POST, 'nome', FILTER_SANITIZE_STRING);
    $email = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_STRING);
    $assunto = filter_input(INPUT_POST, 'assunto', FILTER_SANITIZE_STRING);
    $message = filter_input(INPUT_POST, 'message', FILTER_SANITIZE_STRING);

    //Insere os dados no DB
    $result_msg_cont = "INSERT INTO msg (nome, email, assunto, message) VALUES (':nome', ':email', ':assunto', ':message')";

    $insert_msg_cont = $conn->prepare($result_msg_cont);
    $insert_msg_cont->bindParam(':nome', $nome);
    $insert_msg_cont->bindParam(':email', $email);
    $insert_msg_cont->bindParam(':assunto', $assunto);
    $insert_msg_cont->bindParam(':message', $message);

    if ($insert_msg_cont->execute()) {
        $_SESSION['msg'] = 'Enviado';
        header("location: email");
    }else{
        $_SESSION['msg'] = 'Não enviado';
        header("location: email");
    }
}else{
    $_SESSION['msg'] = 'Problema';
    header("location: email");
}

?>

... And even so it does not send the data to the database. I thank anyone who can help me.

2 answers

0

Hello M4RC0SJS, all right?
Change your index.php to:

<form method="POST" action="send_mail.php">
  <input type="text" name="nome" data-required="true" placeholder="Nome*">
  <input type="email" name="email" data-required="true"       placeholder="Endereço de e-mail*">
  <input type="text" name="assunto" data-required="true" placeholder="Assunto*">
  <textarea name="message" data-required="true" placeholder="Message*"></textarea>
  <button type="submit" name="SendCadCont">Enviar</button>
</form>

There are several ways to insert data into a PHP database, I will make the script the way I prefer and for me I think it is the most basic but it works!

<?php
// Conexão com a base de dados, podes fazer a partir de outro ficheiro e carregá-lo com "include_once("arquivo.php")"
define("HOST",  "localhost");
define("DBUSER", "root");
define("DBPASS", "dbpass");
define("DBNAME", "dbname");

$conn = mysqli_connect(HOST, DBUSER, DBPASS, DBNAME) or die("Erro ao conectar com a base de dados.");

if(isset(SendCadCont)) {
  // Recebe os dados do formulário e armazena-os em vars
  $nome = mysqli_real_escape_string($conn, $_POST['nome']);
  $email = mysqli_real_escape_string($conn, $_POST['email']);
  $assunto = mysqli_real_escape_string($conn, $_POST['assunto']);
  $message = mysqli_real_escape_string($conn, $_POST['message']);
  
  // Insere os dados na db:
  $query="INSERT INTO msg(nome, email, assunto, message) VALUES('$nome', '$email', '$assunto', '$message');
  mysqli_query($conn, $query);
}


?>

I hope I’ve been of some help ;)

  • Thank you very much, your concept uses mysql_connect and in the connection I was using PDO to have more security against SQL attacks(But it worked yes) Thank you very much.

-2


Change this line:

<button type="submit" name="SendCadCont">Enviar</button>

for:

<button type="submit" name="SendCadCont" value="1">Enviar</button>
  • 1

    I can’t believe it was the lack of (value="1").. my friend thank you very much. it worked 100%

Browser other questions tagged

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