2
I’m trying to send a mysql database the data recorded by a form. I have created a db_provida BD and a 4-column 'testimonial' tablet. I can’t get this code to save the record. Here are the components:
Table 'testimony':
Html code:
<form action="testemunho.php" method="post">
Nome: <input type="text" name="nome"/>
<br>
Email: <input type="text" name="email"/>
<br>
Testemunho: <textarea name="testemunho" rows="10" cols="80"></textarea> <br/><br/>
<input type="submit" value="Enviar"/>
</form>
And php code:
<?php
//verifica se existe conexão com bd; caso não tenta, cria uma nova
$conexao = mysql_connect('localhost','giuseppe','joanin21') //porta, usuário, senha
or die("Erro na conexão com banco de dados"); //caso não consiga conectar mostra a mensagem de erro mostrada na conexão
$select_db = mysql_select_db("db_provida"); //seleciona o banco de dados
//Abaixo atribuímos os valores provenientes do formulário pelo método POST
$nome = $_POST['nome'];
$email = $_POST['email'];
$testemunho = $_POST['testemunho'];
$string_sql = "INSERT INTO testemunho (id,nome,email,testemunho) VALUES (null,'$nome','$email','$testemunho')"; //String com consulta SQL da inserção
mysql_query($string_sql,$conexao); //Realiza a consulta
if(mysql_affected_rows() == 1){ //verifica se foi afetada alguma linha, nesse caso inserida alguma linha
echo "<p>Testemunho Registrado</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";
}
mysql_close($conexao); //fecha conexão com banco de dados
?>
What’s the mistake? Recommend.
– Francisco
Hi Francisco! I can’t find anything recorded in the 'testimony' table, nor does he present what error is happening. As if the form did not pass the tags of the.php testimony.
– Bepi
I edited the answer in more detail and also an example of your code running on my server
– user60252
Hi Leo! Thank you so much for sharing your knowledge. I work in a telecommunications center of a university and, only now I realized that the environment of my machine (shared with other users) has mariaDB as a database manager and nginex as a web server. I’m still in control, but I think the problem of lack of communication with DB should be a matter of localhost environment.
– Bepi
OK Leo! In fact, I deleted everything I had in www - where I even had a Joomla installed - and, putting this code of yours, everything went back to working right, also with the recording of the strings. Thanks bro!
– Bepi
then be sure to read this post https://pt.meta.stackoverflow.com/questions/1078/como-e-por-que-aceitar-uma-resposta/1079#1079
– user60252