How to pass username into form that sends data to database

Acidon asked
php database mysql forms
via

I am trying to pass username to database through form:

[insert_php]
$current_user= wp_get_current_user();
$id= $current_user->user_login; 
[/insert_php]


<input type="hidden" name="id" value="[insert_php]$id[/insert_php]">

But it doesn’t get through, the rest of visible inputs do go through though.


Answer
via

If you want to output the $id, you need to echo.
Also be sure to use esc_attr() as a good security practice.

<input type="hidden" name="id" value="[insert_php]echo esc_attr( $id );[/insert_php]">

In addition, as was noted in another answer, you’re calling it an ‘id’, but you’re actually using the login (username). If all you need is the numeric ID, use get_current_user_id().

<input type="hidden" name="id" value="[insert_php]echo esc_attr( get_current_user_id() );[/insert_php]">
Share This
Posted in: