1

There's no PDO error,and the form can get the value correctly,but I never see any data inserted into the table.

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

//the form html code.

<?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);
        }
    }
}

//admin . php is the web page I enter initially.

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

If I submit some string for test,the network of chrome just show what I submit,and these's no error report(maybe I am wrong).There is just no data changed in the database,if I go to the corresponding table,it shows that" MySQL returned an empty result set (i.e. zero rows). " one by one.

Today's log:

[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.

Well,if I change the method of the form from 'post' into 'get',and change the variable from '$_POST' into '$_GET',it runs well and I can see the data inserted in the database correctly,but why the get method is ok while the post isn't?

Frozenowl
  • 11
  • 2
  • 1
    Make sure [errors are being displayed](https://stackoverflow.com/questions/845021/how-to-get-useful-error-messages-in-php) or check the error log file to see if anything is showing up there. – Mike Sep 11 '18 at 02:44
  • Is there a reason you're not using parameter binding in your prepared statement? – Phil Sep 11 '18 at 04:04

1 Answers1

0

Well,after I got the source codes from that book,I know the reason.

It's because I wrote a wrong 'method',I've wrote 'method' as 'methond'.

//the form html code.

<?php
return"
<form methond='post' action='admin.php?page=editor' id='editor'>

That's all.It's all my fault.

Frozenowl
  • 11
  • 2