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

时间:2016-04-05 14:54:57

标签: php mysql blogs

我正在尝试创建一个简单的博客条目表单,用户输入标题,博客条目并提交。然后,表单应使用插入查询将“博客条目”插入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的新手,我不知道如何在没有错误时调试问题!

2 个答案:

答案 0 :(得分:1)

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

我还在您的type="submit"按钮

中添加了Enviar
<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 -->

答案 1 :(得分:0)

使用method = post标记上的<form>解决了问题 所以,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>