如何将数据从HTML表单保存到WordPress中的数据库表? [英] How to save data from an HTML form to a database table in WordPress?
问题描述
我有一个WordPress主题,我试图将一个HTML表单中的数据保存到数据库中。
我创建了HTML表单并添加了save & close按钮,该函数调用名为 saveData()
的JavaScript函数,该函数接收表单中的数据并将其发送到 addrow.php
,它应该将数据保存到名为 vel
的数据库表中。
我认为问题是在 addrow.php
中,因为在WordPress中,需要使用全局 $ wpdb
>
对于如何将数据从HTML表单保存到WordPress驱动的应用程序中的数据库表中,一个简单的例子是什么?
addrow.php
代码:
< ;? php
require(phpsqlinfo_dbinfo.php);
//从URL参数获取数据
$ nombre = $ _GET ['nombre'];
$ direccion = $ _GET ['direccion'];
$ lat = $ _GET ['lat'];
$ lng = $ _GET ['lng'];
$ tipo = $ _GET ['tipo'];
//打开与MySQL服务器的连接
$ connection = mysql_connect(localhost,$ username,$ password);
if(!$ connection){
die('Not connected:'。mysql_error());
}
//设置活动的MySQL数据库
$ db_selected = mysql_select_db($ database,$ connection);
if(!$ db_selected){
die('Can\'t use db:'。mysql_error());
}
//用用户数据插入新行
$ 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());
}
?>
为了将数据插入到数据库表中,最好使用 $ wpdb
。 WordPress Codex 可为您提供示例和更多信息,以帮助您继续操作。
例如,要将新记录插入数据库表,您可以执行此操作(从上面链接的页面):
$ wpdb-> insert('table',array('column1'=>'value1','column2'=> 123),array('%s' '%d'))
如果您发布其他代码(例如, addrow.php
目前尝试保存数据?),我们可能能够提供更具体的信息。
I have a WordPress theme, and I'm trying to save data from an HTML form into a database.
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
.
I think the problem is in addrow.php
because in WordPress, one needs to use the global $wpdb
or some other thing.
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?
The addrow.php
code:
<?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());
}
?>
You are correct; in order to insert data into a database table, it is a best practice to use $wpdb
. The WordPress Codex can provide you with examples and more information to help you proceed.
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.php
currently try to save the data?), we might be able to provide more specific information.
这篇关于如何将数据从HTML表单保存到WordPress中的数据库表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!