PHP 博客条目未提交到 MySQL 数据库

PHP blog entry is not submitting into MySQL database

我正在尝试创建一个简单的博客条目表单,用户可以在其中输入标题、博客条目并提交。然后表单应使用插入查询将 'blog entry' 插入 MYSQL。

我没有收到任何错误。 当我提交表单时没有任何改变,数据库没有新条目。

<?php

session_start();

date_default_timezone_set('America/Mexico_City');

if (!isset($_SESSION['usuario_usuario'])) {

    header("Location: login");

} else {

    include_once 'config.php';

    $guardar_post = $conn -> prepare("INSERT into entries (post_title, post_content, created_at, updated_at) VALUES (:titulo_post, :contenido_post, :created_at, :updated_at);");
    $guardar_post ->bindParam(":titulo_post", $titulo_post);
    $guardar_post ->bindParam(":contenido_post", $contenido_post);
    $guardar_post ->bindParam(":created_at", $created_at);
    $guardar_post ->bindParam(":updated_at", $updated_at);

    if (isset($_POST['enviar'])) {
        $titulo_post = $_POST['titulo'];
        $contenido_post = $_POST['editor1'];
        $created_at = date("Y-m-d H:i:s");
        $updated_at = date("Y-m-d H:i:s");

        $guardar_post -> execute();
    }
}

?>

<!DOCTYPE html>
<html lang="en">

<head>

<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">

<title>SB Admin 2 - Bootstrap Admin Theme</title>

<!-- Bootstrap Core CSS -->
<link href=" bower_components/bootstrap/dist/css/bootstrap.min.css" rel="stylesheet">

<!-- MetisMenu CSS -->
<link href=" bower_components/metisMenu/dist/metisMenu.min.css" rel="stylesheet">

<!-- Custom CSS -->
<link href=" dist/css/sb-admin-2.css" rel="stylesheet">

<!-- Custom Fonts -->
<link href=" bower_components/font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css">

<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->

<!-- CKEditor -->
<script src="ckeditor/ckeditor.js"></script>

</head>

<body>

<div id="wrapper">
<!-- Navigation -->
<?php include_once 'nav.php'; ?>


<!-- Page Content -->
<div id="page-wrapper">
    <div class="container-fluid">
        <div class="row">
            <div class="col-lg-12">
                <h1 class="page-header">Nuevo Post</h1>
            </div>
            <!-- /.col-lg-12 -->
        </div>
        <!-- /.row -->
        <div class="row">
            <div class="col-lg-12">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        Redactar nuevo post
                    </div>
                    <div class="panel-body">
                        <div class="row">
                            <div class="col-lg-10">
                                <form role="form">
                                    <div class="form-group">
                                        <label>Título</label>
                                        <input class="form-control" name="titulo">
                                        <p class="help-block">Entre el titulo del post.</p>
                                    </div>
                                </form>
                            </div>
                            <div class="col-lg-10">
                                <div class="form-group">
                                    <label>Text area</label>
                                    <textarea name="editor1" id="editor1" class="form-control" rows="15"></textarea>
                                </div>
                            </div>


                        </div>
                        <div class="row">
                            <div class=" col-lg-5">
                                <p>
                                    <button type="button" class="btn btn-outline btn-success" name="enviar">Enviar</button>
                                    <button type="button" class="btn btn-outline btn-danger">Borrar</button>

                                </p>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <!-- /.container-fluid -->
    </div>
    <!-- /#page-wrapper -->

</div>
<!-- /#wrapper -->

<!-- jQuery -->
<script src=" bower_components/jquery/dist/jquery.min.js"></script>

<!-- Bootstrap Core JavaScript -->
<script src=" bower_components/bootstrap/dist/js/bootstrap.min.js"></script>

<!-- Metis Menu Plugin JavaScript -->
<script src=" bower_components/metisMenu/dist/metisMenu.min.js"></script>

<!-- Custom Theme JavaScript -->
<script src=" dist/js/sb-admin-2.js"></script>

<!-- Replace the <textarea id="editor1"> with a CKEditor -->
<script>CKEDITOR.replace( 'editor1' );</script>

config.php 文件是:

<?php

$base = "mysql:host=localhost;dbname=bworld";

try {
    $conn = new PDO($base, 'diego', 'diego');
} catch (PDOException $e) { echo $e; }

有谁知道我做错了什么。我是 PHP 的新手,我不知道如何在没有错误的情况下调试问题!

您必须将所有字段放在 <form> 标签内,包括您的提交按钮

我还向你的 Enviar 按钮添加了 type="submit"

<div id="wrapper">
<!-- Navigation -->
<?php include_once 'nav.php'; ?>

<!-- Page Content -->
<div id="page-wrapper">
    <div class="container-fluid">
        <div class="row">
            <div class="col-lg-12">
                <h1 class="page-header">Nuevo Post</h1>
            </div>
            <!-- /.col-lg-12 -->
        </div>
        <!-- /.row -->
        <div class="row">
            <div class="col-lg-12">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        Redactar nuevo post
                    </div>
                    <div class="panel-body">
                        <form role="form">
                            <div class="row">
                                <div class="col-lg-10">                                
                                    <div class="form-group">
                                        <label>Título</label>
                                        <input class="form-control" name="titulo">
                                        <p class="help-block">Entre el titulo del post.</p>
                                    </div>
                                </div>
                                <div class="col-lg-10">
                                    <div class="form-group">
                                        <label>Text area</label>
                                        <textarea name="editor1" id="editor1" class="form-control" rows="15"></textarea>
                                    </div>
                                </div>
                            </div>
                            <div class="row">
                                <div class=" col-lg-5">
                                    <p>
                                        <button type="button" class="btn btn-outline btn-success" name="enviar" type="submit">Enviar</button>
                                        <button type="button" class="btn btn-outline btn-danger">Borrar</button>
                                    </p>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
        <!-- /.container-fluid -->
    </div>
    <!-- /#page-wrapper -->
</div>
<!-- /#wrapper -->

已通过 <form> 标签上的 method = post 解决
所以,sef4eg 是对的。

代码如下:

<div class="panel-body">
  <form role="form" action="" method="post">
    <div class="row">
        <div class="col-lg-10">
            <div class="form-group">
                <label for="titulo">Título</label>
                <input id="titulo" class="form-control" name="titulo">
                <p class="help-block">Entre el titulo del post.</p>
            </div>
        </div>
        <div class="col-lg-10">
            <div class="form-group">
                <label for="editor1">Text area</label>
                <textarea name="editor1" id="editor1" class="form-control" rows="15"></textarea>
            </div>
        </div>
    </div>
    <div class="row">
        <div class=" col-lg-5">
            <p>
                <button type="submit" class="btn btn-success btn-success" name="enviar">Enviar</button>
                <button type="button" class="btn btn-outline btn-danger">Borrar</button>
            </p>
        </div>
    </div>

  </form>
</div>