Database does not show the data entered by the user

Asked

Viewed 89 times

1

Good morning, I’m trying to make a news system, but the database does not receive the data from php. I would like to know how to solve this.

<html>
<head>
    <meta charset="utf-8">
    <title>Inserir Notícia</title>    
</head>

<body>
    <?php

        $host = "localhost";
        $user = "root";
        $pass = "";
        $banco = "sistemanoticias";
        $conexao = @mysql_connect($host, $user, $pass) or die(mysql_error()); 
        mysql_select_db($banco) or die (mysql_error());

    ?>

    <?php 

        $titulo = $_POST['titulo'];
        $campo = $_POST['campo'];

        $sql = mysql_query("INSERT INTO noticias(titulo, campo) VALUES('$titulo, $campo)"); 
        echo "Notícia inserida com sucesso";
    ?>

    <a href="inicio.html">Retornar para o Início</a>

</body>
</html>
  • 1

    VALUES('$titulo, $campo) trouble with quotation marks...

  • I fixed that mistake, but still not sending.

  • 1

    Have you considered using the PDO in place of mysql? The benefits are enormous.

  • 1

    Utilize mysqli_* or PDO rather than mysql_*, as it lies DEPRECATED.

  • Is there an error on the screen? Have you tried using error_reporting(1); ?

1 answer

3

Text fields should be between single quotes each, always add mysql_error() to get the error message from the bank.

$titulo = mysql_real_escape_string($_POST['titulo']);
$campo = mysql_real_escape_string($_POST['campo']);
$sql = "INSERT INTO noticias(titulo, campo) VALUES('$titulo', '$campo')";
$result = mysql_query($sql) or die(mysql_error());

Recommended reading:

Why should we not use mysql type functions_*?

Mysqli vs PDO - which is the most recommended to use?

Why do they say using @arroba to suppress errors is a bad practice?

Browser other questions tagged

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