SQL语法错误(引用??? !!!)

时间:2016-07-26 11:35:17

标签: php mysql sql

有人可以帮我调试吗

<?php
$file_name = basename(__FILE__,'.php');
include("conf.php");
include("XMLSoccer.php");

$years = 1;    ///<-------NUMBER OF YEARS TO GO BACK
$leagueretrive = 3;   ///<--------THE LEAGUE ID TO RETRIEVE DATA FOR

$date1 = date('y', strtotime("-$years years"));
$date2 = date("y");
//CHECKING IF TABLE EXIST IF NOT CREATE NEW
$table = $file_name;
$query = "SELECT ID FROM " . $table;
$resultat = mysqli_query($conn,$query);


if(empty($resultat)) {
    echo "<p>" . $table . " table does not exist</p>";
    $query = mysqli_query($conn,"CREATE TABLE IF NOT EXISTS $file_name (
        Id int NOT NULL PRIMARY KEY,
        HomeGoalDetails varchar(800) NOT NULL,
    )CHARACTER SET utf8 COLLATE utf8_general_ci");
}
else {
    echo "<p>" . $table . "table exists</p>";
} // else

/////GETING THE DATA FROM SERVICE

try {
    $soccer = new XMLSoccer($api_key);
    $soccer->setServiceUrl("http://www.xmlsoccer.com/FootballDataDemo.asmx");
    $results = $soccer->GetHistoricMatchesByLeagueAndSeason(array("league"=>$leagueretrive,"seasonDateString"=>"$date1$date2"));
    print_r($results);
} catch (XMLSoccerException $e) {
    echo "XMLSoccerException: " . $e->getMessage();
}
foreach ($results->Match as $team) {
    $id = $team->Id;
    $homeGoalDetails = $team->HomeGoalDetails;

///INSERTING DATA INTO THE TABLE
    $sql = "INSERT INTO $file_name (HomeGoalDetails)
VALUES ('$homeGoalDetails')
on duplicate key update HomeGoalDetails='$homeGoalDetails'";
}
    if ($conn->query($sql) === TRUE) {
        echo "New record created successfully";
    } else {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }
$conn->close();
?>

我得到的回应

  

错误:INSERT INTO testing2(HomeGoalDetails)VALUES('35':Stefan   Johansen; 4':惩罚Leigh Griffiths;')重复密钥更新   HomeGoalDetails ='35':Stefan Johansen; 4':惩罚Leigh Griffiths;'   您的SQL语法有错误;检查手册   对应于您的MariaDB服务器版本,以获得正确的语法   靠近':Stefan Johansen; 4':惩罚Leigh Griffiths;')复制品   密钥更新HomeGo'在第2行处理完成,退出代码为0

1 个答案:

答案 0 :(得分:0)

您的查询中有未转义的引号。

尝试:

<?php
$file_name = basename(__FILE__,'.php');
include("conf.php");
include("XMLSoccer.php");

$years = 1;    ///<-------NUMBER OF YEARS TO GO BACK
$leagueretrive = 3;   ///<--------THE LEAGUE ID TO RETRIEVE DATA FOR

$date1 = date('y', strtotime("-$years years"));
$date2 = date("y");
//CHECKING IF TABLE EXIST IF NOT CREATE NEW
$table = $file_name;
$query = "SELECT ID FROM " . $table;
$resultat = mysqli_query($conn,$query);


if(empty($resultat)) {
    echo "<p>" . $table . " table does not exist</p>";
    $query = mysqli_query($conn,"CREATE TABLE IF NOT EXISTS $file_name (
        Id int NOT NULL PRIMARY KEY,
        HomeGoalDetails varchar(800) NOT NULL,
    )CHARACTER SET utf8 COLLATE utf8_general_ci");
}
else {
    echo "<p>" . $table . "table exists</p>";
} // else

/////GETING THE DATA FROM SERVICE

try {
    $soccer = new XMLSoccer($api_key);
    $soccer->setServiceUrl("http://www.xmlsoccer.com/FootballDataDemo.asmx");
    $results = $soccer->GetHistoricMatchesByLeagueAndSeason(array("league"=>$leagueretrive,"seasonDateString"=>"$date1$date2"));
    print_r($results);
} catch (XMLSoccerException $e) {
    echo "XMLSoccerException: " . $e->getMessage();
}
foreach ($results->Match as $team) {
    $id = $team->Id;
    $homeGoalDetails = $team->HomeGoalDetails;

///INSERTING DATA INTO THE TABLE
    $sql = "INSERT INTO $file_name (HomeGoalDetails)
VALUES ('".mysqli_real_escape_string($conn,$homeGoalDetails)."')
on duplicate key update HomeGoalDetails='".mysqli_real_escape_string($conn,$homeGoalDetails)."'";
}
    if ($conn->query($sql) === TRUE) {
        echo "New record created successfully";
    } else {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }
$conn->close();
?>