doudong2149
2016-04-05 14:54 阅读 36
已采纳

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

I am trying to create a simple blog entry form where a user enters the title, blog entry and submits it. The form should then insert the 'blog entry' into MYSQL using the insert query.

I am getting NO errors. When I submit form nothing is changed, the database has no new entry.

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

the config.php file is:

<?php

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

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

Does anybody know what I'm doing wrong. I am new to PHP and I have no idea how to debug a problem when I'm getting no errors!

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    douou9786 douou9786 2016-04-06 17:23

    It was resolved with the method = post on the <form> tag
    So, sef4eg was kind of right.

    Here is the code:

    <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>
    
    点赞 评论 复制链接分享
  • donglinxia1541 donglinxia1541 2016-04-05 15:14

    You have to put all your fields inside <form> tag including your submit button

    I also added type="submit" to your Enviar button

    <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 -->
    
    点赞 评论 复制链接分享

相关推荐