使用php和DBMS在数据库中插入新行是postgresql


Inserting new rows in db using php and DBMS is postgresql?

我在互联网上搜索了这个"基本"问题(我是postgresql的新手,但对MySQL有点熟悉),但当涉及到postgresql和php时,只有一些参考文献,所以我转向了stackoverflow。:D

我在yii网站上学习了这个例子http://www.yiiframework.com/doc/guide/1.1/en/database.dao但我总是犯错误。这是我的代码:

    $barya = 'INSERT INTO "BILL" (Assessed_Value,Total_Assessed_Value,Penalty_Percentage,Basic_Tax,SEF_Tax,Discount) VALUES (:Assessed_Value,:Total_Assessed_Value,:Penalty_Percentage,:Basic_Tax,:SEF_Tax,:Discount)';
$command = $connection->createCommand($barya);
        $command = $connection->createCommand($barya);
        $command = bindParam(":Assessed_Value", $compute, PDO::PARAM_STR);
        $command =bindParam(":Total_Assessed_Value", $compute, PDO::PARAM_STR);
        $command =bindValue(":Penalty_Percentage", 0.20, PDO::PARAM_STR);
        $command =bindValue(":Basic_Tax", 0.15, PDO::PARAM_STR);
        $command =bindValue(":SEF_Tax", 0.20, PDO::PARAM_STR);
        $command =bindValue(":Discount", 0.03, PDO::PARAM_STR);
        $command->execute();

我的错误是服务器错误:500。这是在postgre中插入新行的正确方法吗?

[编辑编辑]

我发现我的语法是错误的(在随机将"替换为"和一些箭头之后)

它就像:

$barya = 'INSERT INTO "BILL" ("Assessed_Value","Total_Assessed_Value","Penalty_Percentage","Basic_Tax","SEF_Tax","Discount") VALUES(:Assessed_Value,:Total_Assessed_Value,:Penalty_Percentage,:Basic_Tax,:SEF_Tax,:Discount)';
    $command = $connection->createCommand($barya);
    $command->bindParam(":Assessed_Value", $compute, PDO::PARAM_STR);
    $command->bindParam(":Total_Assessed_Value", $compute, PDO::PARAM_STR);
    $command->bindParam(":Penalty_Percentage", $compute, PDO::PARAM_STR);
    $command->bindParam(":Basic_Tax", $compute, PDO::PARAM_STR);
    $command->bindParam(":SEF_Tax", $compute, PDO::PARAM_STR);
    $command->bindParam(":Discount", $compute, PDO::PARAM_STR);
$command->execute();

但现在我的外键出了问题。有人知道怎么做吗?谢谢D

将PDO与prepared语句一起使用会简单得多:

$barya = 'INSERT INTO BILL (Assessed_Value,Total_Assessed_Value,Penalty_Percentage,Basic_Tax,SEF_Tax,Discount) VALUES(:Assessed_Value,:Total_Assessed_Value,:Penalty_Percentage,:Basic_Tax,:SEF_Tax,:Discount)';
$command = $connection->prepare($barya);
// new data
$Assessed_Value = ' ... ';
$Total_Assessed_Value= ' ... ';
$Penalty_Percentage= ' ... ';
$Basic_Tax= ' ... ';
$SEF_Tax= ' ... ';
$Discount = ' ... ';
$command->execute(array(
  ':Assessed_Value'=>$Assessed_Value,
  ':Total_Assessed_Value'=>$Total_Assessed_Value,
  ':Penalty_Percentage'=>$Penalty_Percentage,
  ':Basic_Tax'=>$Basic_Tax,
  ':SEF_Tax'=>$SEF_Tax,
  ':Discount'=>$Discount,));