我在网站上有一个表格。此表单收集有关事件的数据。除 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();
}
}
?>
那是我的类(class)页面。现在到添加事件页面。我显然做错了什么。我不知道从哪里开始。
<?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_,我觉得我会让它正常运行。 感谢您浏览。
最佳答案
你不能在你的类中使用 $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 );
但那是另一个时代。
关于php - php 中的 oop 和 pdo 有问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18966065/