Reputation: 123
Im pretty new in this and I have some problems with wordpress.
Im trying to insert some information to mysql database. So for that Im doing the following:
First I created a php file in wp-content/plugins/my-codes
and the file name is insertUser.php where I added the following code:
<?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' ) );
?>
Then I created a basic page with the following:
<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>
Now the problem is that when I fill up the form and press the submit button is redirecting me to http://www.example.com/wp-content/plugins/my-codes/insertUser.php
and not insterting any row in the database.
Any ideas of what Im doing wrong?
Thanks!
EDIT
<?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,
));
}
?>
EDIT 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');
CODE EDIT
<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>
Upvotes: 3
Views: 701
Reputation: 123
Thanks to @Naresh Kumar.P for the help. Its necessary to change the config file with the credentials of the database to make the code work.
// ** 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', '');
And that's it. The code is correct!
Thanks!
Upvotes: 1
Reputation: 4210
Try to follow up the code below and it will insert the date into the database check whether the wp-config.php is three folders away alone others all you can copy an paste from here.
I have done the insert coding in the same page and you can copy paste the code for insert alone into the page you like.
<?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>
Upvotes: 1