gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 11:36:53 29 4
gpt4 key购买 nike

我正在尝试创建一个简单的博客条目表单,用户可以在其中输入标题、博客条目并提交。然后,该表单应使用插入查询将“博客条目”插入到 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> 中标签包括您的提交按钮

我还添加了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 -->

关于PHP 博客条目未提交到 MySQL 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36429923/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com