Form data is not sent to the database

Asked

Viewed 101 times

0

I’m having a problem sending data from a form to the database the script runs without pointing out an error apparently but does not enter the data someone would know the problem follows the code :

<!DOCTYPE html>
<html lang="pt-br">
<head>
<!-- Meta tags Obrigatórias -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

<!-- Bootstrap CSS -->
<link rel="stylesheet" href="bootstrap/css/bootstrap.css">

<title>Olá, mundo!</title>
</head>
<body>
<?php

try {
$pdo = new PDO("mysql:dbname=tarefas;host=localhost", "root", "admin");
} catch (PDOException $e) {
echo "ERRO: " . $e->getMessage();
}
?>
<h1>Olá, mundo!</h1>
<br/>
<form action="tarefas.php" method="POST">
Nome: <input name="nome" type="text"/> <br/>
Descricao: <input name="descricao" type="text"/><br/>
<input value="Enviar" type="submit"/>
</form>
<?php
$qt_por_pagina = 5;

$total = 0;
$sql = "SELECT COUNT(*) as c FROM tarefas";
$sql = $pdo->query($sql);
$sql = $sql->fetch();
$total = $sql['c'];
$paginas = $total / $qt_por_pagina;

$pg = '1';
if (isset($_GET['p']) && !empty($_GET['p'])) {
$pg = addslashes($_GET['p']);
}

$p = ($pg - 1) * $qt_por_pagina;

$sql = "SELECT * FROM tarefas LIMIT $p, $qt_por_pagina";
$sql = $pdo->query($sql);

if ($sql->rowCount() > 0) {
foreach ($sql->fetchAll() as $item) {
    echo "Id:".$item['id'] ."<br/>Nome:".$item['nome']. "<br/>Descricao: " . $item['descricao'] . "<br/>";
}
}

echo "<hr/>";

for ($q=0; $q < $paginas; $q++) {
echo '<a href="./?p='.($q+1).'">[ '.($q+1).' ]</a>';
}

?>
<!-- JavaScript (Opcional) -->
<!-- jQuery primeiro, depois Popper.js, depois Bootstrap JS -->
<script src="bootstrap/jquery/jquery.js"></script>
<script src="bootstrap/popper/popper.js"></script>
<script src="bootstrap/bootstrap/js/bootstrap.js"></script>
</body>
</html>



<?php 
try {
  $pdo = new PDO("mysql:dbname=tarefas;host=localhost", "root", "admin");
}
catch (PDOException $e) {
  echo "ERRO: " . $e->getMessage();
}

//definindo posts
$nome = isset($_POST['nome'])?$_POST['nome']:null;
$descricao = isset($_POST['descricao'])?$_POST['descricao']:null;

if(empty($nome)){
  echo "Colocar o nome!";
}
if(empty($descricao)){
  echo "Colocar descricao!";
} else {
  $pdo->exec("INSERT INTO tarefas SET nome='".$nome."', descricao='".$descricao."'");

  echo "Tarefa enviada<br/><a href=\"index.php\">Voltar</a>";
  exit();
  }
  ?>
  • Welcome to the Stackoverflow in Portuguese. To format the code, select it and hit the shortcut CTRL+K or click the button {} in the editor. If you need help, go to [help].

  • valeu was having trouble highlighting the code is that I’m by cell phone

  • Put the HTML of your form in the question to make it easier to understand the problem. Only with this code snippet shown can not identify the error.

  • updated the post

  • 1

    In the Insert is not used SET, but yes VALUES: insert into [table] values ([values])

3 answers

1

I managed to solve, the problem was that in the id column I forgot to define as auto increment but I updated the script to not take the risk of taking sql Injection anyway if someone wants to be working there :

<?php 
ini_set('display_errors', true);
error_reporting(E_ALL);
try {
$pdo = new PDO("mysql:dbname=tarefas;host=localhost", "root", "admin");
} catch (PDOException $e) {
echo "ERRO: " . $e->getMessage();
}
//definindo posts

$nome = isset($_POST['nome'])?$_POST['nome']:null;
$descricao = isset($_POST['descricao'])?$_POST['descricao']:null;

if(empty($nome))
{
echo "Colocar o nome!";
}
if(empty($descricao))
{
echo "Colocar descricao!";
}else{

$sql = "INSERT INTO tarefas(nome, descricao) VALUES(:nome, :descricao) ";

$stmt = $pdo->prepare( $sql ); 

$stmt->bindParam( ':nome', $nome ); 

$stmt->bindParam( ':descricao', $descricao );

$result = $stmt->execute();   

if ( ! $result ) 
{ 
var_dump( $stmt->errorInfo() ); 

exit(); 

}   
echo $stmt->rowCount() . "linhas inseridas";
}
?>

0

Try it this way:

  $execute=$pdo->prepare("INSERT INTO tarefas (nome,descricao) VALUES ('$nome','$descricao')");
  $execute->execute();
  echo "Tarefa enviada<br/><a href=\"index.php\">Voltar</a>";
  exit();
  • I had tried so before and I did not succeed still the same thing

0

Change to the following:

else 
{
   $sql = ("INSERT INTO tarefas (nome, descricao) VALUES ('$nome', 
  '$descricao');") or die (mysql_error());
   $result = mysql_query($sql); // Executa o insert

   echo "Tarefa enviada<br/><a href=\"index.php\">Voltar</a>";
   exit();
}

If this does not work, test your variables and connect to the bank.

Browser other questions tagged

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