动态SQL INSERT

时间:2013-10-23 10:41:08

标签: php postgresql select dynamic

我有一个包含许多字段的PHP表单。我想要的是动态地向查询添加字段,该查询在PostgreSQL表中插入值,具体取决于这些字段的内容。如果一个字段(例如date,int等)为空,我不希望它在查询中,因为当我试图INSERT到表时,它会导致“语法错误”,因为它等待插入值。求救!

修改

INSERT INTO hcd_customermeters ("meterSN", "contractCode",
                "deviceManufacturer", "deviceType",
                "firstInstallationDate", "diameter",
                "pipeID", "operatorCreator", "warehouseDeliveryDate",
                "recordCreation", "SRID") 
            VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)' 

1 个答案:

答案 0 :(得分:3)

在你的mysql查询中定义变量名称不正确的更改名称:

PHP变量规则:

1) variable starts with the $ sign, followed by the name of the variable
2) variable name must start with a letter or the underscore character
3) **variable name cannot start with a number**
4) variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
Variable names are case sensitive ($y and $Y are two different variables)



$sql_insert = 'INSERT INTO hcd_customermeters ("meterSN", "contractCode", "deviceManufacturer", "deviceType", "firstInstallationDate", "diameter", "pipeID", "operatorCreator", "warehouseDeliveryDate", "recordCreation", "SRID") 
            VALUES ("'.$variableName."', "'.$variableName."', "'.$variableName."',"'.$variableName."', "'.$variableName."', "'.$variableName."', "'.$variableName."', "'.$variableName."', "'.$variableName."', "'.$variableName."',"'.$variableName."')';


mysql_query($sql_insert);

如果有任何问题,请提及。