PDO插入MySQL数据库

时间:2018-05-04 12:07:18

标签: mysql pdo prepared-statement simple-html-dom

我试图将解析后的表插入到mysql数据库中。但是数据库中的表保持空白。没有插入任何值。

我已经检查了这个PDO with INSERT INTO through prepared statements并试图将答案应用到我的案例中。但我仍然遇到麻烦。我错过了什么?

更新: 因此,对于任何有同样问题的人来说,这最终是如何运作的:

$pdo = new PDO("mysql:host=$host;dbname=$dbname",$config['DB_USERNAME'],$config['DB_PASSWORD']);
$empty = "TRUNCATE TABLE `teams`";
$statement = $pdo->prepare($empty);
$statement->execute();


// Find TABLE INSIDE HTML
$table = $html->find('table', 1);
// find CELLS of each ROW, starting at 2nd row
foreach($table ->find('tr') as $rowNumber => $row) { if ( $rowNumber < 1 ) continue;    // Foreach row in the table!
$team = "Timberwolves";
$season = "9";
$pos    = $row->find('td', 0)->plaintext;
$player = $row->find('td', 1)->plaintext;
$age    = $row->find('td', 2)->plaintext;
$twoga  = $row->find('td', 3)->plaintext;
$twopct = $row->find('td', 4)->plaintext;
$fta    = $row->find('td', 5)->plaintext;
$ftpct  = $row->find('td', 6)->plaintext;
$threega = $row->find('td', 7)->plaintext;
$threepct = $row->find('td', 8)->plaintext;
$orb    = $row->find('td', 9)->plaintext;
$drb    = $row->find('td', 10)->plaintext;
$ast    = $row->find('td', 11)->plaintext;
$stl    = $row->find('td', 12)->plaintext;
$tov    = $row->find('td', 13)->plaintext;
$blk    = $row->find('td', 14)->plaintext;
$oo     = $row->find('td', 15)->plaintext;
$do     = $row->find('td', 16)->plaintext;
$po     = $row->find('td', 17)->plaintext;
$to     = $row->find('td', 18)->plaintext;
$od     = $row->find('td', 19)->plaintext;
$dd     = $row->find('td', 20)->plaintext;
$pd     = $row->find('td', 21)->plaintext;
$td     = $row->find('td', 22)->plaintext;

// Echo some of the found values to test the code
echo    "'$season', '$team', '$pos', '$player', '$age', '$twoga', '$twopct','$fta','$ftpct','$threega', '$threepct', '$orb','$drb','$ast','$stl','$tov','$blk','$oo','$do','$po','$to','$od','$dd','$pd','$td'<br>";

// INSERT for each row
$sql ="INSERT INTO teams (season,team,pos,player,age,2ga,2gp,fta,ftp,3ga,3gp,orb,drb,ast,stl,tov,blk,oo,do,po,`t-o`,od,dd,pd,td) 
        VALUES (:season,:team,:pos,:player,:age,:twoga,:twopct,:fta,:ftpct,:threega,:threepct,:orb,:drb,:ast,:stl,:tov,:blk,:oo,:do,:po,:to,:od,:dd,:pd,:td)";
try {
$statement = $pdo->prepare($sql);                       

$statement->execute(array(
':season' => $season,
':team' =>$team,
':pos' =>$pos,
':player' =>$player, 
':age' =>$age,
':twoga' =>$twoga,
':twopct' =>$twopct,
':fta' =>$fta,
':ftpct' =>$ftpct,
':threega' =>$threega,
':threepct' =>$threepct,
':orb' =>$orb,
':drb' =>$drb,
':ast' =>$ast,
':stl' =>$stl,
':tov' =>$tov,
':blk' =>$blk,
':oo' =>$oo,
':do' =>$do,
':po' =>$po,
':to' =>$to,
':od' =>$od,
':dd' =>$dd,
':pd' =>$pd,
':td' =>$td
));
} catch(PDOException $e) {
        echo $e->getMessage();
    }                         
// END OF LOOP
}

0 个答案:

没有答案