Problem to send PHP data to database

Asked

Viewed 203 times

1

I’m starting with PHP and I’m having trouble sending to the database, unfortunately I can’t find the error, if anyone can help, grateful.

The HTML is like this:

<form method="post" action="blog.php"  >    
                    <input style="font-family:'open sans',sans-serif;font-size:20px;color:#212121"type="text" name="titulo" placeholder="Título" required />
                        <br>
                    <input type="text" name="texto" placeholder="Texto"  required />
                        <br>
                    <input style="background-color:red;color:white" type="submit" name="submit" value="enviar" />
                </form> 

And PHP like this:

<?php
$titulo = $_POST ['titulo'];
$texto = $_POST ['texto'];
$imagem = "imagem";
echo "$titulo";

$conexao = mysqli_connect("localhost", "root", "", "blog");

mysqli_query($conexao, "INSERT INTO postagens (titulo,texto,imagem) VALUES ('$titulo','$texto,'$imagem')");

echo "Sucesso";
?>

The name of the database is 'Blog' and the table 'posts'.

  • Which texts appear on the screen? Gives some error message?

3 answers

1


<?php
// verificação do metodo post
if ( $_SERVER['REQUEST_METHOD'] == "POST" )
{
    // obtendo os dados do form
        $titulo = $_POST['titulo'];
        $texto  = $_POST['texto'];
        $imagem = "imagem";
        echo "Título: ".$titulo." <br />";

    // realizando a conexão
        $conexao = mysqli_connect("localhost", "root", "", "blog");
        $query   = mysqli_query($conexao, "INSERT INTO postagens(titulo,texto,imagem) VALUES ('$titulo','$texto','$imagem')");

    // se executar a query
        if ($query)
        {
            echo "Sucesso";

        }else
        {
            echo "[ERRO]: ".mysqli_connect_error($conexao);
        }
}
?>

See if this solves your problem.

  • Man, you did, thanks a lot!

  • @Alisson glad you have solved your problem, mark the answer as correct to help upcoming users with the same.

0

It is missing a single quote after $text in the query, try to put to see if it works

mysqli_query($conexao, "INSERT INTO postagens (titulo,texto,imagem) VALUES ('$titulo','$texto','$imagem')");

-1

<?php
$titulo = $_POST ['titulo'];
$texto = $_POST ['texto'];

$conexao = mysqli_connect("localhost", "root", "", "blog");

echo "Sucesso";

mysqli_query($conexao, "INSERT INTO postagens (titulo,texto) VALUES ("{$titulo}","{$texto}"));

Try that one query, it should work, but take the image initially, because to do the upload image you will need to make changes to both your HTML and PHP.

  • And why do you think this will work? PHP can interpret variables within a string set by double quotes; keys are needed only with expressions. Incidentally, you broke your string by using double quotes inside it, which would generate syntax error. And the image field is at a constant value, so I don’t see, for now, any relation to the problem.

  • Actually I don’t think it will work. I always do it and it works.

Browser other questions tagged

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