2

PHPでCRUDクラスを作ろうとしています。私は基本的に、自分がすることすべてを OO にすることに固執しようとしています。私の質問は、フォームの値をクラス自体に送信する方法です。このファイル名に出力する標準フォームがあります。

ただし、適切なクラスを参照するにはそれが必要です... 事前に助けを求めて乾杯!

<?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 に答える