使用 wpdb 调用非对象上的成员函数 insert()


Call to a member function insert() on a non-object with wpdb

>当它调用 mysave.php 文件时,我收到错误调用带有 wpdb 的非对象上的成员函数 insert(),以及我如何获得在 WordPress 模板文件夹中使用 mysave.php 文件目前我需要将其放入根文件夹。

<style>
form#my > #phase2{ display:none; }
</style>
<script>
function _(x){
    return document.getElementById(x);
}
function processPhase1(){
        _("phase1").style.display = "none";
        _("phase2").style.display = "block";        
    } 
function submitForm(){  
            _("myform").method = "post";
            _("myform").action = "mysave.php";
            _("myform").submit();
        }
</script>
        <form id="myform" onsubmit="return false" > 
         <div id="phase1">
        <input type="text" id="inputbook" name="bookname" placeholder="book name" height="34px">
        <button onclick="processPhase1()"><strong>Next</strong></button>
        <div>
        <div id="phase2">
        <input type="text" id="inputcode" name="code" placeholder="code" height="34px">
        <button onclick="submitForm()"><strong>SUBMIT</strong></button>
    <div>
        </form>

我的保存.php

if(isset($_POST['bookname']))
{
    global $wpdb;
    $bookname=$_POST['bookname'];
    $code=$_POST['code'];
    $todaydate = new DateTime();
    $todaydate =$todaydate->format('Y-m-d H-i-s');
    $bookpost= array('post_type'=>'books',
                        'post_title'=>$bookname,
            'post_author'=>'1',
            'post_status'=>'publish',
            'post_date'=>$todaydate );
    $wpdb->insert('wp_posts',$bookpost); 
    $Cid=$wpdb->insert_id;
    update_field('code',$code,$Cid);
}

在 mysave.php 中创建一个模板。在页面上方编写此代码。

<?php
    /**
     * Template Name: Your template name
    */
?>

然后转到您的WP管理面板创建新页面并从下拉列表中选择您的模板。