2013-10-09 66 views
0

*问题解决了*查看答案在我的后下方

我要疯了试图从$ _ POST一个简单的HTML表单数据插入到PHP和PDO的数据库。目前我没有收到任何错误,但没有任何内容会进入数据库。如果我在代码中手动输入值,但在输入html表单时没有任何反应。在某个时候,我输入了“Array”。

如果有人能告诉我我做错了什么,或者指出我朝着正确的方向,我会非常感激!我是初学者所以请裸跟我:)

UPDATE: 我得到的错误是: 致命错误:未捕获的异常“PDOException”有消息“SQLSTATE [23000]:完整性约束违规:1048列'标题'不能为空'in ...

为什么列标题为空?

数据库仅仅是一个4个字段(ID,标题,消息和时间/时间戳字段表。 id字段是主AI和时间戳字段被自动拾取的时间。

这里是connect.inc.php文件:

<?php 
class DB extends PDO 
{ 
    public function __construct($dbname = "blogdata") 
    { 
     try { 
      parent::__construct("mysql:host=localhost;dbname=$dbname;charset=utf8", 
           "root", ""); 
     } catch (Exception $e) { 
      var_dump($e); 
     } 
    } 
} 

?> 

这里是post.php中的文件:

<?php 

require 'connect.inc.php'; 

$db = new DB('blogdata'); 

$stmt = $db->prepare("INSERT INTO blogposts (title, message, time) VALUES (:title, :message, :time)"); 

$stmt->bindParam(':title', $_POST['title']); 
$stmt->bindParam(':message', $_POST['message']); 
$stmt->bindParam(':time', $time); 

$title = $_POST['title']; 
$message = $_POST['message']; 

$stmt->execute(); 

?> 


<!DOCTYPE html> 

<html> 
<head> 
<title>Create blog post</title> 
<meta charset="utf-8" /> 

</head> 

<body> 

<!--- Add blog post ---> 

<div class="add_form"> 
    <form id="add_post" method="post" action="index.php" enctype="text/plain"> 
     <fieldset> 
      <legend>Create post</legend> 
      <label for="post_title">Title: 
       <input id="title" type="text" name="title" value="<?php if (isset($title)) { echo htmlentities ($title); } ?>" > 
      </label> 
      <label for="message">Message: 
       <textarea id="message" name="message" rows="20" cols="30" maxlength="50" value="<?php if (isset($message)) { echo htmlentities ($message); } ?>" ></textarea> 
      </label>           
     </fieldset> 
     <input id="send" type="submit" value="Send"> 
    </form> 
</div> 


</body> 

</html> 

回答

您需要将所有东西都包起来,并检查$ _POST是否为空。此外,问题是在表单中的action =“index.php”。它需要设置为post.php。

这里是post.php中正确的代码:

<?php 

if (!empty($_POST)) { 

    require 'connect.inc.php'; 

    $db = new DB('blogdata'); 

    $stmt = $db->prepare("INSERT INTO blogposts (title, message) VALUES (:title, :message)"); 
    $stmt->bindParam(':title', $_POST['title']); 
    $stmt->bindParam(':message', $_POST['message']); 

    $title = $_POST['title']; 
    $message = $_POST['message']; 

    $stmt->execute(); 

    header ('Location: index.php'); 
    exit; 
} 
?> 

<!DOCTYPE html> 

<html> 
<head> 
<title>Create blog post</title> 
<meta charset="utf-8" /> 
<link rel="stylesheet" href="reset.css" /> 
<link rel="stylesheet" href="style.css" /> 

</head> 

<body> 

<!--- Add blog post ---> 

<div class="add_form"> 
    <form id="add_post" method="post" action="post.php"> 
     <fieldset> 
      <legend>Create post</legend> 
      <label for="post_title">Title: 
       <input id="title" type="text" name="title" value="<?php if (isset($title)) { echo htmlentities ($title); } ?>" > 
      </label> 
      <label for="message">Message: 
       <textarea id="message" name="message" rows="20" cols="30" maxlength="50" value="<?php if (isset($message)) { echo htmlentities ($message); } ?>" ></textarea> 
      </label>           
     </fieldset> 
     <input id="send" type="submit" value="Send"> 
    </form> 
</div> 


</body> 

</html> 
+0

将所有的东西,看看你发现任何错误。 – aynber

+1

@aynber让你知道,例外完全可见没有任何catch块 –

+0

大多数情况下,如果你知道在哪里看:-) catch块让你输出它,如果有错误,你想要的。 – aynber

回答

0

Awser你最后的评论:

@Corum Hi, I´m calling index.php in the form because index.php are displaying all the posts and after sending the form the user should be directed to index.php and be able to see the results... I really can´t seem to solve this:(

如果调用<form action="index.php">您的表单数据的index.php将永远不会被处理。

您必须改为拨打post.php,然后重定向到index.php。

修正码(在你的文件post.php更换顶PHP块):

<?php 
if (!empty($_POST) { 
    // Only process if form is submitted (when page is launched, it use GET method) 
    require 'connect.inc.php'; 

    $db = new DB('blogdata'); 

    $stmt = $db->prepare("INSERT INTO blogposts (title, message, time) VALUES (:title, :message, :time)"); 

    $stmt->bindParam(':title', $_POST['title']); 
    $stmt->bindParam(':message', $_POST['message']); 
    $stmt->bindParam(':time', $time); 

    $title = $_POST['title']; 
    $message = $_POST['message']; 

    $stmt->execute(); 

    // Redirect to index.php 
    header('Location : index.php'); 
    exit; 
} 
?> 

,改变你的HTML表单:<form action="post.php" ...>

在try/catch块
+0

好吧,这拿走了当前的错误,但问题仍然是相同的,数据将不会进入数据库,发送后的形式它只是显示post.php没有错误没有什么... – Lisa

+0

删除'enctype =“text/plain” ',添加'var_dump($ _ POST); die();'紧跟在'if(!empty($ _ POST){'并告诉我们你得到的结果。 – Elorfin

+0

我得到这个:array(2){[“title”] => string(5)“super”[ “message”] => string(11)“bhbhbgkbgkj”} – Lisa

1

这里是正确 connect.inc.php文件:

<?php 
class DB extends PDO 
{ 
    public function __construct($dbname = "blogdata") 
    { 
     $opt = array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION); 
     $dsn = "mysql:host=localhost;dbname=$dbname;charset=utf8"; 
     parent::__construct($dsn, "root", "", $opt); 
    } 
} 

说不上来,如果它造成的错误,但

And here is the post.php file:
<form id="add_post" method="post" action="index.php"

无论如何,问题的真正原因与此类似。有些傻错字某处

+0

嗨!我只是改变了这一点,并收到此错误:致命错误:带有消息'SQLSTATE [23000]的未捕获异常'PDOException':完整性约束违规:1048'标题'列不能为空'在 – Lisa

+0

看来,所有的字段中的值都为零......我认为它与ID字段和AI有关,所以我尝试添加:$ newId = $ db-> lastInsertId(); – Lisa

+0

这就是你正在寻找什么,现在你可以自己阅读和理解这个错误,或者谷歌(不是有意义的部分从“完整性”开始) –