使用php通过wordpress在mysql数据库中添加新行
Posted
技术标签:
【中文标题】使用php通过wordpress在mysql数据库中添加新行【英文标题】:Add a new row in mysql database through wordpress using php 【发布时间】:2016-12-21 12:25:22 【问题描述】:我在这方面很新,我对 wordpress 有一些问题。
我正在尝试向 mysql 数据库中插入一些信息。所以为此我做了以下事情:
首先我在wp-content/plugins/my-codes
创建了一个php文件,文件名为insertUser.php,我在其中添加了以下代码:
<?php
require_once('../../../wp-config.php');
global $wpdb;
$table_name = $wpdb->prefix . "Users";
$name=addslashes($_POST['sName']);
$email=addslashes($_POST['sEmail']);
$wpdb->insert( $table_name, array( '$name','$email' ) );
?>
然后我创建了一个包含以下内容的基本页面:
<form action="/wp-content/plugins/my-codes/insertUser.php" method="post">
Name: <input type="text" name="sName" />
Email: <input type="text" name="sEmail" />
<input type="submit">
</form>
现在的问题是,当我填写表格并按下提交按钮时,会将我重定向到 http://www.example.com/wp-content/plugins/my-codes/insertUser.php
,而不是在数据库中插入任何行。
对我做错了什么有什么想法吗?
谢谢!
编辑
<?php
include '../../../wp-config.php';
global $wpdb;
if(isset($_POST['submit']))
$name=addslashes($_POST['sName']);
$email=addslashes($_POST['sEmail']);
$wpdb->insert("Users",array(
"sName"=>$name,
"sEmail"=>$email,
));
?>
编辑 config.php
<?php
/**
* The base configuration for WordPress
*
* The wp-config.php creation script uses this file during the
* installation. You don't have to use the web site, you can
* copy this file to "wp-config.php" and fill in the values.
*
* This file contains the following configurations:
*
* * MySQL settings
* * Secret keys
* * Database table prefix
* * ABSPATH
*
* @link https://codex.wordpress.org/Editing_wp-config.php
*
* @package WordPress
*/
// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define('DB_NAME', 'foundint_wordpress3ed');
/** MySQL database username */
define('DB_USER', 'foundint_word3ed');
/** MySQL database password */
define('DB_PASSWORD', 'XgY9PuLOgk7e');
/** MySQL hostname */
define('DB_HOST', 'localhost');
/** Database Charset to use in creating database tables. */
define('DB_CHARSET', 'utf8');
/** The Database Collate type. Don't change this if in doubt. */
define('DB_COLLATE', '');
/**#@+
* Authentication Unique Keys and Salts.
*
* Change these to different unique phrases!
* You can generate these using the @link https://api.wordpress.org/secret- key/1.1/salt/ WordPress.org secret-key service
* You can change these at any point in time to invalidate all existing cookies. This will force all users to have to log in again.
*
* @since 2.6.0
*/
define('AUTH_KEY', 'w-+F%ar+H_KthUYw]O<GlY;LgKGe[(XY@auxk;ZYZD| [gx!%FAD)KkN/WJWccym+- s;%Pa=P_hIszZr%$Mtyu)ugwfbju<lYdMrXKOo_>!eZ/$Nf(vHNzlb@on%M>D');
define('SECURE_AUTH_KEY', 'xpM!iVyNHWvYj$YYeeK-[UJLZDDrt%frdTj- YKAN]=tmJv=WMMmUgIFrQV|-Jfm-dJnKxjsNs?tvZo/TRJswh|^EaQVa;b%RBeZL= <k@h+kvES$@a$M(sseVEfh');
define('LOGGED_IN_KEY', '<?d)Lu@[oh<Q&eVRnV+SiCPPk*HzP$(prfU;Co;wbc?PG+hJL/kJggiL[rxQph>ND(I|+k]uYyiosb^)HIm<J$SVe^WiE>VHl*?N+S|&IM]BN|MPE_&)%O(s$sZCMm@');
define('NONCE_KEY', 'eGS+xFleYc[v]m@Gzy>KY/wbj[/lKtQ_ViIo]licFeyi(hFfcachqJGHOlC@eZO@Mq/hOLBTdxi]&Sk@Om<Qojbe+(-<waasHK=VjY)+J=tBwejj;voHF];We?XPiH');
define('AUTH_SALT', '-XF|WkVGk[%gY!S;tBFI>Y[J;v|A>ZbJT)G!R=T)bbAdsXn?<VMzyTIQECve@TRf!rZXlRdlaF?fz>HFXvnOwtl-/I]-B(IlF+r+[UuKrkQ[<GCcs<tp+umtZXzZ');
define('SECURE_AUTH_SALT', ']&Vlqy<wpAB_MUh!$RnjnTvH&jX$goRnZ)XlULL;zyxQcv*;dX=c=^>VsJBG<kElTCtKsw%RSOFBngSqPU|uvy@/bS/c[[e)INPjgNy^BV_G!D_cK--qrP*bx*gq');
define('LOGGED_IN_SALT', 'xP*[_am/Pj?t@X-<%?EY)sx>cs!bt)!d^zXs=vVY<LJ%B)PjlGDHy+Oi!iGIDv!A_r;glYstXeIYVR=?PofnBllW?_<t@wE@Z[o?@UMBmMi-Fap>LEUj_;vZFdH');
define('NONCE_SALT', '_cPe>RB]Nq]j]aQfgWB@+$eLfYig@MGA&]nD&?feKpkCm;v-MkEi-Dt_BWXWNV+QAVbFexDYlhWlJemTTo/bvKpAyjWqso<)kY$FR^G$mwIyvt$P]<M@Dv(wXGyhe');
/**#@-*/
/**
* WordPress Database Table prefix.
*
* You can have multiple installations in one database if you give each
* a unique prefix. Only numbers, letters, and underscores please!
*/
$table_prefix = 'wp_nhgb_';
/**
* For developers: WordPress debugging mode.
*
* Change this to true to enable the display of notices during development.
* It is strongly recommended that plugin and theme developers use WP_DEBUG
* in their development environments.
*
* For information on other constants that can be used for debugging,
* visit the Codex.
*
* @link https://codex.wordpress.org/Debugging_in_WordPress
*/
define('WP_DEBUG', false);
/* That's all, stop editing! Happy blogging. */
/** Absolute path to the WordPress directory. */
if ( !defined('ABSPATH') )
define('ABSPATH', dirname(__FILE__) . '/');
/** Sets up WordPress vars and included files. */
require_once(ABSPATH . 'wp-settings.php');
代码编辑
<form action="/wp-content/plugins/my-codes/insertUser.php" method="post">
Name: <input type="text" name="sName" />
Email: <input type="text" name="sEmail" />
<input type="submit" name="submit">
</form>
【问题讨论】:
兄弟一个想法。如果按下提交按钮,您不会重定向您的页面,您只需将该页面包含在同一页面中。如果您这样做,它将起作用。 【参考方案1】:尝试跟进下面的代码,它会将日期插入数据库中,检查 wp-config.php 是否在三个文件夹之外,其他所有您都可以从这里复制粘贴。
我已经在同一页面中完成了插入编码,您可以将插入的代码单独复制粘贴到您喜欢的页面中。
<?php
include '../../../wp-config.php';
global $wpdb;
if(isset($_POST['submit']))
$name=addslashes($_POST['sName']);
$email=addslashes($_POST['sEmail']);
$wpdb->insert("test",array(
"sName"=>$name,
"sEmail"=>$email,
));
?>
<form action="" method="post">
Name: <input type="text" name="sName" />
Email: <input type="text" name="sEmail" />
<input type="submit" name="submit">
</form>
【讨论】:
一定有一些我遗漏的东西,因为我按照您的建议更改了代码(请参阅编辑),但我仍然无法在数据库中插入任何内容。我不知道... 我对 wordpress 很陌生,所以我可能忘记更改一些配置文件,或者添加插件之类的?有可能吗? 是的,我明白了。您能否更好地在按下按钮时在表单操作中包含您提供的页面。 这样您就可以在if(isset($_POST['submit'])) include ('required page')
上执行此操作,并且您可以将代码写入该页面本身。
那么,让我看看我是否理解。您是否建议将该行添加到页面中?作为php代码?并继续调用函数?【参考方案2】:
感谢@Naresh Kumar.P 的帮助。必须使用数据库的凭据更改配置文件才能使代码正常工作。
// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define('DB_NAME', 'yourDataBaseName');
/** MySQL database username */
define('DB_USER', 'YourDataBaseUserName');
/** MySQL database password */
define('DB_PASSWORD', 'YourDataBasePassword');
/** MySQL hostname */
define('DB_HOST', 'localhost');
/** Database Charset to use in creating database tables. */
define('DB_CHARSET', 'utf8');
/** The Database Collate type. Don't change this if in doubt. */
define('DB_COLLATE', '');
就是这样。代码是正确的!
谢谢!
【讨论】:
以上是关于使用php通过wordpress在mysql数据库中添加新行的主要内容,如果未能解决你的问题,请参考以下文章
如何通过 SSL 上的 PHP 连接到 Amazon MySQL RDS 实例