使用execute($array)插入时出错


I am getting an error when inserting with execute($array)

我有一个表

percentile      int(3)  No           
FoSW            int(3)  Yes     NULL     
dfLR            int(3)  Yes     NULL     
FoA             int(3)  Yes     NULL     
SoG             int(3)  Yes     NULL     
RST             int(3)  Yes     NULL     
SSW             int(3)  Yes     NULL    
total           int(3)  No  

和一个阵列:

Array
(
    [percentile] => 99
    [FoSW] => 125
    [DfLR] => 110
    [FoA] => 60
    [SoG] => 120
    [RST] => 40
    [SSW] => 45
    [total] => 500
)

而这个代码由于某种原因而不起作用。。。Catch不会抛出错误。只是我的if语句反映了错误。。。

if ($_POST['percent']=='add'){
    try{
        $post = $_POST;
        unset($post['percent']);
        $sth = $dbh->prepare("INSERT INTO percentiles (percentile, FoSW, dfLR, FoA, SoG, RST, SSW, total) VALUES (?,?,?,?,?,?,?,?)");
        if ($sth->execute($post)){
            echo 'done<br/>';
        }
        else echo 'error';
    }
    catch(PDOException $e){
        echo 'error'.$e;
    }
}

您的数组$postINSERT操作所需的八个值相匹配,但该数组应该由整数索引,而不是由关联数组/字典索引。

Array
(
    [percentile] => 99
    [FoSW] => 125
    [DfLR] => 110
    [FoA] => 60
    [SoG] => 120
    [RST] => 40
    [SSW] => 45
    [total] => 500
)

如果您按如下方式更改prepare()调用,上述数组将工作:

$sth = $dbh->prepare("INSERT INTO percentiles 
        (percentile, FoSW, 
        dfLR, FoA, 
        SoG, RST, 
        SSW, total) 
    VALUES
        (:percentile, :FoSW, 
        :DfLR, :FoA, 
        :SoG, :RST, 
        :SSW, :total)");