gpt4 book ai didi

php - WordPress 数据库数组不传递数据

转载 作者:行者123 更新时间:2023-11-29 18:15:11 24 4
gpt4 key购买 nike

我正在尝试创建一个表单,允许某人将一行信息保存到 wpdb 中。由于某种原因,当我提交表单时,表中没有存储任何内容。我从表单中删除了所有内容,我可以提交“名称”值并将其存储在数据库中,但是一旦我向表单添加更多字段并尝试将数据添加到数组中,什么也不会发生。这是我到目前为止所拥有的。

function elh_insert_into_db() {

global $wpdb;
// creates safety_users in database if not exists
$table = $wpdb->prefix . "safety_users";
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE IF NOT EXISTS $table (
`id` mediumint(9) NOT NULL AUTO_INCREMENT,
`name` text NOT NULL,
UNIQUE (`id`)
) $charset_collate;";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $sql );
// starts output buffering
ob_start();
?>
<form action="#v_form" method="post" id="v_form">
<label for="first_name"><h3>First Name</h3></label>
<input type="text" name="first_name" id="first_name" />
<label for="last_name"><h3>Last Name</h3></label>
<input type="text" name="last_name" id="last_name" />
<input type="submit" name="submit_form" value="submit" />
</form>
<?php
$html = ob_get_clean();
// does the inserting, in case the form is filled and submitted
if ( isset( $_POST["submit_form"] ) && $_POST["first_name"] != "" ) {
$table = $wpdb->prefix."safety_users";
$name = ($_POST["first_name"]);
$lname = ($_POST["last_name"]);
$wpdb->insert(
$table,
array(
'name' => $name,
'lname' => $lname
)
);
$html = "<p>Your name <strong>$name</strong> was successfully recorded. Thanks!!</p>";
}
// if the form is submitted but the name is empty
if ( isset( $_POST["submit_form"] ) && $_POST["first_name"] == "" )
$html .= "<p>You need to fill the required fields.</p>";
// outputs everything
return $html;

}
// adds a shortcode you can use: [insert-into-db]
add_shortcode('elh-db-insert', 'elh_insert_into_db');

如果有人发现我的代码有任何错误,请告诉我!

最佳答案

尝试:

<form action="index.php" method="POST" id="v_form">

而不是:

<form action="#v_form" method="post" id="v_form">

index.php 替换为您的 PHP 文件的名称。

编辑:似乎有错误。

$name = s($_POST["first_name"]);

删除($_POST之前的“s”

关于php - WordPress 数据库数组不传递数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47087308/

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