sql没有'执行' INSERT INTO语句..但代码很奇怪

时间:2014-12-11 08:42:57

标签: php

<?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”])){???或其他什么?

1 个答案:

答案 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();}