¿Insertar nuevas filas en db usando php y DBMS es postgresql?

I searched for this 'basic' (I'm new at postgresql but a bit familiar w/ MySQL) question in the internet but there are only a few references when it comes to postgresql and php so I turned to stackoverflow. :D

I followed the example at the yii website http://www.yiiframework.com/doc/guide/1.1/en/database.dao but I keep on getting errors. Here is my code:

    $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();

My error is a server error: 500. Is this the right way of inserting new rows in postgre?

[EDITAR EDITAR]

I've found out that my syntax was wrong (after randomly replacing " with ' and some arrows).

Dice como:

$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();

but now I'm having problems with foreign keys. T.T Anyone knows how to? Thanks. :D

preguntado el 10 de marzo de 12 a las 01:03

Saying "now I'm having problems with foreign keys" means little. Well, it's better than the original "server error: 500" but not by much. -

Why using PDO when pg_query_params() is so much easier to use? -

@FrankHeikens - first, it's not easier to use pg_query_params and second, PDO is much better (and actually easier) to use for underlying database communication. As for original question - giving us vague descriptions of your problems won't get you far. "I'm having problems with foreign keys" can be literally thousands of different problems and you haven't pointed one out. You really think someone will help you like that? -

1 Respuestas

It will much much simpler to use PDO with prepared statement:

$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,));

respondido 10 mar '12, 10:03

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.