如何在我的代码中修复include_once问题?

时间:2014-10-27 08:44:50

标签: php html

我还是PHP新手,我已经尝试了几种解决方案,但我似乎错过了一点。 我有2个警告,1个致命错误。我对这两个警告更感兴趣,但欢迎任何帮助。

(!)警告:include_once(model / register / register_member.php):无法打开流:第3行的C:\ wamp \ www \ controller \ register \ index.php中没有这样的文件或目录

(!)警告:include_once():无法在C:\ wamp \ www \ controller \ register中打开'model / register / register_member.php'以包含(include_path ='。; C:\ php \ pear') \ index.php在第3行 (!)致命错误:在第20行的C:\ wamp \ www \ controller \ register \ index.php中的非对象上调用成员函数prepare()

我的5个文件:(全部都在C:\ wamp \ www中)

模型/注册/ register_member.php

function register_member($pseudo, $pass_hache, $email) {
 
    global $db;
    $req = $db->prepare('INSERT INTO membres(pseudo, password, email, date_inscription) VALUES(:pseudo, :password, :email, CURDATE())');
    $req->execute(array(
        'pseudo' => $pseudo,
        'email' => $email));
    $req->closeCursor();
        'password' => $pass_hache,
}

控制器/注册/ index.php的

<?php
 
include_once('model/register/register_member.php');
 
if(isset($_POST['inscription'])) {
 
 
    // On rend inoffensif les données de l'utilisateur
    $_POST['pseudo'] = htmlspecialchars($_POST['pseudo']);
    $_POST['password'] = htmlspecialchars($_POST['password']);
    $_POST['password2'] = htmlspecialchars($_POST['password2']);
    $_POST['email'] = htmlspecialchars($_POST['email']);
 
 
    /**
     * Vérification si le pseudo est disponible
     */
 
    global $db;
    $req = $db->prepare('SELECT pseudo FROM membres WHERE pseudo = ?');
    $req->execute(array($_POST['pseudo']));
 
    $resultat = $req->fetch();
 
    if (!$resultat) {
        $pseudo = $_POST['pseudo'];
    } else {
        include_once('view/register/unavaipseudo.php');
    }
 
 
    /**
     * Vérification de la validité de l'adresse mail
     */
    if (isset($_POST['email'])) {
        $_POST['email'] = htmlspecialchars($_POST['email']); // On rend inoffensives les balises HTML que le visiteur a pu rentrer
 
        if (preg_match("#^[a-z0-9._-]+@[a-z0-9._-]{2,}\.[a-z]{2,4}$#", $_POST['email'])) {
            $email = $_POST['email'];
        } else {
            //echo 'L\'adresse ' . $_POST['mail'] . ' n\'est pas valide, recommencez !';
            //header('Location: mypage.php');
            include_once('view/register/wrongmail.php');
        }
    }
 
    /**
     * Vérification si le mdp est correct
     */
    if (!($_POST['password'] == $_POST['password2']))
        include_once('view/register/wrongpass.php');
 
    /**
     * Hachage du mot de passe
     */
    $pass_hache = sha1($_POST['password']);
 
    $pseudo = $_POST['pseudo'];
    $email = $_POST['email'];
 
    register_member($pseudo, $pass_hache, $email);
 
    echo 'Vous avez été inscrits !';
}
else
{
    include_once('view/register/index.php');
}

查看/注册/ index.php的

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml">
 
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Inscription</title>
    </head>
    <style>
        form
        {
            text-align:center;
        }
    </style>
      
    <body>
        <h3>Inscrition à l'espace membre.</h3>
 
        <form action="controller/register/index.php" method="post">
                <p><label for="pseudo">Pseudo :</label> <input type="text" id="pseudo" name="pseudo" /></p>
                <p><label for="pass">Mot de passe : </label> <input type="password" id="pass" name="password" /></p>
                <p><label for="verif_pass">Retapez votre mot de passe : </label> <input type="password" id="password2" name="password2" /></p>
                <p><label for="email">Adresse email :</label> <input type="text" id="email" name="email" /></p>
                  
                <input type="submit" value="Inscription" name="inscription" />
        </form>
    </body>
</html>

register.php(在root中)

<?php
 
include_once('model/connexion_ident_conn.php');
include_once('controller/register/index.php');
 
 
/*if (!isset($_GET['section']) OR $_GET['section'] == 'index')
{
    include_once('controller/register/index.php');
}*/

当然,还有一个连接到数据库的文件( model / connexion_ident_conn.php )。 我知道我的代码质量非常糟糕,我将重写OOP中的所有内容。但我真的对我现在缺少的东西感兴趣。

2 个答案:

答案 0 :(得分:0)

你的档案在里面 controller/register/index.php

你包括 include_once('model/register/register_member.php');

可能您不包括此处web-root的文件。尝试使用前导斜杠: include_once('/model/register/register_member.php');
或者您必须返回文件结构: include_once('/../../model/register/register_member.php');

致Alex Linte的评论:

Autoload表示您使用的每个班级都会自动包含在内。
看看如何实现它,在这里:
http://php.net/manual/en/language.oop5.autoload.php

答案 1 :(得分:0)

你的2个警告是你的路径错误!

所以不要这样:

include_once('model/register/register_member.php');

它应该是:

include_once('/../../model/register/register_member.php');

并且您的错误与您的$db变量不同,该变量不是对象!