$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>php - Unable to insert current username into custom table through html form|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

php - Unable to insert current username into custom table through html form

matteradmin6PV0评论

I have a custom html form from which I am trying to insert records into a custom table .My problem is getting the current user name and inserting that value into the custom table. When the user clicks the Submit button the code below should fire. This is my php code:

<?php>
session_start;
require_once "wp-load.php";
require_once "dbconfig.php";
global $wpdb, $current_user;
$current_user=wp_get_current_user();
$table_name="persons";
$wpdb->insert( $table_name, array(
              'first_name' => $_POST['first_name'],
              'last_name'  => $_POST['last_name'],
               'email'     => $_POST['email'],
               'telephone' => $_POST['telephone'],
               'user_name' => [$current_user]
               )
          );
?>

Any help is greatly appreciated. Been working on this for hours now, unable to find the solution.

Thanks in advance.

I have a custom html form from which I am trying to insert records into a custom table .My problem is getting the current user name and inserting that value into the custom table. When the user clicks the Submit button the code below should fire. This is my php code:

<?php>
session_start;
require_once "wp-load.php";
require_once "dbconfig.php";
global $wpdb, $current_user;
$current_user=wp_get_current_user();
$table_name="persons";
$wpdb->insert( $table_name, array(
              'first_name' => $_POST['first_name'],
              'last_name'  => $_POST['last_name'],
               'email'     => $_POST['email'],
               'telephone' => $_POST['telephone'],
               'user_name' => [$current_user]
               )
          );
?>

Any help is greatly appreciated. Been working on this for hours now, unable to find the solution.

Thanks in advance.

Share Improve this question asked Mar 18, 2019 at 13:39 David D'LimaDavid D'Lima 33 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

wp_get_current_user returns a WP_User object. You'll need to pull the name out of that in order to send it try $current_user->display_name in your insert statement. On that note, you should absolutely not be inserting unsanitized user entered data into your database. Please look into SQL injection and input sanitization.

The following is the code to insert user name into a custom table:

$current_user=wp_get_current_user();
$current_username = $current_user->user_login;
Post a comment

comment list (0)

  1. No comments so far