Php 在wordpress数据库中插入两行

Php 在wordpress数据库中插入两行,php,mysql,sql,wordpress,Php,Mysql,Sql,Wordpress,您好,我正在开发一个模板,在这个模板中,我必须获取一些值,并将它们插入到wordpress数据库中我自己创建的表中。 现在,由于我不知道如何在wordpress数据库中插入记录,所以我正在本地服务器上进行尝试。 我的temlate文件是: <?php /** Template Name: Injection. */ get_header(); ?> <form method="post" action=""> Album: <input type="text

您好,我正在开发一个模板,在这个模板中,我必须获取一些值,并将它们插入到wordpress数据库中我自己创建的表中。 现在,由于我不知道如何在wordpress数据库中插入记录,所以我正在本地服务器上进行尝试。 我的temlate文件是:

<?php
/**
   Template Name: Injection.
 */
get_header();
?>
<form method="post" action="">
Album: <input type="text" name="album" />
Artist: <input type="text" name="artist" />
<input type="submit" name="submit"/>
</form>
<?php
if(isset($_POST['submit']))
{
require_once('../../../wp-load.php');
 global $wpdb

 $album=$_POST['album'];
 $artist=$_POST['artist'];

 $wpdb->insert( 'music', array( 'album' => $album, 'artist' => $artist ), array( '%s', '%s' ) )
}
?>
这里我有一个奇怪的问题,如果我不在这里使用$wbdb类,那么我的temlate文件数据显示在前端,但是当我使用$wpdb进行插入时,它在前端没有显示任何内容

那么,对于在mysql中插入数据应该怎么做,您有什么建议吗


谢谢

前端不显示任何内容。。我认为php有问题,要找出错误所在,请尝试以下方法:

编辑wp-config.php,使wp_DEBUG为真

define('WP_DEBUG', true);

现在,它应该会在首页上看到一些错误消息。

您没有添加;登录你的模板,试试下面的代码

    <?php
/**
   Template Name: Injection.
 */
get_header();
?>
<form method="post" action="">
Album: <input type="text" name="album" />
Artist: <input type="text" name="artist" />
<input type="submit" name="submit"/>
</form>
<?php
if(isset($_POST['submit']))
{
require_once('../../../wp-load.php');
 global $wpdb;

 $album=$_POST['album'];
 $artist=$_POST['artist'];

 $wpdb->insert( 'music', array( 'album' => $album, 'artist' => $artist ), array( '%s', '%s' ) );
}
?>

首先打开theme/function.php文件,并将此代码添加到function.php文件中。 首先添加hook_操作

 add_action('wpcf7_before_send_mail','save_to_db');
现在创建将数据存储到数据库中的函数

    function save_to_db($cf7) {

       global $wpdb;

       $name = $_POST["your-name"];
       $mail = $_POST["your-email"];
       $subject = $_POST["your-subject"];
       $phoneno = $_POST["your-phoneno"];
       $address = $_POST["your-address"];
       $message = $_POST["your-message"];

       $test = $wpdb->insert($wpdb->prefix.'contactus', 
        array(
          'name'          => 'test',
          'email'       => 'test@gmail.com',
          'subject'     => 'fsaf',
          'phone'       => '1234566230',
          'address'     => 'tes',
          'message'     => 'asdasdasd'            
        ),
        array(
          '%s',
          '%s',
          '%s',
          '%s',
          '%s',
          '%s'
        ) 
       ); 
       /*echo $wpdb->last_query;die;
      echo '<pre>'; print_r($test);*/
    }

事实上,我检查了很多次,页面本身并没有给出任何解析错误,但知道它在工作,我成功地将数据保存在表中。。。。谢谢你能给我添加一些上下文/解释吗?我已经添加了一些上下文。请检查一下。它可以在任何条件下工作,