Get wp_current_user_id using PHP and MySQL

awesome people of the Stack Community!

Like the newbie coder I am, it took many hours but at last I got:

  • a HTML form inside wordpress ->
  • to submit value to MySQL database ->
  • and then store the values inside the table.

This page and form requires a login to use, so what I’m trying to do now is getting the wp_current_user_id stored in a field in the table “order” and the field userID.

Does anyone know if this is possible?

Thanks in advance for any kinds of help.
Cheers.

order_confirmation.php

<?php

   $db_name = "dbname";
   $db_user = "username";
   $db_pass = "cool_password";
   $db_host = "hostname";

    include 'config.php';

    $db_conn = connectToDB($db_host, $db_user, $db_pass, $db_name);

    //Get values from the form then -> database
    $sql = mysqli_query($db_conn, "
        INSERT INTO ordre(
            user_fname,
            user_lname,
            user_address, 
            user_email,
            user_phone, 
            service_chosen,
            user_note,
            order_date,
            statusID
        )
        VALUES(
            '".$_POST['user_fname']."',
            '".$_POST['user_lname']."',
            '".$_POST['user_address']."',
            '".$_POST['user_email']."',
            '".$_POST['user_phone']."',
            '".$_POST['service_chosen']."',
            '".$_POST['user_note']."',
            '".date('Y-m-d')."',
            '1' 
        )
    ");

    if($sql == TRUE) {
        echo "<p>Yay it worked</p>"; 
        );

    } else {
        echo "<p>Error: " . mysqli_errno($sql) . "</p>";
    }
?>

config.php

<?php

function connectToDB($db_host, $db_user, $db_pass, $db_name) {

//Create the db connection
$db_conn = new mysqli($db_host, $db_user, $db_pass, $db_name);

//Print out an error if any
if($db_conn->connect_error) {
    die('Error #' . $db_conn->connect_errno . ': ' . $db_conn->connect_error);
} 
else {
    //Connected msg
    echo "Connected: " . $db_conn->host_info;
}
    return $db_conn;
}
?>

Solutions Collecting From Web of "Get wp_current_user_id using PHP and MySQL"

Thanks to @Samuel Elh I manage to get this done.

I added this to my order-confirmation.php after I included config.php

    require ('../wp-blog-header.php');

    global $wpdb;

    global $current_user;
    get_currentuserinfo();

    $skrivID = $current_user->ID;

Then the insert query, I added the table I wanted the user ID stored in and used the variable $skrivID as value.

        $sql = mysqli_query($db_conn, "
        INSERT INTO ordre(
            userID,
            user_fname,
            user_lname,
            user_address, 
            user_email,
            user_phone, 
            service_chosen,
            user_note,
            order_date,
            statusID
        )
        VALUES(
            '".$skrivID."',
            '".$_POST['user_fname']."',
            '".$_POST['user_lname']."',
            '".$_POST['user_address']."',
            '".$_POST['user_email']."',
            '".$_POST['user_phone']."',
            '".$_POST['service_chosen']."',
            '".$_POST['user_note']."',
            '".date('Y-m-d')."',
            '1' 
        )
    ");

Ran the form with two different users and voila

http://dump.no/files/b007c7cd4c6e/2016-05-19_10_28_18-frigg.hiof.no___localhost___interaktiv_v1631___ordre___phpMyAdmin_2.9.1.1.jpg

Thanks again Samuel!