2012-03-10 92 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,));