我怎样才能将数据插入到mysql中?

时间:2014-06-03 05:18:37

标签: php mysql phpmyadmin sql-insert

//这是完整的脚本。 我需要将返回的数组结果发送到mysql。我在mySQL插入函数上尝试了很多变种,但mysql仍然不会填充数据。 mysql中的表格包含列名称' date',' home',' score',#39; away'。我使用xamp作为本地主机,数据库名称为:' brazil',表名:' seriea'。任何想法,将不胜感激!感谢。

<?php 

$data = array();
$html = file_get_contents('http://www.soccerstats.com/round_details.asp?league=brazil'); //get the html returned from the following url
$doc = new DOMDocument();
libxml_use_internal_errors(true);

if(!empty($html)){
    $doc->loadHTML('<meta http-equiv="content-type" content="text/html; charset=utf-8">'.$html);
    libxml_clear_errors();
    $xpath = new DOMXPath($doc);

    $entries = $xpath->query('//table[@class="stat"]');
    foreach($entries as $key => $value) {



        $data[] = array(
            'date' => trim($value->getElementsByTagName('font')->item(0)->nodeValue),
            'home' => trim($value->getElementsByTagName('font')->item(1)->nodeValue),
            'score' => trim($value->getElementsByTagName('font')->item(2)->nodeValue),
            'away' => trim($value->getElementsByTagName('font')->item(3)->nodeValue),
        );
    }
}


echo "<pre>";

print_r($data);
echo "</pre>";

// Create connection
$con=mysqli_connect("***","***","***");
mysqli_select_db('brazil', $con);

// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

else
{
$date = $_POST['date'];
$home = $_POST['home'];
$score = $_POST['score'];
$away = $_POST['away'];
$sql="INSERT INTO seriea (date, home, score, away) VALUES ('$date','$home','$score','$away')";
if (!mysqli_query($sql,$con))
{
die('Error: ' . mysqli_error());
}
else{
echo "1 record added";
}
}
mysqli_close($con);
?>

//输出到mysql //

Array
(
    [0] => Array
        (
            [date] => 1 Jun 14
            [home] =>  Fluminense 
            [score] => 1 - 1
            [away] =>  Internacional 
        )

    [1] => Array
        (
            [date] => 1 Jun 14
            [home] =>  Vitória 
            [score] => 0 - 1
            [away] =>  Sport Recife 
        )

    [2] => Array
        (
            [date] => 1 Jun 14
            [home] =>  Corinthians 
            [score] => 1 - 1
            [away] =>  Botafogo 
        )

    [3] => Array
        (
            [date] => 1 Jun 14
            [home] =>  Chapecoense 
            [score] => 2 - 1
            [away] =>  Bahia 
        )

    [4] => Array
        (
            [date] => 1 Jun 14
            [home] =>  Cruzeiro 
            [score] => 3 - 0
            [away] =>  Flamengo 
        )

    [5] => Array
        (
            [date] => 1 Jun 14
            [home] =>  Santos 
            [score] => 2 - 0
            [away] =>  Criciúma 
        )

    [6] => Array
        (
            [date] => 1 Jun 14
            [home] =>  Grêmio 
            [score] => 0 - 0
            [away] =>  Palmeiras 
        )

    [7] => Array
        (
            [date] => 1 Jun 14
            [home] =>  Figueirense 
            [score] => 1 - 3
            [away] =>  Atlético PR 
        )

    [8] => Array
        (
            [date] => 31 May 14
            [home] =>  São Paulo 
            [score] => 2 - 1
            [away] =>  Atlético MG 
        )

    [9] => Array
        (
            [date] => 31 May 14
            [home] =>  Coritiba 
            [score] => 3 - 0
            [away] =>  Goiás 
        )

    [10] => Array
        (
            [date] => 30 May 14
            [home] =>  Bahia 
            [score] => 0 - 2
            [away] =>  Santos 
        )

    [11] => Array
        (
            [date] => 29 May 14
            [home] =>  Internacional 
            [score] => 2 - 0
            [away] =>  Chapecoense 
        )

    [12] => Array
        (
            [date] => 29 May 14
            [home] =>  Flamengo 
            [score] => 1 - 1
            [away] =>  Figueirense 
        )

    [13] => Array
        (
            [date] => 29 May 14
            [home] =>  Atlético MG 
            [score] => 2 - 0
            [away] =>  Fluminense 
        )

    [14] => Array
        (
            [date] => 29 May 14
            [home] =>  Atlético PR 
            [score] => 2 - 2
            [away] =>  São Paulo 
        )

    [15] => Array
        (
            [date] => 29 May 14
            [home] =>  Corinthians 
            [score] => 1 - 0
            [away] =>  Cruzeiro 
        )

    [16] => Array
        (
            [date] => 29 May 14
            [home] =>  Goiás 
            [score] => 0 - 0
            [away] =>  Vitória 
        )

)

1 个答案:

答案 0 :(得分:0)

从数据所在的$data开始,您需要从中构建查询。考虑这个例子:

$values = '';
$initial_statement = 'INSERT INTO `seriea` (`date`, `home`, `score`, `away`) VALUES ';
foreach($data as $key => $value) {
    $values[] = "('".implode("','", $value)."')";
}

$values = implode(',', $values);
$complete_statement = $initial_statement . $values;
$query = mysqli_query($con ,$complete_statement);

$complete_statement应该产生这样的结果:

INSERT INTO `seriea` (`date`, `home`, `score`, `away`) VALUES ('1 Jun 14',' Fluminense ','1 - 1',' Internacional '),('1 Jun 14',' Vitória ','0 - 1',' Sport Recife '),('1 Jun 14',' Corinthians ','1 - 1',' Botafogo '),('1 Jun 14',' Chapecoense ','2 - 1',' Bahia '),('1 Jun 14',' Cruzeiro ','3 - 0',' Flamengo '),('1 Jun 14',' Santos ','2 - 0',' Criciúma '),('1 Jun 14',' Grêmio ','0 - 0',' Palmeiras '),('1 Jun 14',' Figueirense ','1 - 3',' Atlético PR '),('31 May 14',' São Paulo ','2 - 1',' Atlético MG '),('31 May 14',' Coritiba ','3 - 0',' Goiás '),('30 May 14',' Bahia ','0 - 2',' Santos '),('29 May 14',' Internacional ','2 - 0',' Chapecoense '),('29 May 14',' Flamengo ','1 - 1',' Figueirense '),('29 May 14',' Atlético MG ','2 - 0',' Fluminense '),('29 May 14',' Atlético PR ','2 - 2',' São Paulo '),('29 May 14',' Corinthians ','1 - 0',' Cruzeiro '),('29 May 14',' Goiás ','0 - 0',' Vitória ')
  

Important Note: Please add this on top of your php file

   header('Content-Type: text/html; charset=utf-8');

  这样你就可以得到“Fluminense”而不是“Fluminense”