I wanted to store the current user ID to the database, but instead I got that error, the php file is custom and i put it inside my wordpress theme folder, how can I fix this? I wanted to make a simple custom php script that can store data to the database. And I think wp_get_current_user() is the problem, how do I use it properly?
Here is my code
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$current_user = wp_get_current_user();
$current_user_id = $current_user->ID;
$title = mysqli_real_escape_string($conn, $_REQUEST['title']);
$department = mysqli_real_escape_string($conn, $_REQUEST['department']);
$date_today = date("Y-m-d");
$doc = $_FILES['fileName']['name'];
$doc_path = $_SERVER['DOCUMENT_ROOT'] . "/wordpress/wp-content/uploads/documents/" . $doc;
$sql = "INSERT INTO wp_document (ID,
department,
title,
submit_date,
doc_path,
revision_count,
rejection_count,
status)
VALUES ($current_user_id, '$department', '$title','$date_today', '$doc_path', 0, 0, 'Not Reviewed')";
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}