I am working on a PHP backend script for my WordPress site to update a database table when a user submits a form. It is throwing an error at the line with $wpdb->prepare
.
Fatal error: Uncaught Error: Call to a member function prepare() on null
Here is my current test script.
<?php
if (isset($_POST['SaveNewCourse'])){
$courses_query = "INSERT INTO SC_COURSES (COURSE_NAME, COURSE_DESC, ACTIVE, ADDED_BY_USER_ID, ADDED_DTG) VALUES ( %s, %s, 1, %d, %s )";
$newcoursename = $_POST['AddCourseCourseName'];
$newcoursedesc = $_POST['AddCourseCourseDesc'];
$newcourseuserid = $_POST['AddCourseUserID'];
$newcoursedtg = $_POST['AddCourseDTG'];
global $wpdb;
$prepared_query_courses = $wpdb->prepare( $courses_query, $newcoursename, $newcoursedesc, $newcourseuserid, $newcoursedtg );
echo $prepared_query_courses;
}
?>
I realize it is not actually trying to update the database right now, I am just trying to test it out before I make it go live, so I wanted to see what the prepared INSERT statement looked like. What do I need to change to prevent the error?