数据未插入MySQL数据库

时间:2015-07-20 08:16:37

标签: php mysql

我的表设置如下图所示。

enter image description here

当我尝试运行以下代码将值插入数据库时​​,我收到错误:

  

失败:插入Betfairodds   (HorseBackLayTimeformTR)VALUES('Intrepid','5.5','5.9',   '0')

有人能帮忙,因为我试图调试代码。

//loop through each individual card
    foreach ($getdropdown2 as $dropresults) {

 $horse = preg_replace('/\h*[^ a-zA-Z].*$/m', '', trim($dropresults->childNodes->item(8)->textContent));
 $back =  trim(GetBetween($dropresults->childNodes->item(18)->textContent, 'Back', '£'));
 $lay =  trim(GetBetween($dropresults->childNodes->item(20)->textContent, 'Lay', '£'));

    $sql = "INSERT INTO `Betfairodds` (`Horse`,`Back`,`Lay`,`TimeformTR`)VALUES( '$horse','$back', '$lay', '0')";
    $res = mysqli_query($db, $sql);
        if (!$res) {

            echo PHP_EOL . "FAIL: $sql";
                 trigger_error(mysqli_error($db), E_USER_ERROR);

        }

}

2 个答案:

答案 0 :(得分:0)

我从0中删除了引号',因为它在架构中定义为int,当然在VALUES之前添加了空格..

$sql = "INSERT INTO `Betfairodds` (`Horse`,`Back`,`Lay`,`TimeformTR`) VALUES( '$horse','$back', '$lay', 0)";

答案 1 :(得分:-1)

你的陈述是错误的。您不应该在数据字段上放置单引号。所以应该是这样的:

$sql = "INSERT INTO `Betfairodds` (Horse,Back,Lay,TimeformTR)VALUES( '$horse','$back', '$lay', '0')";