我的PHP中有什么导致内部服务器错误?

时间:2015-07-02 19:43:51

标签: php ajax oop post

我是PHP的新手,需要一些帮助来找出我的member_update脚本有什么问题。我有一个小时前工作,当它是一堆乱七八糟的意大利面时。一旦我试图通过使用类使其更紧凑和可维护,它就停止工作了。所以,我可以保证信息($_POST['memberAction']$_FILES['pic']等)正确地提交到服务器,但我对它的处理有点不妥。请帮我弄清楚我做错了什么。

member_update.php

<?php

include "../classes.php";

$message = ''; 

$message .=  'Action: ' . $_POST['memberAction'] . '\n';
$message .=  'Name: ' . $_POST['fullname'] . '\n';
$message .=  'Title: ' . $_POST['title'] . '\n';
$message .=  'Biography: ' . $_POST['bio'] . '\n';
$message .=  'Sort order: ' . $_POST['sord'] . '\n';
$targetFileName = basename($_FILES['pic']['name']);
$targetFileNameAndPath = 'assets/' . $targetFileName;
$message .= 'Target file: ' . $targetFile . '\n';

TeamManager tm = new TeamManager();

$thisAction = $_POST['memberAction']; 

if ($thisAction == 'add')
{

    $addMemberResult = tm.addMember(new TeamManager::TeamMember ($_POST['fullname'], $_POST['title'], $_POST['bio'], $targetFileName) )

    if ($addMemberResult->succeeded)
    {
        $message .= "Successfully added member to list\n";

        if (move_uploaded_file($_FILES['pic']['tmp_name'], $targetFileNameAndPath))
        {
            $message .= "Successfully added " . $targetFileName . " to assets folder\n";        
        }
        else
        {
            $message .= "Encountered problem when trying to add " . $targetFileName . " to assets folder\n";
        }
    }
    else
    {
        $message .= ""
    }
}
elseif ($thisAction == 'update')
{
    // ... 
}
elseif ($thisAction == 'delete')
{
    // ...      
}
else 
{
    $message .= 'Didn\'t recognize action';
}

echo json_encode($message);

?>

classes.php

<?php

final class MySqlInfo
{
    const DBNAME = 'mydb';
    const USER = 'myuser';
    const PSSWD = 'something';
    const TEAMTABLENAME = 'somename';

    public getUser ( )
    {
        return self::USER;
    }

    public getPassword ( ) 
    {
        return self::PSSWD; 
    }
}

final class MethodResult
{

    public $succeeded; 
    public $message; 

    public MethodResult()
    {
        $succeeded = NULL; 
        $message = NULL;
    }
    public MethodResult ( $succeededInit, $messageInit )
    {
            $succeeded = $succeededInit;
            $message = $messageInit;
    }   

}

final class AllySite 
{

    const ROOTURL = 'http://mysite/mysubsite';

    function getRootUrl()
    {
        return self::ROOTURL;
    }


}

final class TeamManager
{

    private $dbcon;

    public function TeamManager ( )
    {
        $dbcon = mysqli_connect('localhost', MySqlInfo.getUser(), MySqlInfo::getPassword());
        $dbcon->select_db(MySqlInfo::DBNAME);
        // need to add error handling here later
    }

    final public class TeamMember 
    {
        public $name; // team member name
        public $title; // team member title
        public $bio; // team member bio
        public $sord; // team member sort order
        public $picfn; // team member profile picture file name
    }

    public function addMember ( TeamMember M )
    {
        if (self::$dbcon->connect_error)
        {
            return new MethodResult(false, 'Not connected to database');
        }

        $q = "INSERT INTO" . MySqlInfo::TEAMTABLENAME . "( " . implode( ',' array(M->name, M->title, M->bio, M->sord, M->picfn) ) . ") VALUES ('" . implode('\',\'', array($_POST['fullname'], $_POST['title'], $_POST['bio'], $_POST['sord'], $targetFileName)) . "')";
        // ^ query for inserting member M to the database

        if (!mysqli_query($con, $q))
        {
            return new MethodResult(false, 'Query to insert new team member failed');
        }

        return new MethodResult(true, 'Successfully added new member' . M->name);
    }
}

?>

1 个答案:

答案 0 :(得分:0)

这两个文件都有语法问题,请参阅代码中的内联注释。 我试图修复大多数,但这并不意味着它将成为工作代码。

classes.php

<?php

final class MySqlInfo
{
    const DBNAME = 'mydb';
    const USER = 'myuser';
    const PSSWD = 'something';
    const TEAMTABLENAME = 'somename';

    //missing "function"
    public function getUser ( )
    {
        return self::USER;
    }
    // missing "function"
    public function getPassword ( ) 
    {
        return self::PSSWD; 
    }
}

final class MethodResult
{

    public $succeeded; 
    public $message; 

    // you are declaring the same function below again
    /*
    public function MethodResult()
    {
        $succeeded = NULL; 
        $message = NULL;
    }
    */
    public function MethodResult ( $succeededInit, $messageInit )
    {
            $succeeded = $succeededInit;
            $message = $messageInit;
            // if you are trying to set the public properties then do this
            // $this->message = $messageInit;
    }   

}

final class AllySite 
{

    const ROOTURL = 'http://mysite/mysubsite';

    function getRootUrl()
    {
        return self::ROOTURL;
    }


}

final class TeamManager
{

    private $dbcon;

    public function TeamManager ( )
    {
        $dbcon = mysqli_connect('localhost', MySqlInfo.getUser(), MySqlInfo::getPassword());
        $dbcon->select_db(MySqlInfo::DBNAME);
        // need to add error handling here later
    }

    // final public class ? plus you can't define a class inside another class

    /*
    final public class TeamMember 
    {
        public $name; // team member name
        public $title; // team member title
        public $bio; // team member bio
        public $sord; // team member sort order
        public $picfn; // team member profile picture file name
    }
    */

    // missing $ operator for the M parameter
    public function addMember ( TeamMember $M )
    {
        if (self::$dbcon->connect_error)
        {
            return new MethodResult(false, 'Not connected to database');
        }

        //added a comma before array, also M-> all need $
        $q = "INSERT INTO" . MySqlInfo::TEAMTABLENAME . "( " . implode( ',', array($M->name, $M->title, $M->bio, $M->sord, $M->picfn) ) . ") VALUES ('" . implode('\',\'', array($_POST['fullname'], $_POST['title'], $_POST['bio'], $_POST['sord'], $targetFileName)) . "')";
        // ^ query for inserting member M to the database

        if (!mysqli_query($con, $q))
        {
            return new MethodResult(false, 'Query to insert new team member failed');
        }

        //added $M
        return new MethodResult(true, 'Successfully added new member' . $M->name);
    }
}

?>

member_update.php

<?php

include "../classes.php";

$message = ''; 

$message .=  'Action: ' . $_POST['memberAction'] . '\n';
$message .=  'Name: ' . $_POST['fullname'] . '\n';
$message .=  'Title: ' . $_POST['title'] . '\n';
$message .=  'Biography: ' . $_POST['bio'] . '\n';
$message .=  'Sort order: ' . $_POST['sord'] . '\n';
$targetFileName = basename($_FILES['pic']['name']);
$targetFileNameAndPath = 'assets/' . $targetFileName;
$message .= 'Target file: ' . $targetFile . '\n';

$tm = new TeamManager();

$thisAction = $_POST['memberAction']; 

if ($thisAction == 'add')
{

    // is TeamMember a private property, a method ..., also missing ;
    $addMemberResult = $tm->addMember(new TeamManager::$TeamMember ($_POST['fullname'], $_POST['title'], $_POST['bio'], $targetFileName) );

    if ($addMemberResult->succeeded)
    {
        $message .= "Successfully added member to list\n";

        if (move_uploaded_file($_FILES['pic']['tmp_name'], $targetFileNameAndPath))
        {
            $message .= "Successfully added " . $targetFileName . " to assets folder\n";        
        }
        else
        {
            $message .= "Encountered problem when trying to add " . $targetFileName . " to assets folder\n";
        }
    }
    else
    {
        //missing ;
        $message .= "";
    }
}
elseif ($thisAction == 'update')
{
    // ... 
}
elseif ($thisAction == 'delete')
{
    // ...      
}
else 
{
    $message .= 'Didn\'t recognize action';
}

echo json_encode($message);

?>