在php中遇到oop和pdo问题

时间:2013-09-23 18:16:03

标签: php mysql oop pdo

我在网站上有一张表格。此表单收集有关EVENT的数据。除了ID之外,该事件还有20个字段,这些字段位于表中,与事件对应。 在我刚刚接受($ _POST ['submit'])之前,如果是,则使用mysql_query将事件字段插入数据库。

现在我正在尝试使用oop和这个pdo,而且我在这一点上几乎打败了自己。我确信我做错了很多。

<?php  
$str = $_SERVER['DOCUMENT_ROOT'];
include  $str."/wp-config.php";

$config['db'] = array(
    'host'      => DB_HOST,
    'username'  => DB_USER,
    'password'  => DB_PASSWORD,
    'dbname'    => DB_NAME,
    'charset'   => DB_CHARSET
);


$db = new PDO('mysql:host='.$config['db']['host'].';dbname='.$config['db']['dbname'].';charset='.$config['db']['charset'], $config['db']['username'], $config['db']['password']);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDF::ATTR_EMULATE_PREPARES, false);


class match extends event {
    public $_matchno;
    public $_completed;

    function __construct() {
        $this->_completed = "N";
    }

    public function addMatch($matchno) {
        $sql = $db->prepare("INSERT INTO wp_hue5gf_match_details (matchno, event_name, dateofmatch, fighttype, comp, matchref, fightclass) 
            VALUES (".$matchno.", $this->_eventname, $this->_doe, $this->_status, $this->_completed, $this->_refree, $this->_fightclass)");
        $sql->execute();
    }
}

class event {
    public $_promouser;
    public $_eventname;
    public $_fightclass;
    public $_no_of_matches;
    public $_status;
    public $_doe;
    public $_venue;
    public $_city;
    public $_state;
    public $_zip;
    public $_sanc_body;
    public $_doctor;
    public $_refree;
    public $_referee2;
    public $_judge;
    public $_judge2;
    public $_judge3;
    public $_boxcomm;
    public $_descript;

    function __construct() {
        $this->_promouser       = $_SESSION['username'];
        $this->_eventname       = $_POST['ename'];
        $this->_fightclass      = $_POST['efightclass'];
        $this->_no_of_matches   = $_POST['no_of_matches'];
        $this->_status          = $_POST['estatus'];
        $this->_doe             = $_POST['year']."-".$_POST['month']."-".$_POST['day'];
        $this->_venue           = $_POST['venue'];
        $this->_city            = $_POST['city'];
        $this->_state           = $_POST['state'];
        $this->_country         = $_POST['country'];
        $this->_zip             = $_POST['zip'];
        $this->_sanc_body       = $_POST['sbody'];
        $this->_doctor          = $_POST['doctor'];
        $this->_refree          = $_POST['refree'];
        $this->_referee2        = $_POST['refree2'];
        $this->_judge           = $_POST['judge'];
        $this->_judge2          = $_POST['judge2'];
        $this->_judge3          = $_POST['judge3'];
        $this->_boxcomm         = $_POST['boxcom'];
        $this->_descript        = $_POST['descript'];
    }

    public function insertEvent() {
        $sql = $db->prepare("INSERT INTO event (promouser, eventname, fightclass, no_of_matches, status, doe, venue, city, state, country, zip, 
            sanc_body, doctor, refree, referee2, judge, judge2, judge3, boxcomm, descript) VALUES ($this->_promouser, $this->_eventname, $this->_fightclass, $this->_no_of_matches, $this->_status, $this->_venue, 
            $this->_city, $this->_state, $this->_country, $this->_zip, $this->_sanc_body, $this->_doctor, $this->_refree, $this->_referee2, $this->_judge, $this->_judge2, $this->_judge3, $this->_boxcomm, $this->_descript)");
        $sql->execute();
    }    
}
?>

这就是我的课程页面。现在到添加事件页面。我显然做错了什么。我不知道从哪里开始。

<?php
require_once(bloginfo( 'template_url' ).'/definedClasses.php');
if($_POST['submit'])
{
    $event = new event();

    event::insertEvent();

    for($y=1;$y<= $event->_no_of_matches;$y++)
    {
        $match = new match();
        match::addMatch($y);
    }
}

?> 
 <form name="addevent" method="post" action="" enctype="multipart/form-data" >
  <!-- The form here with all these inputs for the post values -->
</form>

我确定我想要立刻做太多事情。我应该把它减少到一个小得多的问题然后通过它。但我不知道从哪里开始。我不是在找你为我做这件事,我正在寻找一些我不理解的指导。我很确定它与PDO有关。如果这是mysql_,就像过去一样,我有一种感觉,我会让它正确运行。 谢谢你偷看。

1 个答案:

答案 0 :(得分:0)

您不能在课程中使用$db,因为他们不知道它存在。

What is dependency injection?

将PDO对象作为构造函数中的参数传递

function __construct( PDO $db ) {
    $this->$db = $db;
}

然后使用$this->db代替$db

也可以使用$event->insertEvent()而不是静态使用它。

更好的方法是

$event = new Event( $_POST );
$eventSaver = new EvenSaver( $db ); //Name is stupid but you get it
$eventSaver->saveEvent( $event );

但这是一个不同的时间。