I'm working on a very simple CMS system for a school project, and I'm having a bit of trouble.
Right now, my webpage is set up as so:
In my header.php I have this, along with other miscellaneous HTML:
<?php
require_once('php/constants.php');
require_once("php/functions.php");
?>
For constants.php - Contains every defined constant I can think of since I'd rather change the one constant here than search in my code to find the however many instances I could have elsewhere.
For Functions.php - Contains every function I'm creating for usage on this CMS. Contains everything from my emailing functions using PHPMailer, to Registering and Logging in using MeekroDB for Database interaction.
Here lies the problem.
On a page I have for account registration, have something along the lines of this
//get the username, password, first/last name, and send it to register function.
$firstName = $_POST['first_name'];
$lastName = $_POST['last_name'];
$password = $_POST['password'];
$email_address = $_POST['email_address'];
//register the user. where register() is in functions.php
if(register($email_address, $password, $firstName, $lastName))
{
echo "REGISTRATION SUCCESSFUL";
}
else
{
echo "REGISTRATION UNSUCCESSFUL";
}
In my functions.php file, I declare my database information using the DB::QUERY assignment that MeekroDB mentions I use.
//MeekroDB - Used for database CRUDding
require_once("php/meekrodb.php");
DB::$user = 'root'; //yes i'll change this later
DB::$password = ''; //this too
DB::$dbName ="testDB";
In the actual register function I have this:
function register($email, $password, $firstName, $lastName)
{
//check to see if the user already exists
//poll the DB to see if the email is already in there.
$results = DB::QUERY("SELECT email FROM users WHERE email = %s", $email);
//if there is a row in the DB with this email...
if(DB::count() >= 1)
{
return false;
}
else
{
//lets prepare the user for insertion into the database.
$username = $firstName . '_' . $lastName;
//generate a salt for database insertion and password hashing
$salt = hash('sha512', uniqid(mt_rand(1, mt_getrandmax()), true));
$password = hash('sha512', $password . $salt);
//create the insert statement array for insertion.
$insertionArray =
array(
'username' => $username,
'password' => $password,
'email' => $email,
'salt' => $salt,
'isAdmin' => 0
);
//insert the new user into the database.
DB::INSERT('users', $insertionArray);
//Send a registration email to the new user.
//Send an email to the user stating they've registered.
$mailer = createMailer();
//set the contents of the email
$mailer->MsgHTML(generateEmail_Registration($username));
//set the destination address
$mailer->AddAddress($email, $firstName . ' ' . $lastName);
if($mailer->Send())
{
return true;
}
else
{
return false;
}
}
}
To me, this should work.
HOWEVER:
WHenever I try running the register() function, I end up getting tons of errors involving MeekroDB and I'm not 100% sure how to fix them (whether it's an INCLUDE issue, or maybe it's something with meekroDB?)
Here are the errors I'm getting.
Warning: mysqli::set_charset(): invalid object or resource mysqli in D:\xampp\htdocs\finalproject\php\meekrodb.php on line 186
Warning: MeekroDB::get(): Property access is not allowed yet in D:\xampp\htdocs\finalproject\php\meekrodb.php on line 188
Warning: mysqli::real_escape_string(): invalid object or resource mysqli in D:\xampp\htdocs\finalproject\php\meekrodb.php on line 496
Warning: mysqli::query(): invalid object or resource mysqli in D:\xampp\htdocs\finalproject\php\meekrodb.php on line 628
Warning: MeekroDB::queryHelper(): Property access is not allowed yet in D:\xampp\htdocs\finalproject\php\meekrodb.php on line 662
Warning: MeekroDB::queryHelper(): Property access is not allowed yet in D:\xampp\htdocs\finalproject\php\meekrodb.php on line 663
Warning: mysqli::real_escape_string(): invalid object or resource mysqli in D:\xampp\htdocs\finalproject\php\meekrodb.php on line 496
Warning: mysqli::real_escape_string(): invalid object or resource mysqli in D:\xampp\htdocs\finalproject\php\meekrodb.php on line 496
Warning: mysqli::real_escape_string(): invalid object or resource mysqli in D:\xampp\htdocs\finalproject\php\meekrodb.php on line 496
Warning: mysqli::real_escape_string(): invalid object or resource mysqli in D:\xampp\htdocs\finalproject\php\meekrodb.php on line 496
Warning: mysqli::query(): invalid object or resource mysqli in D:\xampp\htdocs\finalproject\php\meekrodb.php on line 628
Warning: MeekroDB::queryHelper(): Property access is not allowed yet in D:\xampp\htdocs\finalproject\php\meekrodb.php on line 662
Warning: MeekroDB::queryHelper(): Property access is not allowed yet in D:\xampp\htdocs\finalproject\php\meekrodb.php on line 663
REGISTRATION UNSUCCESSFUL //of course.
I think it has something to do with the way I'm including my various PHP files, and that in return is buggering up my MeekroDB connection somehow.
If you guys need more information, let me know but I think I've pretty much gotten everything out on the table.
I don't post here often, so if I've missed out on some kind of ettiquette somewhere, I apologize in advance.
Thanks! :)
If you follow along in the meekrodb.php file, the first error is called on your $mysql object which has just attempted a connection (have a look at the meekrodb.php file to see what I'm talking about) so these errors probably have to do with the connection information being bad or some other issue connecting to the database, and so other functions are failing.
A simple way to see more informative errors would be to try using their $throw_exception_on_error
function:
function register($email, $password, $firstName, $lastName)
{
//check to see if the user already exists
//poll the DB to see if the email is already in there.
DB::$error_handler = false; // since we're catching errors, don't need error handler
DB::$throw_exception_on_error = true;
try {
$results = DB::QUERY("SELECT email FROM users WHERE email = %s", $email);
} catch(MeekroDBException $e) {
echo "Error: " . $e->getMessage() . "<br>\n"; // this should be a better error...
echo "SQL Query: " . $e->getQuery() . "<br>\n"; // SELECT email FROM users...
}
...