Javascript 如何将 HTML 表单中的数据保存到 WordPress 中的数据库表中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5291581/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 16:30:14  来源:igfitidea点击:

How to save data from an HTML form to a database table in WordPress?

phpjavascriptdatabasewordpressforms

提问by jcliff

I have a WordPress theme, and I'm trying to save data from an HTML form into a database.

我有一个 WordPress 主题,我正在尝试将数据从 HTML 表单保存到数据库中。

I made the HTML form and added a "save & close" button which calls a JavaScript function named saveData()that takes the data from the form and sends it to addrow.php, which should save the data into a database table named vel.

我制作了 HTML 表单并添加了一个“保存并关闭”按钮,该按钮调用一个名为 JavaScript 的函数saveData(),该函数从表单中获取数据并将其发送到addrow.php,该函数会将数据保存到名为vel.

I think the problem is in addrow.phpbecause in WordPress, one needs to use the global $wpdbor some other thing.

我认为问题在于,addrow.php因为在 WordPress 中,需要使用全局$wpdb或其他一些东西。

What would be a simple example be for how to save data from an HTML form into a database table in a WordPress-powered application?

一个简单的例子是如何将数据从 HTML 表单保存到 WordPress 驱动的应用程序中的数据库表中?

The addrow.phpcode:

addrow.php代码:

<?php
    require("phpsqlinfo_dbinfo.php");

    // Gets data from URL parameters
    $nombre = $_GET['nombre'];
    $direccion = $_GET['direccion'];
    $lat = $_GET['lat'];
    $lng = $_GET['lng'];
    $tipo = $_GET['tipo'];

    // Opens a connection to a MySQL server
    $connection = mysql_connect ("localhost", $username, $password);
    if (!$connection) {
        die('Not connected : ' . mysql_error());
    }

    // Set the active MySQL database
    $db_selected = mysql_select_db($database, $connection);
    if (!$db_selected) {
        die ('Can\'t use db : ' . mysql_error());
    }

    // Insert new row with user data
    $query = sprintf("INSERT INTO vel " .
    " (id, nombre, direccion, lat, lng, tipo ) " .
    " VALUES (NULL, '%s', '%s', '%s', '%s', '%s');",
    mysql_real_escape_string($nombre),
    mysql_real_escape_string($direccion),
    mysql_real_escape_string($lat),
    mysql_real_escape_string($lng),
    mysql_real_escape_string($tipo));

    $result = mysql_query($query);

    if (!$result) {
        die('Invalid query: ' . mysql_error());
    }
?>

回答by

You are correct; in order to insert data into a database table, it is a best practice to use $wpdb. The WordPress Codexcan provide you with examples and more information to help you proceed.

你是对的; 为了向数据库表中插入数据,最好使用$wpdb. WordPress Codex可以为您提供示例和更多信息以帮助您继续操作。

For example, to insert a new record into a database table, you can do this (from the above linked page):

例如,要将新记录插入数据库表,您可以这样做(从上面的链接页面):

$wpdb->insert( 'table', array( 'column1' => 'value1', 'column2' => 123 ), array( '%s', '%d' ) )

If you post additional code (e.g., how does addrow.phpcurrently try to save the data?), we might be able to provide more specific information.

如果您发布其他代码(例如,addrow.php当前如何尝试保存数据?),我们可能会提供更具体的信息。