gpt4 book ai didi

php - 如何修复显示的 WordPress 自定义插件问题 - "Database Update Required"

转载 作者:搜寻专家 更新时间:2023-10-31 21:48:21 24 4
gpt4 key购买 nike

如何修复显示“需要更新数据库”的 WordPress 自定义插件问题 - 即使插件工作正常。我附上了错误/警告消息的图像。代码片段包括插件的所有源代码。我不知道如何删除错误/警告消息。

enter image description here

<?php
/**
* Plugin Name: Content Creator 2 plugin
* Plugin URI: http://www.nodomain.com
* Description: This plugin will create a simple HTML-form with 3 input fields and store the information into a stage table.
* Version: 1.0
* Author: Murat Kilic
* Author URI: http://www.nodomain.com
* License: GPLv3
**/

function insert_into_db() {
# This ($wpdb) is a global variable inside WordPress that gives me access to create new tables inside WordPress
global $wpdb;

#Create a new table inside my existing wp-database if table not exists
$table = $wpdb->prefix . "cc2testplugin";
#Setting the charset for the database
$charset_collate = $wpdb->get_charset_collate();
# Creating table with SQL
$sql = "CREATE TABLE IF NOT EXISTS $table (
`id` MEDIUMINT NOT NULL AUTO_INCREMENT,
`stagename` TEXT NOT NULL,
`capacity` MEDIUMINT NOT NULL,
`url` TEXT NOT NULL,
UNIQUE (`id`)
) $charset_collate;";

#Including upgrade.php file
require_once( ABSPATH . 'wp-admin/upgrade.php');

#Useful for creating new tables and updating existing tables to a new structure.
dbDelta ($sql);

#Starts output buffering
ob_start();

?>

<!-- Here I will create my HTML_form with 3 input-fields -->
<form action="#v_form" method="post" id="v_form">
<label for="stagename">
<h3>Name of stage:</h3>
</label>
<input type="text" name="stagename" id="stagename">

<label for="capacity">
<h3>Max capacity of stage:</h3>
</label>
<input type="number" min="1000" max="50000" name="capacity" id="capacity">

<label for="url">
<h3>Type MAP location (url):</h3>
</label>
<input type="url" name="url" id="url">
<input type="submit" value="submit" name="submit_form">
</form>
<!-- END HTML-form -->

<?php
#Gets the current buffer contents and delete current output buffer
$html = ob_get_clean();
# Check if the user has clicked on the submit button
if (isset ($_POST["submit_form"]) ) {
#Setting table prefix
$table = $wpdb->prefix . "cc2testplugin";
#Collecting data from input fields and store it in variables
$stagename = strip_tags($_POST["stagename"], "");
$capacity = strip_tags($_POST["capacity"], "");
$url = $_POST["url"];

#Inserting data from the HTML-form based on the variables into the table
$wpdb->insert(
$table,
array(
'stagename' => $stagename,
'capacity' => $capacity,
'url' => $url
)
);
#Writing out a message after having inserted the data from the HTML-Form
$html = "<p>The stage with following name <strong>$stagename</strong> was successfully inserted! Thanks.</p>";
}
# Displays /outputs text on screen
return $html;
}
#Adding a wordpress shortcode that can be used on either a wordpress page or post
add_shortcode('cc2testplugin','insert_into_db');
?>

最佳答案

我在这里看到的问题是,您在每次运行带有短代码的页面时都在检查是否创建表。当您创建表格时,您包含了 upgrade.php,它将显示您描述的消息。

您需要做的是断开创建表的部分(包括对 upgrade.php 的调用)并将其添加到您的激活插件部分。您应该只需要检查并创建该表一次。不要忘记在卸载插件时删除表格(以及您添加的任何其他内容)。

在这里阅读更多相关信息: https://codex.wordpress.org/Creating_Tables_with_Plugins

关于php - 如何修复显示的 WordPress 自定义插件问题 - "Database Update Required",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50530103/

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