Error 404 when saving a form - Wordpress

Asked

Viewed 25 times

0

Ladies and gentlemen, good night!

I am using the wordpress tool a little while ago and I came up with a problem that I could not solve. When I try to save a form in mysql database error 404 appears. I would like a help on how to fix

html:

<form action="teste.php" method="post">
Nome: <input type="text" name="nome">
<br>
Email: <input type="text" name="email">
<br>
Observação: <textarea name="observacao" rows="10" cols="80"></textarea> <br><br>
<input type="submit" value="Enviar">

php:

<?php
//aqui é só um exemplo para não rodar o script abaixo sem necessidade
if ((isset($_POST['email']))&&(!empty($_POST['email']))){

   //porta, usuário, senha, nome data base
   //caso não consiga conectar mostra a mensagem de erro mostrada na conexão
   $conexao = mysqli_connect("localhost", "paroq110_pjn", "hrsist@20", "paroq110_pjn") or die("Erro na conexão com banco de dados " . mysqli_error($conexao));

  //Abaixo atribuímos os valores provenientes do formulário pelo método POST
  $nome = $_POST['nome']; 
  $email = $_POST['email'];
  $observacao = $_POST['observacao'];

   $string_sql = "INSERT INTO pjn_teste (nome,email,observacao) VALUES ('$nome','$email','$observacao')";
   $insert_member_res = mysqli_query($conexao, $string_sql);
   if(mysqli_affected_rows($conexao)>0){ //verifica se foi afetada alguma linha, nesse caso inserida alguma linha
       echo "<p>Teste ok</p>";
       echo '<a href="testimonianze.html">Voltar para formulário de cadastro</a>'; //Apenas um link para retornar para o formulário de cadastro
   } else {
       echo "Erro, não foi possível inserir no banco de dados";
   }
   mysqli_close($conexao); //fecha conexão com banco de dados
}else{
    echo "Por favor, preencha os dados";
}

    mysql_close($conexao); //fecha conexão com banco de dados 
?>

Detail: I inserted php in the root of the theme folder in use... I don’t know if I did it correctly.

2 answers

1

Without having access to logs or more details, I would say that is some configuration in Mysql preventing this access.

In addition, I suggest you do not directly manipulate the Wordpress database and choose to use the API that Wordpress itself offers to manipulate the database, through the class WPDB. I’d also take a look at the documentation on hooks/Hooks, which are of two types: actions/actions and filters/Filters.

As mentioned that is new in Wordpress, I would say that read the developer documentation on and test how actions and filters work, it will be the best investment in your time if you want to learn more of Wordpress, which is architected to be quite customized through these hooks.

0

The problem is in <form action="teste.php" method="post"> Note that you are trying to trigger a test.php file without putting the full URL of where that file is. For example, if your file is inside the theme folder it should be like

<form action="<?php bloginfo('stylesheet_directory'); ?>/teste.php" method="post">

where the

<?php bloginfo('stylesheet_directory'); ?>/

points to the folder of your active theme.

This answer only serves the issue of error 404, the way you want to save the data in the database, is not a good practice in Wordpress. I recommend searching for add_post_meta() and wpdb.

Browser other questions tagged

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