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].– NoobSaibot
valeu was having trouble highlighting the code is that I’m by cell phone
– user115743
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.
– Felipe J. R. Vieira
updated the post
– user115743
In the Insert is not used
SET
, but yesVALUES
:insert into [table] values ([values])
– Woss