Вставка PDO в базу данных MySQL - PullRequest
0 голосов
/ 04 мая 2018

Я пытаюсь вставить разобранную таблицу в базу данных mysql. Но таблица в базе данных остается пустой. Ни одно из значений не вставлено.

Я уже проверил этот PDO с INSERT INTO через подготовленные заявления и попытался применить ответы к моему делу. Но у меня все еще проблемы. Чего мне не хватает?

Это мой код:

// Simple Dom
require_once('simple_html_dom.php');
require_once 'config.php';
    $host=$config['DB_HOST'];
    $dbname=$config['DB_DATABASE'];

// Get Connection to HTML and DATABASE  
$html = file_get_html('url');
$pdo = new PDO("mysql:host=$host;dbname=$dbname",$config['DB_USERNAME'],$config['DB_PASSWORD']);

// 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;

  $team = "Timberwolves";
  $season = "9";

    // Get all columns of the table
    $pos    = $row->find('td', 0);
    $player = $row->find('td', 1)->plaintext;
    $age    = $row->find('td', 2);
    $twoga  = $row->find('td', 3);
    $twopct = $row->find('td', 4);
    $fta    = $row->find('td', 5);
    $ftpct  = $row->find('td', 6);
    $threega = $row->find('td', 7);
    $threepct = $row->find('td', 8);
    $orb    = $row->find('td', 9);
    $drb    = $row->find('td', 10);
    $ast    = $row->find('td', 11);
    $stl    = $row->find('td', 12);
    $tov    = $row->find('td', 13);
    $blk    = $row->find('td', 14);
    $oo     = $row->find('td', 15);
    $do     = $row->find('td', 16);
    $po     = $row->find('td', 17);
    $to     = $row->find('td', 18);
    $od     = $row->find('td', 19);
    $dd     = $row->find('td', 20);
    $pd     = $row->find('td', 21);
    $td     = $row->find('td', 22);

    // 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
  $statement = $pdo->prepare("INSERT INTO `teams` (season,team,pos,player,age,2ga,2g%,fta,ft%,3ga,3g%,orb,drb,ast,stl,tov,blk,oo,do,po,to,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)");

  $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));

// END OF LOOP
}

Парсинг таблицы отлично работает в моих глазах. Я получу это как вывод:

'9', 'Timberwolves', 'PG', 'Mike Conley ', '29', '47', '50','51','86','61', '41', '8','28','61','56','58','8','8','8','6','7','6','6','2','7')
'9', 'Timberwolves', 'PG', 'Steve Blake ', '35', '15', '46','6','80','47', '34', '5','23','60','31','52','4','7','6','1','4','4','4','1','5')
'9', 'Timberwolves', 'SG', 'Isaiah Thomas ', '27', '59', '53','80','91','83', '38', '10','19','56','37','49','4','7','9','8','9','5','4','2','6')
'9', 'Timberwolves', 'SF', 'Rondae Hollis-Jefferson ', '22', '48', '46','45','75','12', '22', '34','62','27','64','60','25','3','5','7','3','8','6','7','6')
'9', 'Timberwolves', 'SF', 'Tony Snell ', '25', '14', '55','7','81','50', '41', '6','30','13','32','86','6','9','2','3','1','5','4','2','6')
'9', 'Timberwolves', 'PF', 'Joel Embiid ', '22', '77', '50','99','78','41', '37', '49','72','27','48','6','98','3','7','9','6','9','3','9','4')
'9', 'Timberwolves', 'PF', 'Draymond Green ', '26', '28', '49','27','71','35', '31', '25','63','69','87','53','43','5','7','4','4','9','7','7','8')
'9', 'Timberwolves', ' C', 'Marc Gasol ', '32', '64', '48','41','84','35', '39', '15','49','43','37','59','40','7','6','5','4','6','4','5','5')
'9', 'Timberwolves', ' C', 'Marreese Speights ', '29', '35', '52','31','88','65', '37', '40','63','15','23','70','29','7','4','7','1','5','1','6','2')
'9', 'Timberwolves', 'SF', 'Dante Cunningham ', '29', '19', '58','5','59','36', '39', '21','41','7','33','90','17','8','2','4','1','5','4','4','5')
'9', 'Timberwolves', ' C', 'Boban Marjanovic ', '28', '69', '55','53','81','0', '0', '84','74','8','24','82','35','4','5','9','2','9','4','9','4')
'9', 'Timberwolves', 'SF', 'Rasual Butler ', '36', '19', '62','10','69','32', '31', '3','32','15','36','91','46','7','3','4','1','8','6','5','6')

Вот так выглядит DB-таблица:

enter image description here

...