[英]Data does not enter into mysql database through WordPress form using php
[英]Add a new row in mysql database through wordpress using php
我在這方面很新,我在使用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', 'x{pM!iVyNHWvYj$YYeeK-[UJLZDDrt%frd{Tj- 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@T{Rf}!rZXl}RdlaF?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)INP}jgNy^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;glYstXeI{YVR=?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>
嘗試跟進下面的代碼,它會將日期插入數據庫,檢查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>
感謝@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', '');
就是這樣。 代碼是正確的!
謝謝!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.