使用PHP将html表单提交到SQL,获取ERROR 500

时间:2017-04-10 04:03:45

标签: php html sql

我有一个基本的html表单如下:

<form class="blogPost" action="../php/action_page.php" method="post">
  <fieldset>
    <legend>Type your blog post</legend>

    <br>Title <br>
    <input name="title" type="text" placeholder="Witty Title?">
</br>
     <br>Post <br>
    <input name="post" type="text" placeholder="Write something clever!">
  </br>

  </fieldset>
<br>
  <input type="submit" name="submit" value="Submit">
</form>

然而,当我通过以下PHP脚本传递它时,我在浏览器中得到500错误。 任何帮助将不胜感激。

<?php
include 'dbconfig.php';


$title = $_POST["title"];
$post = $_POST["post"];


try {
$conn = new PDO("mysql:host=$host; dbname=$dbname", $user, $pass);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$sql = "INSERT INTO blog_posts (title, post)
        VALUES ('$title', '$post')";

$conn->exec($sql);

            echo "New record created successfully";
}

catch (PDOException $pe) {
  echo "Couldn't connect to the database $dbname: " . $pe->getMessage() .
      ". </br>Record NOT created.";
}

$conn = null;
 ?>

这是关注w3schools.com上发布的PDO示例

0 个答案:

没有答案