使用PHP和MySQL获取wp_rent_user_id

时间:2016-05-19 作者:gruffmeister

Stack社区的杰出人士!

就像我这个新手一样,这花了很多小时,但最后我得到了:

wordpress中的HTML表单,用于将值提交到MySQL数据库,然后将值存储在表中此页面和表单需要登录才能使用,因此我现在要做的是获取wp_current_user_id 存储在表“order”中的字段和字段userID中。

有人知道这是否可行吗?

提前感谢您的帮助。干杯


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;
}
?>

1 个回复
最合适的回答,由SO网友:gruffmeister 整理而成

多亏了@Samuel Elh,我成功地完成了这件事。

我将此添加到订单确认中。php之后,我包括配置。php

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

    global $wpdb;

    global $current_user;
    get_currentuserinfo();

    $skrivID = $current_user->ID;
然后在insert查询中,我添加了希望存储用户ID的表,并使用变量$skrivID作为值。

        $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\' 
        )
    ");
用两个不同的用户运行表单

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

再次感谢塞缪尔!