1

我有一个错误,我不知道我做错了什么。请帮忙:)所以,我有一个名为活动的类,其中我有几个函数,如下所示:

class Activities
{
  public $content = null;

  public function __construct( $data=array() ) {
    if ( isset( $data['content'] ) ) $this->content = $data['content'];
  }

  public function storeFormValues ( $params ) {

    $this->__construct( $params );

  }

  public static function getData() {
    $conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
    $sql = "SELECT * FROM exceptions WHERE name = 'atrakcje'";
    $st = $conn->prepare( $sql );
    $st->execute();
    $row = $st->fetch();
    $conn = null;
    if ( $row ) return new Activities( $row );
  }

  public function update() {

    $conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
    $sql = "UPDATE exceptions SET content=:content WHERE name = 'atrakcje'";
    $st = $conn->prepare ( $sql );
    $st->bindValue( ":content", $this->content, PDO::PARAM_STR );
    $st->execute();
    $conn = null;
  }
}

我在我制作的小管理面板中有一个函数 editActivities() :

function editActivities() {

  $results = array();
  $results['pageTitle'] = "Edytuj Atrakcje";
  $results['formAction'] = "editActivities";

  if ( isset( $_POST['saveChanges'] ) ) {

    $activities->storeFormValues( $_POST );
    $activities->update();
    header( "Location: admin.php?action=editActivities&status=changesSaved" );

  } elseif ( isset( $_POST['cancel'] ) ) {

    header( "Location: admin.php?action=editActivities" );

  } else {

    $results['activities'] = Activities::getData();
    require( TEMPLATE_PATH . "/admin/editActivities.php" );

  }
}

我也有一个 HTML 表单来进行更改。但是当我提交填写好的表格时,我收到一个错误:

Fatal error: Call to a member function storeFormValues() on a non-object in C:\xampp\htdocs\admin.php on line 285

我的代码中的 285 行在这里:

if ( isset( $_POST['saveChanges'] ) ) {

    $activities->storeFormValues( $_POST );
    $activities->update();

我不知道发生了什么事。我有相同的代码来编辑其他文章和用户,但我不知道这里有什么不好。谢谢你的帮助!

4

2 回答 2

1

$activities没有实例化,所以你不能调用它的实例方法之一。你应该用它来实例化它

$activities = new Activities($_POST);

此外,在您的静态函数中,getData您可以返回 anActivities或 NULL,具体取决于$row. 但这$results['activities']有时可能会成为对象,有时可能不会,这很难处理,并且可能导致再次引发相同的错误。

于 2013-06-07T22:08:10.183 回答
0

添加

$activities = new Activities();
$activities->storeFormValues( $_POST );
....
于 2013-06-07T21:58:12.573 回答