2016-08-15 8 views
3

私はこれでかなり新しく、私はwordpressにいくつかの問題があります。PHPを使ってmysqlデータベースに新しい行を追加します。

私はいくつかの情報をmysqlデータベースに挿入しようとしています。だからImは以下を実行するために:

<?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')); 
?> 

でその後、私は基本的なページを作成しました:私は、次のコードを追加したところ

まず私はwp-content/plugins/my-codesでPHPファイルを作成し、ファイル名がinsertUser.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"> 
</form> 

今の問題は、私は、フォームを記入し、送信ボタンをhttp://www.example.com/wp-content/plugins/my-codes/insertUser.phpに私をリダイレクトし、データベース内の任意の行をinstertingされていない押したときということです。

何が間違っているのアイデアですか?

ありがとうございます!

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[([email protected];ZYZD| [}gx!%FAD)KkN}/WJWccym+- s;%Pa=P_hIszZr%$Mtyu)ugwfbju<lYdMrXKOo_>!eZ/$Nf([email protected]%M>D'); 
define('SECURE_AUTH_KEY', 'x{pM!iVyNHWvYj$YYeeK-[UJLZDDrt%frd{Tj- YKAN]=tmJv=WMMmUgIFrQV{|-Jfm-dJnKxjsNs?tvZo/}TRJswh|^EaQVa;b%RBeZL= <[email protected]+kvES${@a$M(sseVEfh'); 
define('LOGGED_IN_KEY', '<?d)[email protected][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([email protected]'); 
define('NONCE_KEY', 'eGS+xFleYc[v][email protected]>KY/wbj[/lKtQ_ViIo]licFeyi(}[email protected]@{Mq/hOLBTdxi]&[email protected]<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?<[email protected]{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}|[email protected]/bS/c[[e)INP}jgNy^BV_G!D_}cK--qrP*bx*gq'); 
define('LOGGED_IN_SALT', 'xP*[_am/}[email protected]{-<%?EY)sx>cs!bt)!d^zXs=vVY<LJ%B)PjlGDHy+Oi!iGIDv!}A_r;glYstXeI{YVR=?PofnBllW?_<[email protected]{[email protected][[email protected]>LEUj_;vZFdH'); 
define('NONCE_SALT', '_cPe>RB]Nq]j][email protected][email protected]&]nD&}?feKpkCm;v-MkEi-Dt_BWXWNV+QAVbFexDYlhWlJemTTo/bvKpAyjWqso<)kY}$}FR^G$mwIyvt$P]<[email protected](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> 
+0

1つのアイデア。送信ボタンを押すと、そのページを同じページに含めるだけであなたのページをリダイレクトすることはありません。あなたがそれを好きなら、それはうまくいくでしょう。 –

答えて

1

以下のコードに従ってください。データベースに日付が挿入されます.WP-config.phpが3つのフォルダから離れているかどうかを確認してください。

私は同じページにコードを挿入していますし、好きなページにコードを貼り付けてコピーすることもできます。

<?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> 
+0

あなたが提案した(編集を参照)ように私はコードを変更し、Imはまだデータベースに何も挿入することができないので、何かIm欠落している必要があります。私は知らない... – AlguienEnEsteMundo

+0

私は非常に新しいWordPressには、多分私はいくつかの設定ファイルを変更することを忘れて、またはプラグインや何かを追加する新しい?出来ますか? – AlguienEnEsteMundo

+0

わかりました。ボタンが押されたときにフォームアクションに与えたものを含む方が良いでしょうか。 –

1

@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', ''); 

これだけです。コードは正しいです!

ありがとうございます!

関連する問題