2

试图在 PHP 中创建一个 CRUD 类。我基本上是在努力坚持让我做的一切都是面向对象的。我的问题是,如何将表单值提交给类本身,我有一个标准表单,它只是输出到这个文件名。

但是,我需要它来引用正确的类......提前为帮助干杯!

<?php 

require('connection/connection.php');

class Crud{
public function __construct(){

    $v_title = $_POST['v_id'];
    $v_title = $_POST['v_title'];
    $v_features = $_POST['v_features'];
    $yt_id = $_POST['yt_id'];
    $v_subject = $_POST['v_subject'];
    $taname = "videohubapp";

    echo $v_title;
}

protected static function Create($v_id, $v_title, $v_features, $yt_id, $v_subject){

    #Creates entries
    $query = $conn->prepare("INSERT INTO $taname (v_title, v_features, yt_id, v_subject) VALUES (:v_title, :v_features, yt_id, v_subject)");
    $query->bindParam(":v_title", $v_title);
    $query->bindParam(":v_features", $v_features);
    $query->bindParam(":yt_id", $yt_id);
    $query->bindParam(":v_subject", $v_subject);
    $query->execute();
}

public static function Read($v_id, $v_title, $v_features, $yt_id, $v_subject){

    #Reads database entries
    $query = $conn->prepare("SELECT * FROM $taname ORDER BY v_id");
    $query->setFetchMode(PDO::FETCH_ASSOC);
    while($row = $conn->fetch())
    {
        echo $row['v_id'] . "\n";
        echo $row['v_title'] . "\n";
        echo $row['v_features'] . "\n";
        echo $row['yt_id'] . "\n";
        echo $row['v_subject'] . "\n";
    }

}

protected static function Update($v_id, $v_title, $v_features, $yt_id, $v_subject){

    #Updates database entries
    $query = $conn->prepare("UPDATE $taname SET (v_title = :v_title, v_features = :v_features, yt_id = :yt_id, v_subject = :v_subject) WHERE v_id = :v_id");
    $query->bindParam(":v_id", $v_id);
    $query->bindParam(":v_title", $v_title);
    $query->bindParam(":v_features", $v_features);
    $query->bindParam(":yt_id", $yt_id);
    $query->bindParam(":v_subject", $v_subject);
    $query->execute();
}

protected static function Delete($v_id, $v_title, $v_features, $yt_id, $v_subject){

    #Delete entries from the database
    $query = $conn->prepare("DELETE FROM $taname WHERE v_id = :v_id");
    $query->bindParam(":v_id", $v_id);
    $query->execute();
}

public static function XMLWebService(){

    #XMLParse
    $query = $conn->prepare("SELECT * FROM $taname");
    $query->execute();

}

}

?>
4

1 回答 1

0

首先看一下Model View Controller Pattern
同样在您的班级中尝试使用属性并使用$this

Class Crud{
 public $v_id,$v_title,$v_features,$yt_id,$v_subject,$taname; // all attributes

    public function __construct(){

    $this->v_title = $_POST['v_id'];
    $this->v_title = $_POST['v_title'];
    $this->v_features = $_POST['v_features'];
    $this->yt_id = $_POST['yt_id'];
    $this->v_subject = $_POST['v_subject'];
    $this->taname = "videohubapp";

    var_dump($this); // dump this object.
}
//.. rest of the code

提交表单时,这样的事情会起作用,但取决于您如何处理。

if(isset($_POST["submit"])){
 // a form submitted
 $MyCRUDObj = new CRUD(); // since you pass the values to the attributes of object from $_POST you don't need any parameters.
var_dump($MyCRUDObj); // dump form object and see what values attributes has.
}

建议尝试根据您的需要清理和验证表单值。
希望我有帮助

于 2012-07-20T19:30:41.147 回答