没有PDO错误,表单也能正确取值,但一直没看到有数据插入到表中。
<?php
//complete code for controllers/admin/editor.php
//include class definition and create an object
include_once "models/Blog_Entry_Table.class.php";
$entryTable = new Blog_Entry_Table($db);
//was editor form submitted?
$editorSubmitted = isset($_POST['action']);
if ($editorSubmitted) {
$buttonClicked = $_POST['action'];
//was "save" button clicked
$insertNewEntry = ($buttonClicked === 'save');
if ($insertNewEntry) {
//get title and entry data from editor form
$title = $_POST['title'];
$entry = $_POST['entry'];
//save the new entry
$entryTable->saveEntry($title, $entry);
}
}
//load relevant view
$editorOutput = include_once "views/admin/editor-html.php";
return $editorOutput;
//表单html代码。
<?php
return"
<form methond='post' action='admin.php?page=editor' id='editor'>
<fieldset>
<legend>New Entry Submission</legend>
<label>Title</label>
<input type='text' name='title' maxlength='150' />
<label>Entry</label>
<textarea name='entry'></textarea>
<fieldset id='editor-buttons'>
<input type='submit' name='action' value='save' />
<input type='submit' name='action' value='delete' />
</fieldset>
</fieldset>
</form>
";
<?php
//complete code listing for models/Blog_Entry_Table.class.php
class Blog_Entry_Table{
private $db;
//notice there are two underscore characters in __construct
public function __construct($db){
$this->db = $db;
}
public function saveEntry($title, $entry){
$entrySQL = "INSERT INTO blog_entry ( title, entry_text )
VALUES ( '$title', '$entry' )";
$entryStatement = $this->db->prepare($entrySQL);
try {
$entryStatement->execute();
}
catch (Exception $e) {
$msg = "<p>You tried to run this sql: $entrySQL<p>
<p>Exception: $e</p>";
trigger_error($msg);
}
}
}
//管理员。 php是我最初进入的网页。
<?php
error_reporting(E_ALL);
ini_set("display_errors",1);
include_once "models/Page_Data.class.php";
$pageData=new Page_Data();
$pageData->title="PHP/MySQL blog demo";
$pageData->addCSS("css/blog.css");
//$pageData->content="<h1>YES!</h1>";
$pageData->content=include_once "views/admin/admin-navigation.php";
$dbInfo="mysql:host=localhost;dbname=simple_blog";
$dbUser="root";
$dbPassword="root";
$db=new PDO($dbInfo,$dbUser,$dbPassword);
$db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
$navigationIsClicked=isset($_GET['page']);
if($navigationIsClicked){
$contrl=$_GET['page'];
}else{
$contrl="editor";
}
$pageData->content.=include_once "controllers/admin/$contrl.php";
$page=include_once "views/page.php";
echo $page;
如果我提交一些字符串进行测试,chrome 的网络只显示我提交的内容,并且没有错误报告(也许我错了)。如果我转到相应的表,数据库中的数据没有更改,它显示“MySQL 返回了一个空结果集(即零行)。”一一显示。
今天的日志:
[Tue Sep 11 08:40:12.935566 2018] [core:warn] [pid 15448:tid 852] AH00098: pid file C:/phpStudy/PHPTutorial/Apache/logs/httpd.pid overwritten -- Unclean shutdown of previous Apache run?
[Tue Sep 11 08:40:13.061936 2018] [mpm_winnt:notice] [pid 15448:tid 852] AH00455: Apache/2.4.23 (Win32) OpenSSL/1.0.2j PHP/5.5.38 configured -- resuming normal operations
[Tue Sep 11 08:40:13.061936 2018] [mpm_winnt:notice] [pid 15448:tid 852] AH00456: Server built: Jul 1 2016 16:42:20
[Tue Sep 11 08:40:13.061936 2018] [core:notice] [pid 15448:tid 852] AH00094: Command line: 'C:\phpStudy\PHPTutorial\Apache\bin\httpd.exe -d C:/phpStudy/PHPTutorial/Apache'
[Tue Sep 11 08:40:13.568497 2018] [mpm_winnt:notice] [pid 15448:tid 852] AH00418: Parent: Created child process 21416
[Tue Sep 11 08:40:14.525578 2018] [mpm_winnt:notice] [pid 21416:tid 692] AH00354: Child: Starting 150 worker threads.
好吧,如果我将表单的方法从'post'更改为'get',并将变量从'$_POST'更改为'$_GET',它运行良好并且我可以看到插入数据库的数据正确,但为什么 get 方法可以,而 post 却不行?
最佳答案
嗯,拿到那本书的源代码后,我知道原因了。
因为我写错了'method',我把'method'写成了'methond'。
//the form html code.
<?php
return"
<form methond='post' action='admin.php?page=editor' id='editor'>
就是这样,都是我的错。
关于php - 用get方法插入是可以的,而post不是。(关于PHP/PDO/HTML),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52267876/