0

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;
}
?>
6
  • Couldn't you let WordPress handle the DB stuff? like, require('wp-blog-header.php') which is in the root, then use wp_get_current_user()->ID to get the user ID if logged in.. Also look at $wpdb codex.wordpress.org/Class_Reference/wpdb for handling db queries..
    – Ismail
    May 19 '16 at 0:19
  • Unfortunately not, it's a school project and our teacher have named some specific things we must do and this is one of them, create your own form, your own tables inside the database and get them in with your own mysqli queries. In his examples he also used this method, prob not the best but it is what it is :) May 19 '16 at 0:23
  • Ah. So now, getting wp_current_user_id stored in a table. Is it okay to use $wpdb this time?
    – Ismail
    May 19 '16 at 0:24
  • I should imagine so since all the other values are "by the teachers book". But how to make it it work inside order_confirmation.php? May 19 '16 at 1:23
  • 1
    That didn't quite work but you put me on right track, thank you so much dude :D What I did was require ('../wp-blog-header.php'); global $wpdb; global $current_user; get_currentuserinfo(); $skrivID = $current_user->ID; Then in the insert query I added userID on the top and used the variable under VALUES. I'll update the thread. Thanks again! May 19 '16 at 8:23
1

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!

5
  • Awesome! glad you got it working now. I'd suggest looking at vip.wordpress.com/documentation/best-practices/security/… while inserting data, to prevent possible SQL injections at least you could sanitize the user submitted data (e.g sanitize_text_field( $_POST['user_fname'] )). Good luck with the rest of the project :)
    – Ismail
    May 19 '16 at 12:19
  • @SamuelElh Thanks for the tip, I assume having good security will score some extra points ^^, Thanks man, appreciate your help. Now just to figure out this stuff: wordpress.stackexchange.com/questions/227174/… =D May 19 '16 at 12:28
  • No worries. You're welcome. You can mark your answer as accepted for future viewers :)
    – Ismail
    May 19 '16 at 12:45
  • 1
    I will, must wait until tomorrow ^^, May 19 '16 at 12:45
  • Ah got it. I had the same experience when I first joined StackOverflow ;)
    – Ismail
    May 19 '16 at 12:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.