dongqiuxu2270 2013-10-09 16:47
浏览 39
已采纳

INSERT与PDO $ _POST无法正常工作

SOLVED - ANSWER ADDED AT THE BOTTOM OF THE POST

Please can someone help me out as I can´t understand what the heck I am doing wrong. I have a html form with 2 fields "title" and "message". I´m trying to get this to go into the database with PDO and $_POST but I am just getting this error:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'title' cannot be null' in

I´m doing everything by the book but it´s not working and I going to throw my computer out the window soon. So please anyone have any idea what is wrong? Why is title turning "NULL"?

The database is a 4 column table (id, title, message and timestamp). The id field is primary and auto intent

ANY help is really appreciated!!! And I´m a beginner...

Here is post.php file:

<?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" />
<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" 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>

And here is the connect.inc.php file:

<?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);
    }
}

?>

ANSWER:

This issue was finally solved. The problem is you need to check if $_POST is empty or not. And right after the if (!empty($_POST)) { set the require 'connect.inc.php';. Also to minimize code do like Dave Just said, change :

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

To:

$stmt->execute(array(':title' => $_POST['title'], ':message' => $_POST['message']));

Here is the working code in 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->execute(array(':title' => $_POST['title'], ':message' => $_POST['message']));

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

  // Redirect to index.php
  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">
        <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>
  • 写回答

1条回答 默认 最新

  • duan_88598 2013-10-09 18:02
    关注

    If you haven't thrown the computer out the window already, you should've checked whether $_POST vars were set or not before passing them to a PDO execute statement.

    <?php 
        require 'connect.inc.php';
        if( isset($_POST['title'], $_POST['message']) ) {
            $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();
        }
    ?>
    

    PS

    I have no idea what $time would do there.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上