2015-11-21 49 views
1

我正在为一个学校项目开发一个非常简单的CMS系统,而且我遇到了一些麻烦。PHP - include_once/include不工作

现在,我的网页被设置为这样:

  • 我创建一个用户可以访问的每一个网页 - 在顶部和底部,我有一个include_once(header.php文件)和include_once( footer.php)。这些包含应该是加载的任何页面的顶部和底部。

在我的header.php我都这样了,其他杂项HTML一起:

<?php require_once('php/constants.php'); require_once("php/functions.php"); ?>

对于constants.php - 包含了每一个定义的常量我能想到的,因为我宁愿改变在我的代码中搜索一个常量,以查找我可以在其他地方找到的很多例子。

For Functions.php - 包含我在此CMS上使用的每个函数。包含我使用PHPMailer的电子邮件功能,使用MeekroDB进行数据库交互的注册和登录。

这就是问题所在。

在页面上我有账号注册,沿着这

//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";  
     } 

线的东西在我functions.php文件,我宣布使用DB :: QUERY的作业,MeekroDB我提到我的数据库信息使用。

//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"; 

在实际的寄存器功能我有这样的:

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

}  

对我来说,这应该工作。

但是:

每当我试图运行寄存器()函数,我最终得到吨,涉及MeekroDB错误的,我不是100%确定如何解决这些问题(无论它是一个包括发布日期,或许这是与meekroDB东西?)

这是我得到的错误。

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. 

我认为这是与我包括我的各种PHP文件的方式,并在返回莫名其妙buggering我的MeekroDB连接。

如果你们需要更多的信息,请告诉我,但我想我已经把所有东西都拿出来了。

我不经常在这里发帖,所以如果我错过了某种地方的礼节,我提前道歉。

谢谢! :)

回答

1

如果您沿用meekrodb.php文件,则会在刚刚尝试连接的$ mysql对象上调用第一个错误(查看meekrodb。php文件来查看我在说什么),所以这些错误可能与连接信息不佳或连接到数据库的其他问题有关,所以其他功能失败。

一个简单的方法来看到更多的信息错误,将使用他们的$throw_exception_on_error功能尝试:

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... 

    } 
    ... 
+0

我会检查这一关是正确的答案,因为你是那里为奇数信息正确(该死的错别字) 非常感谢你。加上我不知道错误部分的抛出异常。感谢您的支持 – Pilapodapostache

+0

没有汗 - 祝您的项目顺利! –

0

如果这是与包括问题/ include_once你会得到完全不同的消息。我首先推荐使用2.3版本的最新版本库,最重要的是检查你使用的PHP版本。

从我可以告诉有最有可能的问题与库MySQLi连接,并根据您的PHP版本的检查,以确保没有失败的连接已经有问题,此页http://php.net/manual/en/mysqli.connect-error.php

作为发现

警告 mysqli-> connect_error属性仅在PHP 5.2.9和5.3.0版本中正常工作。如果需要与早期PHP版本兼容,请使用mysqli_connect_error()函数。

由于没有连接,这将导致您在此表单中看到的多个警告。

警告:mysqli的:: METHOD_NAME():无效的对象或资源的mysqli