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.

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

报告相同问题?

悬赏问题

  • ¥50 关于#html5#的问题:H5页面用户手机返回的时候跳转到指定页面例如(语言-javascript)
  • ¥15 无法使用此凭据登录,因为你的域不可用,如何解决?(标签-Windows)
  • ¥15 yolov9的训练时间
  • ¥15 二叉树遍历没有报错但无法正常运行
  • ¥15 在linux系统下vscode运行robocup3d上场球员报错
  • ¥15 Python语言实验
  • ¥15 SAP HANA SQL 增加合计行
  • ¥20 用C#语言解决一个英文打字练习器,有偿
  • ¥15 srs-sip外部服务 webrtc支持H265格式
  • ¥15 在使用abaqus软件中,继承到assembly里的surfaces怎么使用python批量调动