1

我在网站上有一个表格。此表格收集有关事件的数据。除了 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_ 就像以前一样,我有一种感觉,我会让它运行正常。谢谢你偷看。

4

1 回答 1

0

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

什么是依赖注入?

在构造函数中将 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 );

但那是不同的时间。

于 2013-09-23T19:00:59.580 回答