提交OO-PHP Crud的表单

时间:2012-07-20 19:10:40

标签: php mysql oop object

尝试在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();

}

}

?>

1 个答案:

答案 0 :(得分: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.
}

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