<?php
if(isset($_POST["submit"])){
include 'db.php';
$title=$_POST['title'];
$desc=$_POST['desc'];
try{
$sql=" INSERT INTO posttbl (title,desc)
VALUES ('$title','$desc')" ;
$e=$pdo->exec($sql);
}catch (PDOexception $e){
$error="error inserting";
include 'error.php';
exit();
}
header('Location:/test/');
}
?>
*/////////FORM
<html>
<body>
<h3>Add item</h3>
<form action="" method="POST">
Title: <input type="text" name="title"><br />
Item Description: <textarea name="desc" id="comment" type="text"></textarea><br />
<input name="submit" type="submit" value="Submit" />
</form>
代码正在运行,但它没有在我的数据库中插入任何东西......我错过了什么?或做某事不对?我的数据库由3列组成.postid,title和desc ...所以什么错了?它是if isset($ _ POST [“submit”])){???或其他什么?
答案 0 :(得分:0)
这是将行插入PDO的正确语法
try {
$var = array(
'title' => $title,
'desc' => $desc
);
$req = $bdd->prepare("INSERT INTO posttbl (title,desc) VALUE (:title,:desc)");
$req->execute($var);} catch (Exception $e) {
echo $e->getMessage();}