Query not executing

时间:2017-07-12 08:18:52

标签: php postgresql

I am new to postgresql and i don't know what is the syntax error in my query.What happens is when i execute the following query..it shows syntax error in pgadmin..

$sql1 = "INSERT INTO cpmu_phy_achivement(implementor, scheme_code, phycomp_code, month_code, finmonthcode, year_code, target_ach, updated_by, remarks, ason, reason_code)
VALUES (0,'$schemecode', $phycompcode, $entrymonth, $finmonthcode, $finyearcode, $targetach, '$empcode', '$remarksave', '$as_onsave', $reason_codesave)";

when i am echoing the query..it appears as..

INSERT INTO cpmu_phy_achivement( implementor, scheme_code, phycomp_code, month_code, finmonthcode, year_code, target_ach, updated_by, remarks, ason, reason_code) 
VALUES (0, 'AGR3-17', 3, 6, 6, 2017, 0, '13', '', '2017-07-12', )

If i insert reason code it executes successfully..but when the reason code is null..the error occurs..help me solving this..please..

4 个答案:

答案 0 :(得分:1)

我强烈建议您使用预准备语句。他们会处理你这种边缘案件。

首先,阅读pg_prepare手册。

您的代码如下所示:

$sql1 = "INSERT INTO cpmu_phy_achivement(implementor, scheme_code, phycomp_code, month_code, finmonthcode, year_code, target_ach, updated_by, remarks, ason, reason_code)
VALUES (?,?,?,?,?,?,?,?,?,?,?)";

$stmt = pg_prepare($connection, $sql1);
$values = array(0,$schemecode, $phycompcode, $entrymonth, $finmonthcode, $finyearcode, $targetach, $empcode, $remarksave, $as_onsave, $reason_codesave);
$result = pg_execute($connection, $stmt, $values);

请注意,您不必处理值的字符串或数字表示。

进一步阅读:

如果您不想重用sql语句,可以使用pg_query_params,正如Craig Ringer建议的那样。

答案 1 :(得分:0)

You've got 11 fields and only 10 values.

Ah, look, $reason_codesave is not in single quotes. That's why it's missing from the echo.

$sql1 = "INSERT INTO cpmu_phy_achivement(implementor, scheme_code,phycomp_code, month_code, finmonthcode,year_code, target_ach,updated_by,remarks,ason,reason_code)
VALUES (0,'$schemecode',$phycompcode,$entrymonth,$finmonthcode,$finyearcode,$targetach,                                     '$empcode','$remarksave','$as_onsave','$reason_codesave')";

答案 2 :(得分:0)

在插入数据库之前检查$reason_codesave值,如果它为null,则传递一个空字符串。 试试这个

$reason_codesave=($reason_codesave)?$reason_codesave:' ';
$sql1 = "INSERT INTO cpmu_phy_achivement(implementor, scheme_code,phycomp_code, month_code,
          finmonthcode,year_code, target_ach,updated_by,remarks,ason,reason_code)
          VALUES (0,'$schemecode',$phycompcode,$entrymonth,$finmonthcode,$finyearcode,$targetach,
         '$empcode','$remarksave','$as_onsave','$reason_codesave')";

或者你可以这样做

if($reason_codesave){
$sql1 = "INSERT INTO cpmu_phy_achivement(implementor, scheme_code,phycomp_code, month_code,
          finmonthcode,year_code, target_ach,updated_by,remarks,ason,reason_code)
          VALUES (0,'$schemecode',$phycompcode,$entrymonth,$finmonthcode,$finyearcode,$targetach,
         '$empcode','$remarksave','$as_onsave','$reason_codesave')";
}else{
$sql1 = "INSERT INTO cpmu_phy_achivement(implementor, scheme_code,phycomp_code, month_code,
              finmonthcode,year_code, target_ach,updated_by,remarks,ason)
              VALUES (0,'$schemecode',$phycompcode,$entrymonth,$finmonthcode,$finyearcode,$targetach,
             '$empcode','$remarksave','$as_onsave')";
}

它适用于你。

答案 3 :(得分:-2)

使用下面的MySql插入查询来解决您的问题。

INSERT INTO cpmu_phy_achivement
set implementor='0', scheme_code='AGR3-17', phycomp_code='3', month_code='6', finmonthcode='6', 
year_code='2017', target_ach='0', updated_by='13', remarks='', ason='2017-07-12', reason_code='';