2012-03-10 86 views
-2

我在网上搜索了这个'基本'(我是postgresql的新手,但有点熟悉w/MySQL)的问题,但是当涉及到postgresql和php时,我只找到了一些参考,所以我转向了stackoverflow。 :D使用php和DBMS在db中插入新行是postgresql?

我跟着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(); 

但TT

+3

说“现在我有外键问题”的意思很小。那么,它比原来的“服务器错误:500”更好,但不是太多。 – 2012-03-10 03:17:51

+0

为什么在使用pg_query_params()时更容易使用PDO? – 2012-03-10 08:00:15

+0

@FrankHeikens - 首先,使用'pg_query_params'并不容易,其次,PDO比底层数据库通信更好(实际上更容易)。至于原始问题 - 给我们模糊的描述你的问题不会让你走得更远。 “我有外键问题”可能会成千上万个不同的问题,你没有指出一个问题。你真的认为有人会帮助你吗? – 2012-03-10 10:31:01

回答

1

用准备好的语句使用PDO会简单得多:

0现在我遇到了外键问题
$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,));